1

Having something like in Java:

class A {}
class B {private A a;}
class C {private A a;}

How could I know which is the class that declared a ?

I.e. I want to get class B or class C

Any ideas are appreciated.

Regards

Henrik Aasted Sørensen
  • 6,966
  • 11
  • 51
  • 60
Sergiu
  • 2,502
  • 6
  • 35
  • 57
  • Weird requirement. The only thing I can think of is passing the value as a parameter to the `a` objects, either in its constructor or as a private attribute. Nothing in the JVM would provide you this info. – SJuan76 Mar 08 '13 at 11:20
  • 2
    *"Getting declaring class in Java"* Use a debugger? Why would this be needed in production code? – Andrew Thompson Mar 08 '13 at 11:20
  • I don't understand what you want to do... You want ALL classes running in the JVM that have an attribute of class A? Why do you want this? – m0skit0 Mar 08 '13 at 11:22
  • Why you want to know which is the class that declared a ? – Amira Mar 08 '13 at 11:24
  • Maybe you want to read [this](http://stackoverflow.com/questions/520328/can-you-find-all-classes-in-a-package-using-reflection) – m0skit0 Mar 08 '13 at 11:32

5 Answers5

5

You couldn't just with the structure you've specified. You'd have to pass a reference to an instance of B or C into A's constructor, then write some logic to determine the type passed in.

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
2

As i understand the question you need to find all usages of class A (type usages) in your code (Please correct me if i'm wrong).

It depend on your IDE and the installed plugins for code inspection, most IDE's provide such a functionality, in Eclipse for example you can right click a Class and select "References->Project"

And if your IDE does not have this there are alot of tools for java, take a look at: A tool like ReSharper, but for Java?

Community
  • 1
  • 1
CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
  • in Java, using Eclipse, you can right click a Class and select "References->Project". I'm not sure if the question was about runtime or not... – Salvatorelab Mar 08 '13 at 11:31
2

You have to do someting like:

class A {

    boolean itWasC;

    public A( C objectC ) {
        itWasC = true;
    }

    public A( B objectB ) {
        itWasC = false;
    }
}

And once you create an object of class "A" from class B or class C pass this to the constructor. For example: A objectA = new A( this )

It is weird, and you can't do it without instanciating objects.

Salvatorelab
  • 11,614
  • 6
  • 53
  • 80
2
class A {
    public void print(){
        String className = new Exception().getStackTrace()[1].getClassName();
        System.out.println(className);
    }
}
class A1 {
    private A a;
    public A1(){
        a= new A();
        a.print();
    }
}
class A2 {
    private A a;
    public A2(){
        a= new A();
        a.print();
    }
}
public class C {
    public static void main(String[] args) {
        A1 a1= new A1();
        A2 a2 = new A2();
    }
}
Salvatorelab
  • 11,614
  • 6
  • 53
  • 80
neonleo
  • 194
  • 1
  • 2
0

you cannot do that in java unless you have a reference of the Object in Class A... definitely not through programatically nor in runtime.

But if you just want to find out the classes that are referencing Class A , one option is to rename the class to something else and try to compile.

And Voila , compiler would list all the classes that Reference Class A , but cannot resolve to a type.

Another alternative ,would be to use Reflections to find out the Variables in a Class and compare if the type of the variable is of Class A type

Heres a sample Program i wrote to do just that

import java.lang.reflect.Field;

import com.test.ClassA;

 package com.test;
    public class ClassA {

   }



    public class ClassB {

        private ClassA classA;

        public static void main(String[] args) throws Exception,
                NoSuchFieldException {

            Field classVariable = ClassB.class
                    .getDeclaredField("classA");

            classVariable.setAccessible(true);

            System.out.println(classVariable.getType());

            System.out.println(ClassA.class);

            if (ClassA.class.equals(classVariable.getType())) {
                System.out.println("Class A is referenced");
            }

        }
    }

Result

class com.test.ClassA
class com.test.ClassA
Class A is referenced
Sudhakar
  • 4,823
  • 2
  • 35
  • 42