0

What I want to do is extract this data in a program and list them. I want to be able to look at a class file that has no documentation and find out what is accessible. I can do this by decoding the file via the information at “https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html”. But I am hoping for a simpler way. I have Searched stackoverflow and google but with no luck

I have thought about:

`object.class.instance_methods
object.instance_variables
object.class_variables` 

etc. but I would have to invoke the class file during the execution and I not sure if I can do that. EDIT: To make things a bit clearer. I have a FILE if = New File(“jar/file.jar”); This is not in the class path of my program. I can extract a class from it as an InputStream. How do I make sense of that InputStream? Yes I know about javap, I want to do this programmatically.

cliff2310
  • 570
  • 2
  • 15
  • 28
  • 2
    Take a look at javadoc for [java.lang.Class](https://docs.oracle.com/javase/10/docs/api/index.html?java/lang/Class.html) specifically the methods `getDeclaredConstructors` and `getDeclaredMethods`. – pamcevoy Jun 13 '18 at 23:03
  • 1
    The key-term for this is **Reflection API**. Most of the methods are located in the `Class` class. Given a class `Foo` in some variable `foo` you get a `Class` instance by using `Foo.class` or `foo.getClass()`. – Zabuzard Jun 13 '18 at 23:15
  • 1
    @Zabuza I missed " Can I get all methods of a class?" in my searches. I'll go take a look at it. It looks like it may be my answer. Thanks for the pointers. – cliff2310 Jun 13 '18 at 23:57
  • 2
    *"I want to be able to **look** at a class file that has no documentation and find out what is accessible"* If by "look at" you mean yourself, not code you write, then run the [Java Class File Disassembler](https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javap.html): `javap Foo.class` – Andreas Jun 14 '18 at 00:09
  • @Zabuza No this is NOT a duplicate! I have a File Myclass.class and I want to read that file NOT a class in the programs class path. And No I do not want to disassemble the code. I want to read the public entry points and data. What I have is 'File in = new File( "path\some.class" );' How do I get this information from 'in'. – cliff2310 Jun 14 '18 at 16:12
  • Then please be more specific when asking questions. Note that a `.class` file is written in byte-code (see [Wikipedia](https://en.wikipedia.org/wiki/Java_bytecode)). It's not source code anymore. Reading byte-code is usually done using the `javap` tool (see [How do I read a Java class file (Java bytecode)](https://stackoverflow.com/questions/14146782/how-do-i-read-a-java-class-file-java-bytecode)). In that case I would still vote for **duplicate**, but to that question (and related) then. – Zabuzard Jun 14 '18 at 17:57
  • Well the answer to my question, which was not answered before. javap in a programmable way gave me the hint I needed. It was the ClassParser class that has what I need. This is a Maven thing, see the following if need help with adding Maven to Eclipse: How do I add a Maven dependency in Eclipse? Note read all the comments before you start. The code you will need to add is found at: http://mvnrepository.com/artifact/org.apache.bcel/bcel/5.2 for the maven data. I will give you the highlights of the code. The sudo-code: See next comment – cliff2310 Jun 20 '18 at 17:00
  • import org.apache.bcel.classfile. jf = new JarFile(jarName); Enumeration entries = jf.entries(); ClassParser cp = new ClassParser (is, cn); JavaClass jc = cp.parse(); org.apache.bcel.classfile.Method[] mth = jc.getMethods(); for(org.apache.bcel.classfile.Method md : mth) – cliff2310 Jun 20 '18 at 17:02

0 Answers0