Sergiu
Sergiu

Reputation: 2626

Getting declaring class in Java

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

Upvotes: 1

Views: 421

Answers (5)

neonleo
neonleo

Reputation: 192

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();
    }
}

Upvotes: 2

Sudhakar
Sudhakar

Reputation: 4873

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

Upvotes: 0

CloudyMarble
CloudyMarble

Reputation: 37576

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?

Upvotes: 2

Salvatorelab
Salvatorelab

Reputation: 11873

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.

Upvotes: 2

Michael Berry
Michael Berry

Reputation: 72334

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.

Upvotes: 5

Related Questions