How can I access a private field of an inner class in another package which extends another inner class in yet another package by using reflection (package java.lang.reflect)?
package MainPackage;
import java.lang.reflect.Field;
class PrimaryClass
{
public static void main(String[] args)
{
AuxiliaryPackage.TerciaryClass.BugattiVeyron bugattiVeyron =
(new AuxiliaryPackage.TerciaryClass()).new BugattiVeyron(
new MainPackage.SecondaryClass());
System.out.println("Name: "+ bugattiVeyron.name);
System.out.println("Tires type: " + bugattiVeyron.tiresType);
try
{
Class bugattiVeyronClass = bugattiVeyron.getClass();
Field invisibleField1 = bugattiVeyronClass.getDeclaredField("topSpeed");
invisibleField1.setAccessible(true);
System.out.println(invisibleField1.get(bugattiVeyron));
Field invisibleField2 = bugattiVeyronClass.getDeclaredField("tamperedOdometer");
invisibleField2.setAccessible(true);
System.out.println(invisibleField2.get(bugattiVeyron));
}
catch (Exception e)
{
/* I ALWAYS GET AN EXCEPTION !! */
/* I need a change to be done IN the TRY block. */
/* WHEN THE BugattiVeyron class DOES NOT EXTEND another inner class, NO EXCEPTION. */
/* See at the end for a detail of the just mentioned case. */
System.out.println("An exception has occurred.");
System.err.println(e.getMessage());
e.printStackTrace();
}
}
}
package MainPackage;
public class SecondaryClass
{
public class SportsCar
{
/* Public fields. Accesible from any package. */
public String name = new String();
/* Protected fields. Accesible from the same package and subclasses in any package */
protected String tiresType = new String();
/* Package fields. No explicit modifier. Accessible from the same package only. */
int topSpeed; //km per hour
/* Private fields. Accessible from the same class only. */
private boolean tamperedOdometer;
/* Default constructor */
public SportsCar()
{
name = "Sports car";
tiresType = "Race";
topSpeed = 250;
tamperedOdometer = false;
}
public SportsCar(String name, String tiresType, int topSpeed, boolean tamperedOdometer)
{
this.name = name;
this.tiresType = tiresType;
this.topSpeed = topSpeed;
this.tamperedOdometer = tamperedOdometer;
}
}
}
package AuxiliaryPackage;
public class TerciaryClass
{
public class BugattiVeyron extends MainPackage.SecondaryClass.SportsCar
{
/* Default constructor */
public BugattiVeyron(MainPackage.SecondaryClass secondaryClass)
{
secondaryClass.super("Buggati Veyron", "Michelin Pilot Sport PS2 PAX", 431, false);
}
}
}
When the BugattiVeyron class does not extend anything (after a few changes in the code) I instantiate it like this:
AuxiliaryPackage.TerciaryClass.BugattiVeyron bugattiVeyron = (new
AuxiliaryPackage.TerciaryClass()).new BugattiVeyron();
… and no exception rises… So what should I change in the try block to get things done well?
Thanks in advance!!
EDIT 1.
I didn’t mean to yell. I thought that the intro might get lost because of the length of the post. I have included the stackTrace. I apologize. This is my second question.
Here is the first exception (The other one is similar):
java.lang.NoSuchFieldException: topSpeed
at java.lang.Class.getDeclaredField(Unknown Source)
at MainPackage.PrimaryClass.main(PrimaryClass.java:17)
The line 17 is:
Field invisibleField1 = bugattiVeyronClass.getDeclaredField(“topSpeed”);
EDIT 2.
I know it does NOT look like a “good programming pattern”. This is about a research on the limits of java.lang.reflect.
EDIT 3: THE ANSWER.
I want to thank all those who were kind and patient to me and, tried to provide answers. Finally, I got the solution from vsb. So, it took around an hour to get the answer since I posted the question. A record! My first question has not received any attention at all since March the 1st.
This code works.
BugattiVeyron class doesn’t have these fields, they are from superclass and must be accessed correspondingly.
You can use following method to find field in the class hierarchy:
But please consider another way to solve your problem. It’s very bad style and should only be used when absolutely necessary.