Classes used in a class

  • Thread starter Gregor =?ISO-8859-2?Q?Kova=E8?=
  • Start date
G

Gregor =?ISO-8859-2?Q?Kova=E8?=

Hi!

Is it possible to get all class names that are used in a specific class?
For example you have code:
String a = "AAAA";
MyClass c = new MyClass();

and the tool would return String, MyClass

Best regards,
Kovi

--
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
| Gregor Kovac | (e-mail address removed) |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| In A World Without Fences Who Needs Gates? |
| Experience Linux. |
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
 
O

Oliver Wong

Gregor Kovac said:
Hi!

Is it possible to get all class names that are used in a specific class?
For example you have code:
String a = "AAAA";
MyClass c = new MyClass();

and the tool would return String, MyClass

Yes, it's certainly possible, because a compiler has to do this before
it can perform the type checking phase.

If your next question is "where can I download such a tool?",
unfortunately I don't know of any such tool. If you have some experience
writing parsers, writing the tool yourself should be easy enough, since you
can get a grammar for Java on various sites on the net. If not, then you'd
better start learning!

- Oliver
 
C

Chris Uppal

Gregor said:
Is it possible to get all class names that are used in a specific class?

You can get that kind of information rather easily by parsing the .class files.
There are several libraries that will do the hard work of parsing for you, so
it's just a matter of learning how to use them.

-- chris
 
I

IchBin

Oliver said:
Yes, it's certainly possible, because a compiler has to do this before
it can perform the type checking phase.

If your next question is "where can I download such a tool?",
unfortunately I don't know of any such tool. If you have some experience
writing parsers, writing the tool yourself should be easy enough, since you
can get a grammar for Java on various sites on the net. If not, then you'd
better start learning!

- Oliver

Do you meen something like this...

public class FieldTypes
{
public static void main(String[] args) {
GridBagConstraints g = new GridBagConstraints();
printFieldNames(g);
}

static void printFieldNames(Object o) {
Class c = o.getClass();
java.lang.reflect.Field[] publicFields = c.getFields();
for (int i = 0; i < publicFields.length; i++) {
String fieldName = publicFields.getName();
Class typeClass = publicFields.getType();
String fieldType = typeClass.getName();
System.out.println("Name: " + fieldName +
", Type: " + fieldType);
}
}
}

--

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
 
O

Oliver Wong

IchBin said:
Oliver said:
Yes, it's certainly possible, because a compiler has to do this
before it can perform the type checking phase.

If your next question is "where can I download such a tool?",
unfortunately I don't know of any such tool. If you have some experience
writing parsers, writing the tool yourself should be easy enough, since
you can get a grammar for Java on various sites on the net. If not, then
you'd better start learning!

Do you meen something like this...

public class FieldTypes
{
public static void main(String[] args) {
GridBagConstraints g = new GridBagConstraints();
printFieldNames(g);
}

static void printFieldNames(Object o) {
Class c = o.getClass();
java.lang.reflect.Field[] publicFields = c.getFields();
for (int i = 0; i < publicFields.length; i++) {
String fieldName = publicFields.getName();
Class typeClass = publicFields.getType();
String fieldType = typeClass.getName();
System.out.println("Name: " + fieldName +
", Type: " + fieldType);
}
}
}


This is a good start, but I don't think you can use reflection to peer
into the bodies of methods to check for references to classes there.

- Oliver
 
G

Gregor =?ISO-8859-2?Q?Kova=E8?=

Chris said:
You can get that kind of information rather easily by parsing the .class
files. There are several libraries that will do the hard work of parsing
for you, so it's just a matter of learning how to use them.

-- chris

Yes, there are. Since I'm using NetBeans all the time, I just used their
plugin.
I got a solution in just one line :)))

Best regards,
Kovi

--
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
| Gregor Kovac | (e-mail address removed) |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| In A World Without Fences Who Needs Gates? |
| Experience Linux. |
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top