Create a BCEL JavaClass object from arbitrary .class file - java

I'm playing around with BCEL. I'm not using it to generate bytecode, but instead I'm trying to inspect the structure of existing compiled classes.
I need to be able to point to an arbitrary .class file anywhere on my hard drive and load a JavaClass object based on that. Ideally I'd like to avoid having to add the given class to my classpath.

The straightforward way is to create a ClassParser with the file name and call parse(). Alternatively you can use SyntheticRepository and supply a classpath (that is not your classpath, IYSWIM).

The existing .class can be class loaded to java lang class object. Then it can be converted into BCEL intermediate javaclass structure. The following code may help:-
Class<?> javaClass1 = null;
javaClass1 = ucl.loadClass("com.sample.Customer");
org.apache.bcel.classfile.JavaClass javaClazz1=org.apache.bcel.Repository.lookupClass(javaClass1);

new ClassParser(classfilebytearrayhere).parse()

Related

Java, Load classes from JAR to use them later

I have a problem loading class from JAR file.
I can load them with URLClassLoader using a JarFile etc etc like in this answer; but if later in code I try to instantiate them with reflection:
Object obj = Class.forName(className).newInstance()
I get a ClassNotFoundException.
Can I dinamically load a class to use them later, when I need them, just like classes in ClassPath?
Thank you!
You need to provide class loader to Class.forName method - as otherwise it will look in this same class loader as your class is in.
Object obj = Class.forName("name", true, loader).newInstance() .
But you can't just load a class and then use it in your code like MyLoadedType - as here java does not know where to look for that class, unless you will ensure that your code and loaded code is in this same class loader - you can do this by running all your code from custom class loader that allows for adding new sources in runtime. (URLClassLoader allows for this but method is protected - so you need to extend it and make it public, in java 8 system class loader is also URLClassLoader - but this was changed in java 9) .
But you can operate on that code using reflections like I showed you.

Loading a class and checking for method presence without resolving the class

I am trying to load a class from a folder to check for the implementation of a specific method. The class has some imports that are not present in the folder or its subfolders. Loading the class with Class clazz = Class.forName(className, false, classLoader); works fine, but when I call clazz.getDeclaredMethod("methodName") then I get a NoClassDefFoundError because some imports cannot be resolved.
Is there a possibility to examine a class at runtime (I do not intend to call methods or instantiate it) without loading dependencies?
If not, how else can I check a class for a specific method implementation when I have a classes-folder as a starting point?
Interesting question.
I don't think you want to roll your own byte code parser, so try Apache BCEL or Spring ASM. Both allow you to read/write class files without having to load them.
You might be able to do that with a library such as Apache Commons BCEL.
The Byte Code Engineering Library (Apache Commons BCEL™) is intended to give users a convenient way to analyze, create, and manipulate (binary) Java class files (those ending with .class).
you could try executing `javap ' from your program and parsing the output. For example:
javap Driver
Compiled from "Driver.java"
public class Driver {
public Driver();
public static void main(java.lang.String[]);
}

Getting all classes with Javassist using a pattern

How can I get all classes using a pattern like "com.stackoverflow.*" with Javassist?
i found only 2 methods :
1/ Find a class by full name
CtClass ClassPool.getDefault().getCtClass("com.stackoverflow.user.name")
2/ Find a list of classes with fullnames :
CtClass[] ClassPool.getDefault().get(String [] arg0)
You could use some library like :
https://github.com/ronmamo/reflections
I don't think you can do that just with JRE classes.
Example from the doc :
Reflections reflections = new Reflections("my.project.prefix");
Set<Class<? extends SomeType>> subTypes =
reflections.getSubTypesOf(SomeType.class);
Set<Class<?>> annotated =
reflections.getTypesAnnotatedWith(SomeAnnotation.class);
Michael Laffargue's suggestion is the best way to go. The Reflections library uses javassist under the covers. Basically, javassist provides a means of reading raw byte code from class or jar files and extracting the class meta-data from it without actually class loading it, where as Reflections provides a richer API around locating (via classpath specifications) and filtering the set of classes you're looking for.
You can do the same thing yourself using javassist only, but you will be recreating some portion of the Reflections library. You could look at Reflections' source code to see how it works, but very generally speaking, it looks like this:
Locate the classpath you want to scan. This will usually be a group of directories with a tree of class files, or a group of Jar files, but could also include more complex structures like WARs or EARs (which Reflections supports quite nicely).
Add the root of the file system where the class files live, or the JAR file reference to your ClassPool instance.
Using a file system iteration or a JarInputStream, iterate through each class file or JarEntry. You can filter out any files or entries that do not match "com/stackoverflow/**.class"
For the remaining, using the name of the file or entry, derrive the class name and load it from the javassist class pool.
Use the loaded CtClass to apply any further search criteria.
Now you have your class reference list, release the ClassPool for garbage collection.

Importing Libraries Java

So I have two Jars I want to use in my project from here http://www.jhlabs.com/ip/filters/index.html
I added both CheckFilter and MarbleFilter to my class path. But when I do
CheckFilter();
It says I have to create a method CheckFilter()
I'm pretty sure that's the method I need to call to use that effect. But when I try any of the other methods in the library Jar it still gives me the same thing.
I have no experience with importing/using external libraries. Any help would be great.
checkFilter = new CheckFilter();
CheckFilter();
I tried above and it says I need to create a local variable checkFilter
How are you writing up the code. I will suggest to use eclipse IDE, it will make your tasks simple
If you are using eclipse. You need to do import the jar Filters.jar to your build path
which as you mentioned you downloaded from JHLabs Download page
I found Filters.jar inside dist directory.
Then you will be able to import the class or package
import com.jhlabs.image.*;
OR
import com.jhlabs.image.CheckFilter;
After importing the class or package you will be able to create object to it by
CheckFilter checkFilter = new CheckFilter();
In case you are totally new you can take help from people over IRC or chat and get going.
Someone would be able to quickly help you out
----==----==----==----==----==----==----==----==----
Read your comments and Question again.
You are totally missing the point. If you call to CheckFilter() directly without invoking new keyword, compiler will consider you are trying to access a method which is inside the class you are writing up. and give you error.
As I mentioned above. Your are trying to accessing Instance variable for the class without declaring it. Either do
CheckFilter checkFilter;
before you access checkFilter variable or directly instantiate the class the way I mentioned.
Seems to me you are missing a log of points :D
Methods don't exist without a class. That is probably the constructor to a class. Use
CheckFilter checkFilter = new CheckFilter();
instead. Then call methods on checkFilter.
From #andy-thomas in this similar question
The import statement imports classes from the jar file, not the jar file itself.
An import statement of the form:
import stdlib.*;
will import all the classes in the package stdlib.
Oracle provides this tutorial on importing.
This statement
CheckFilter();
Tries to call a method CheckFilter defined in your class, which is not the case. If this is a utility function, this may be a static method, in which case you can call it like this
ClassName.CheckFilter(); // replace ClassName with the class containing this function
If not, then you may have to instantiate an object
ClassName obj = new ClassName();
obj.CheckFilter();
or skip the object variable
new ClassName().CheckFilter(); // Not prefered
To add jar files, right click on your project in "Package Explorer", go to "Configure Build Path" and then to "Add External Jars".
Set the jar (libraries) into your classpath and use import statements in your java code to include the required Classes.

Are there any libraries for extracting class, method, member and field dependency names from a .class file (bytecode)?

As the title says, are there any libraries for extracting class, method, member and field dependency names from a .class file (bytecode)?
For example, if I a compiled Scala .class file uses something like this:
var xs = new List[java.lang.String]();
"blah" :: xs;
xs(0).charAt(0);
I should get that I use these classes and methods:
java.lang.String
java.lang.String#charAt
scala.collection.immutable.List
scala.collection.immutable.List#apply
Is there any library with API I can use in my own Scala program that can do this for me?
Here is ASM framework tutorial explaining how to collect dependencies from classes.
You need to be able to read the code of method bodies. I would use a byte code library such as ObjectWeb's ASM, BCEL or JavaAssist.

Categories

Resources