I am trying to receive class field value via reflection. But when I call my code I receive exception: IllegalArgumentException: Can not set class field field to java.lang.Class. (I am calling from javaagent)
My code:
private static Class<?> GetInstance(Class<?> clz) throws NoSuchFieldException, IllegalAccessException {
Class<?> clazz1 = null;
Field f = clz.getDeclaredField("INSTANCE");
f.setAccessible(true);
clazz1 = f.get(clz).getClass();
return clazz1;
}
private static Class GetClassLoader(Class<?> clz) throws NoSuchFieldException, IllegalAccessException, InstantiationException {
Field f = clz.getDeclaredField("classLoader");
f.setAccessible(true);
Class cls = f.get(clz).getClass();
return cls;
}
public static void agentmain(String agentArgs, Instrumentation inst){
try{
inst.addTransformer(new ClientTransfomer());
Class<?> FMLDeobfuscatingRemapper = null;
Class<?> InstanceClass;
Class<?>[] classes = inst.getAllLoadedClasses();
for(int i = 0;i < classes.length;i++){
if(classes[i].getName().contains("cpw.mods.fml.common.asm.transformers.deobf.FMLDeobfuscatingRemapper")){
FMLDeobfuscatingRemapper = classes[i];
}
}
Class<?> instance = GetInstance(FMLDeobfuscatingRemapper);
Class cloader = GetClassLoader(instance);
Method m = cloader.getDeclaredMethod("findClass");
m.setAccessible(true);
m.invoke(null, "net.minecraft.client.entity.EntityClientPlayerMP");
}catch (Exception e){
}
How to get class from field?
With fieldValue.getClass(). Reflection not required.
Your code makes no sense.
You have a method called GetInstance() which returns a Class, not an instance. You need to change the return value of this method to Object and remove the getClass() call inside it.
You have a method called GetClassLoader() which returns a Class, not a ClassLoader. You need to change the return type of this method to ClassLoader, and remove the getClass() call inside it.
You are then reflectively treating this object as though it was a ClassLoader, which it isn't, and trying to call findClass() on it.
But I can see no necessity for using a reflection at all here once you have the instance: you can just call getClass().getClassLoader().findClass() directly.
You mean something like that?
public class One {
public Two two = new Two();
}
public class Two {
public void hello(){
System.out.println("hello");
}
}
public static void main(String[] args) {
public static void main(String[] args) {
One one = new One();
Class<?> clazz = one.getClass();
try {
Field twoField = clazz.getField("two");
twoField.setAccessible(true);
Class<?> twoClazz =Class.forName(twoField.getType().getName());
Method method = twoClazz.getMethod("hello", null);
method.invoke(twoClazz.newInstance(), null);
} catch (NoSuchFieldException | IllegalAccessException | InvocationTargetException | NoSuchMethodException | ClassNotFoundException | InstantiationException e) {
e.printStackTrace();
}
}
}
Use getType() like below:
clz.getDeclaredField("fieldName").getType()
Related
I have a problem in creating an object.
Basically I would like to create an object from a given class by taking originally a string. I know that using Class.forName(field) works, but not in my case and I will show you why below. I have thought about reflection but fear it will cause the same problem.
My code as of right now is (template is a String):
int n = template.length();
String field = at.getFieldName().trim();
field = field.substring(0, field.length() - 1);
Class<?> correctClass = Class.forName(field);
UniqueEntity<correctClass> ue = new UniqueEntity<correctClass>();
The error message I get is that correctClass cannot be resolved to a type.
Once you used Class.forName() method you got your Class instance (variable correctClass) invoke method getConstructor() or getConstructors() to get an instance of class Constructor. Once you get your instance of class Constructor invoke method newInstance() to get an instance of your class. Here is the example:
Class myClass = Class.forName(field);
Constructor constructor = myClass.getConstructor()
Object o = constructor.newInstance()
All this assuming that your class has default constructor.
In order for you to be able to create a UniqueEntity with the type of your reflective class, you need to pass the class type into a generic helper method.
private static <T> UniqueEntity<T> createEntity(Class<T> clazz) {
return new UniqueEntity<T>(clazz);
}
Code
import java.lang.reflect.Type;
public class Generic {
public static void main(String[] args) {
try {
String field = "java.lang.Integer";
UniqueEntity<?> entity = fromField(field);
System.out.println(entity);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static UniqueEntity<?> fromField(String field) throws ClassNotFoundException {
return createEntity(Class.forName(field));
}
private static <T> UniqueEntity<T> createEntity(Class<T> clazz) {
return new UniqueEntity<T>(clazz);
}
private static class UniqueEntity<T> {
private final Type type;
public UniqueEntity(Class<T> clazz) {
this.type = clazz.getGenericSuperclass();
}
#Override
public String toString() {
return "UniqueEntity [type=" + type + "]";
}
}
}
I am trying to cast a class that I dynamically obtain that has implemented an interface. I have tried the following below but it doesn't seem to work. How I achieve this.
public InterfaceX test(){
InterfaceX x = null;
Class<?> classX = Class.forName("com.TestClassX");
x = (InterfaceX) classX;
return x;
}
EDIT:
I dont want to create an instance, since I am just looking to simply call a static method defined by the interface.
If x is a Class object, you cannot do x.staticMethod(). However, this is possible using reflection.
public interface Interface {
static void method() {
System.out.println("Hello, World!");
}
}
Then, in the main class you can do this.
import java.lang.reflect.*;
public class Main {
public static void invokeMethod(String className) {
try {
Class<?> clazz = Class.forName(className);
Method method = clazz.getDeclaredMethod("method");
method.invoke(null); // null is used for static methods. For instance methods, pass the instance.
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
invokeMethod("Interface");
}
}
This works, but you should generally try to avoid using reflection (anything in java.lang.reflect). Most of the time there is a better solution to the problem you are trying to solve.
Say I have two classes
class parentClass{
String myElement;
}
class childClass extends parentClass{
String notMyElemtent;
}
Now say there is an object of class childClass. Is there a way programatically tell that myElement in that object belongs to parentClass originally??
You can do it with reflection. Use obj.getClass().getField("myElement") to get Field object, representing your field.
Now you can use getDeclaringClass() method of Member interface to get class or interface actually declaring this member. So do something like this
childClass obj = new childClass();
Field field = obj.getClass().getField("myElement");
if (field.getDeclaringClass().equals(parentClass.class)) {
// do whatever you need
}
Is there a way to tell that myElement in that object belongs to parentClass originally?
Yes, you can use reflection to examine the fields of the super class:
Use Class.getSuperClass() to get the super class
Then, use
Class.getDeclaredFields() on the returned class from the above call to get a list of all fields declared by the super class
Class.getDeclaredField(String fieldName) to directly check the existence of a given field
In code, this could look like:
public static boolean belongsToParent(Object o, String fieldName) {
Class<?> sc = o.getClass().getSuperclass();
boolean result = true;
try {
sc.getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
result = false;
}
return result;
}
public static void main(String[] args) {
childClass cc = new childClass();
System.out.println("myElement belongs to parentClass: " +
belongsToParent(cc, "myElement"));
System.out.println("notMyElemtent belongs to parentClass: " +
belongsToParent(cc, "notMyElemtent"));
}
Output:
myElement belongs to parentClass: true
notMyElemtent belongs to parentClass: false
Well, use getDeclaredField(name) of a class, and if its not there, try looking at its super class and so on. Works for multiple levels of inheritance:
Class<?> clazz = childClass.class;
do {
try {
Field f = clazz.getDeclaredField(fieldName);
//there it is! print the name of the super class that holds the field
System.out.println(clazz.getName());
} catch (NoSuchFieldException e) {
clazz = clazz.getSuperclass();
}
} while (clazz != null);
import java.lang.reflect.Field;
public class Test4 {
public static void main(String[] args){
Child child = new Child();
System.out.println(getDeclaringClass(child.getClass(), "value"));
}
public static String getDeclaringClass(Class<?> clazz, String name) {
try {
Field field = clazz.getDeclaredField(name);
} catch (NoSuchFieldException e) {
if(clazz.getSuperclass() != null){
return getDeclaringClass(clazz.getSuperclass(), name);
}else{
return null;
}
}
return clazz.getName();
}
}
class Parent {
String value = "something";
}
class Child extends Parent {
}
This question already has answers here:
Instantiating generics type in java
(12 answers)
Closed 2 years ago.
I know Java's generics are somewhat inferior to .Net's.
I have a generic class Foo<T>, and I really need to instantiate a T in Foo using a parameter-less constructor. How can one work around Java's limitation?
One option is to pass in Bar.class (or whatever type you're interested in - any way of specifying the appropriate Class<T> reference) and keep that value as a field:
public class Test {
public static void main(String[] args) throws IllegalAccessException,
InstantiationException {
Generic<Bar> x = new Generic<>(Bar.class);
Bar y = x.buildOne();
}
}
public class Generic<T> {
private Class<T> clazz;
public Generic(Class<T> clazz) {
this.clazz = clazz;
}
public T buildOne() throws InstantiationException, IllegalAccessException {
return clazz.newInstance();
}
}
public class Bar {
public Bar() {
System.out.println("Constructing");
}
}
Another option is to have a "factory" interface, and you pass a factory to the constructor of the generic class. That's more flexible, and you don't need to worry about the reflection exceptions.
And this is the Factory implementation, as Jon Skeet suggested:
interface Factory<T> {
T factory();
}
class Araba {
//static inner class for Factory<T> implementation
public static class ArabaFactory implements Factory<Araba> {
public Araba factory() {
return new Araba();
}
}
public String toString() { return "Abubeee"; }
}
class Generic<T> {
private T var;
Generic(Factory<T> fact) {
System.out.println("Constructor with Factory<T> parameter");
var = fact.factory();
}
Generic(T var) {
System.out.println("Constructor with T parameter");
this.var = var;
}
T get() { return var; }
}
public class Main {
public static void main(String[] string) {
Generic<Araba> gen = new Generic<Araba>(new Araba.ArabaFactory());
System.out.print(gen.get());
}
}
Output:
Constructor with Factory<T> parameter
Abubeee
Here's a rather contrived way to do it without explicitly using an constructor argument. You need to extend a parameterized abstract class.
public class Test {
public static void main(String [] args) throws Exception {
Generic g = new Generic();
g.initParameter();
}
}
import java.lang.reflect.ParameterizedType;
public abstract class GenericAbstract<T extends Foo> {
protected T parameter;
#SuppressWarnings("unchecked")
void initParameter() throws Exception, ClassNotFoundException,
InstantiationException {
// Get the class name of this instance's type.
ParameterizedType pt
= (ParameterizedType) getClass().getGenericSuperclass();
// You may need this split or not, use logging to check
String parameterClassName
= pt.getActualTypeArguments()[0].toString().split("\\s")[1];
// Instantiate the Parameter and initialize it.
parameter = (T) Class.forName(parameterClassName).newInstance();
}
}
public class Generic extends GenericAbstract<Foo> {
}
public class Foo {
public Foo() {
System.out.println("Foo constructor...");
}
}
I really need to instantiate a T in Foo using a parameter-less
constructor
Simple answer is "you cant do that" java uses type erasure to implment generics which would prevent you from doing this.
How can one work around Java's limitation?
One way (there could be others) is to pass the object that you would pass the instance of T to the constructor of Foo<T>. Or you could have a method setBar(T theInstanceofT); to get your T instead of instantiating in the class it self.
For Java 8 ....
There is a good solution at https://stackoverflow.com/a/36315051/2648077 post.
This uses Java 8 Supplier functional interface
Use The Constructor.newInstance method. The Class.newInstance method has been deprecated since Java 9 to enhance compiler recognition of instantiation exceptions.
public class Foo<T> {
public Foo()
{
Class<T> newT = null;
instantiateNew(newT);
}
T instantiateNew(Class<?> clsT)
{
T newT;
try {
newT = (T) clsT.getDeclaredConstructor().newInstance();
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException | NoSuchMethodException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
return newT;
}
}
From https://stackoverflow.com/a/2434094/848072. You need a default constructor for T class.
import java.lang.reflect.ParameterizedType;
class Foo<T> {
public bar() {
ParameterizedType superClass = (ParameterizedType) getClass().getGenericSuperclass();
Class<T> type = (Class<T>) superClass.getActualTypeArguments()[0];
try {
T t = (T) type.getDeclaredConstructor().newInstance();
//Do whatever with t
} catch (Exception e) {
// Oops, no default constructor
throw new RuntimeException(e);
}
}
}
Generics in Java are generally more powerful than in C#.
If you want to construct an object but without hardwiring a constructor/static method, use an abstract factory. You should be able to find detailed information and tutorials on the Abstract Factory Pattern in any basic design patterns book, introduction to OOP or all over the interwebs. It's not worth duplicating code here, other than to mention that Java's closure syntax sucks.
IIRC, C# has a special case for specifying a generic type has a no-args constructor. This irregularity, by definition, presupposes that client code wants to use this particular form of construction and encourages mutability.
Using reflection for this is just wrongheaded. Generics in Java are a compile-time, static-typing feature. Attempts to use them at runtime are a clear indication of something going wrong. Reflection causes verbose code, runtime failures, unchecked dependencies and security vulnerabilities. (Class.forName is particularly evil.)
I could do this in a JUnit Test Setup.
I wanted to test a Hibernate facade so I was looking for a generic way to do it. Note that the facade also implements a generic interface. Here T is the database class and U the primary key.
Ifacade<T,U> is a facade to access the database object T with the primary key U.
public abstract class GenericJPAController<T, U, C extends IFacade<T,U>>
{
protected static EntityManagerFactory emf;
/* The properties definition is straightforward*/
protected T testObject;
protected C facadeManager;
#BeforeClass
public static void setUpClass() {
try {
emf = Persistence.createEntityManagerFactory("my entity manager factory");
} catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
}
}
#AfterClass
public static void tearDownClass() {
}
#Before
public void setUp() {
/* Get the class name*/
String className = ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[2].getTypeName();
/* Create the instance */
try {
facadeManager = (C) Class.forName(className).newInstance();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
Logger.getLogger(GenericJPAController.class.getName()).log(Level.SEVERE, null, ex);
}
createTestObject();
}
#After
public void tearDown() {
}
/**
* Test of testFindTEntities_0args method, of class
* GenericJPAController<T, U, C extends IFacade<T,U>>.
* #throws java.lang.ClassNotFoundException
* #throws java.lang.NoSuchMethodException
* #throws java.lang.InstantiationException
* #throws java.lang.IllegalAccessException
*/
#Test
public void testFindTEntities_0args() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException {
/* Example of instance usage. Even intellisense (NetBeans) works here!*/
try {
List<T> lista = (List<T>) facadeManager.findAllEntities();
lista.stream().forEach((ct) -> {
System.out.println("Find all: " + stringReport());
});
} catch (Throwable ex) {
System.err.println("Failed to access object." + ex);
throw new ExceptionInInitializerError(ex);
}
}
/**
*
* #return
*/
public abstract String stringReport();
protected abstract T createTestObject();
protected abstract T editTestObject();
protected abstract U getTextObjectIndex();
}
Quick solution that worked for me.
I see there is already an answer for this and this may not even be the best way to go about it. Also, for my solution you'll need Gson.
However, I ran into a situation where I needed to create an instance of a generic class of type java.lang.reflect.Type.
The following code will create an instance of the class you want with null instance variables.
T object = new Gson().fromJson("{}", myKnownType);
Where myKnownType is known before hand and obtained via TypeToken.getType().
You can now set appropriate properties on this object. Again, this may not be the best way to do this but it works as a quick solution if that's what you need.
This question already has answers here:
How can a derived class invoke private method of base class?
(7 answers)
Closed 7 years ago.
public class A{
private int getC(){
return 0;
}
}
public class B extends A{
public static void main(String args[]){
B = new B();
//here I need to invoke getC()
}
}
Can you please tell me if it is possible to do sush thing via reflection in java?
class A{
private void a(){
System.out.println("private of A called");
}
}
class B extends A{
public void callAa(){
try {
System.out.println(Arrays.toString(getClass().getSuperclass().getMethods()));
Method m = getClass().getSuperclass().getDeclaredMethod("a", new Class<?>[]{});
m.setAccessible(true);
m.invoke(this, (Object[])null);
} catch (Exception e) {
e.printStackTrace();
}
}
}
EDIT:
This is quiet an old post but adding a few nuggets of advice
Reconsider your design
Calling private method of parent, though possible through Reflection, but should not be done. Calling private methods on parent might leave the class in invalid state and may lead to unexpected behaviors.
You can do it using reflection, but unless there is a very good reason to do so, you should first reconsider your design.
The code below prints 123, even when called from outside A.
public static void main(String[] args) throws Exception {
Method m = A.class.getDeclaredMethod("getC");
m.setAccessible(true); //bypasses the private modifier
int i = (Integer) m.invoke(new A());
System.out.println("i = " + i); //prints 123
}
public static class A {
private int getC() {
return 123;
}
}
You should declare getc protected. That's exactly what it's for.
As for reflection: Yes, it is possible. You'd have to call setAccessible on the method object though. And it's bad style... ;-)
getDeclaredMethod will only return the private methods in the current class not the inherited methods. To achieve it you need to navigate the inheritance graph via the getSuperclass method. Here is a code snippet that does it
private Method getPrivateMethod(Object currentObject) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Class<?> currentClass = currentObject.getClass();
Method method = null;
while (currentClass != null && method == null) {
try {
method = currentClass.getDeclaredMethod("getC");
} catch (NoSuchMethodException nsme) {
// method not present - try super class
currentClass = currentClass.getSuperclass();
}
}
if (method != null) {
method.setAccessible(true);
return method;
} else {
throw new NoSuchMethodException();
}
}
you can try like this using reflection:
Method getCMethod = A.class.getDeclaredMethod("getC");
getCMethod.setAccessible(true);
getCMethod.invoke(new A());