1

Is it possible in Java to get the 'parent' class of an object using an instance of it and its class type?

Lets say we have these two class types and the annotation type:

public class Foo{

}

public class Bar{

    @TestAnnotation
    public Foo mFoo = new Foo();
}

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation{

}

I would want to get the Field object of mFoo using an instance of Foo, otherwise null, with this I would get the annotation using TestAnnotation test = field.TestAnnotation(SQLiteID.class);


The end result I want is to know whether the instance of Foo is part of another type that contains a certain annotation

This seems rather far fetched so I don't expect it to be possible.

2
  • Are you asking about trying to find out if some class with a certain annotation holds a reference to the instance in mFoo? Oct 12, 2013 at 4:23
  • Correct, my understanding of Java says that it isn't possible in a feasible way, however I wondered if there was anyone out there that new something I didn't about it
    – FabianCook
    Oct 12, 2013 at 4:34

3 Answers 3

4

This is not possible/feasible in general.

The same object instance can have many names - that is, the same object can be assigned to many different static fields, instance fields, parameters, local variables (etc) simultaneously.

All "solutions" involve some form of iteration over all such variables, which is rarely feasible and such general designs should be avoided. Even with a reduced domain (such as with annotations) a similar issue remains - i.e. how to find all such roots (objects instances with members having a certain annotation) of said domain?

1
  • Agreed, more of a discussion question.
    – FabianCook
    Oct 12, 2013 at 4:35
2

I would want to get the Field object of mFoo using an instance of Foo.

This is doing things backwards and not possible.

You can get the Field of Bar by testing its type and then try to retrieve a specific annotation

Field[] fields = Bar.class.getDeclaredFields();
for (Field field : fields) {
    if (Foo.class.isAssignableFrom(field.getType())) {
        TestAnnotation testAnnotation = field.getAnnotation(TestAnnotation.class);
        System.out.println(testAnnotation); // might be null, if annotation doesn't exist
    }
}

Now if you have an instance of Foo you can also compare it to the field value of an instance of Bar.

6
  • I was thinking of passing the Bar object to Foo but I don't believe it would be in any way feasible
    – FabianCook
    Oct 12, 2013 at 4:36
  • @SmartLemon What do you mean passing the Bar object to Foo? Oct 12, 2013 at 4:37
  • As in, if it was the parent while constructing the object Foo I could pass the instance of Bar that I am using Foo with.
    – FabianCook
    Oct 12, 2013 at 5:03
  • @SmartLemon :( I don't understand. What parent? Oct 12, 2013 at 5:06
  • No need to worry. Think of in Android passing the context to something like a DbHelper where it uses that context to resolve things like what applications data folder the database should be stored in.
    – FabianCook
    Oct 12, 2013 at 5:29
1

This isn't possible in Java. It would be possible by writing a JVM agent but would probably have a severe performance penalty because of all the bookkeeping.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.