• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Identifying all class and Interfaces used in the class

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how can i display all classes and interfaces used in a particular class
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at the java.lang.Class class. It tells you just about everything you might want to know about a particular class. You can get a Class object for a particular class through "Class cls = Class.forName("packageName.className");"
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see how java.lang.Class would help with the Original Poster's question. However, I am still using Java 1.4. Is there something new in later Java?
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Class object will give you information about superclass, interfaces, fields, and methods through a number of methods (getSuperclass, getInterfaces, getFields, getDeclaredFields, getDeclaredMethods, etc.) and you can find out even more by accessing the returned Class, Field, Method, etc. objects.

But it will not give you information about which classes are used inside methods - e.g. you cannot find out whether or not a String is used inside a method.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can read the class file itself. The format is well documented and it has the kinds of things you are looking for.

What's your end goal? You may find existing tools that already do the job. For example, there are several good class and package dependency analyzers.
 
prasad Venkat
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stan James:
You can read the class file itself. The format is well documented and it has the kinds of things you are looking for.

What's your end goal? You may find existing tools that already do the job. For example, there are several good class and package dependency analyzers.

 
prasad Venkat
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That true , but here i need all classes used inside a class that may
in class/method. reading class format may not solve this problem
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reading the class file (maybe via BCEL) will find all other classes "statically" used by the class. That is probably what you want.

It is nearly impossible to work out all the classes that might be used "dynamically", i.e. via reflection.
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

reading class format may not solve this problem



Find the DOC and see. When you reference another class the compiler has to put that information in your class and there is a readable list of referenced classes in there. Try it with and without BCEL to see which you like. I've never tried BCEL; maybe I need an excuse to try it.

And that's right about reflection. Class.forName(arg1).newIntance() doesn't count as referencing another class. The compiler can't know what might be in that string. Neither can anybody else until it runs.

I'm not sure if you meant you have to know which methods reference which other classes. That will be harder fer sure.
 
prasad Venkat
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am searching for solution ,where i dont want to go with BCEL
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With respect, people keep telling you how to do this and you just keep saying you don't like it. Well, tough, what you are asking for is difficult to do. However many times you ask the question, no-one will tell you a dead-easy way to do it.

Your choices are: (a) interpret the class file yourself, (b) use a third-party library (e.g. BCEL) to interpret the class file, (c) redesign your application so you don't have this requirement in the first place.
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Prasad,

Here is the solution for you. Tell me are you ok with this:


import java.io.*;
import java.lang.reflect.*;

public class ClassParser {
public static void main(String[] args) {
try {
System.out.println("Enter the Class to be parsed (e.g.:java.lang.Thread) ");
String classToBeParsed = new BufferedReader(new InputStreamReader(System.in)).readLine();
Class classToParse = Class.forName(classToBeParsed);
Class[] classes = classToParse.getClasses();
for(int i = 0 ; i < classes.length; i++ ) {
System.out.println(classes[i].getName());
}
}catch(Exception e){
e.printStackTrace();
}
}
}

 
prasad Venkat
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dinesh .., you tried using getClasses(), this will help you to know only innerclasses but not classes/methods used in side classes
 
knowledge is the difference between drudgery and strategic action -- tiny ad
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic