Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Library jar generation with only list of available methods and members

Tags:

java

android

Maybe someone can help me.
How can I generate a jar library similar to android.jar located in android sdk-platform. It should remove all methods implementations and replace it with throw new RuntimeException("Stub!"); like this:

AppWidgetManager() { throw new RuntimeException("Stub!"); }
public NdefMessage(NdefRecord[] records) { throw new RuntimeException("Stub!"); } 
public NdefRecord[] getRecords() { throw new RuntimeException("Stub!"); } 

All public members are also present.

like image 362
John Avatar asked Jun 13 '13 09:06

John


People also ask

How do I include all JARs in a folder classpath?

In general, to include all of the JARs in a given directory, you can use the wildcard * (not *. jar ). The wildcard only matches JARs, not class files; to get all classes in a directory, just end the classpath entry at the directory name.

What is the difference between jar and library?

A JAR serves the same function an an Assembly in the C#/. net world. It's a collection of java classes, a manifest, and optionally other resources, such as properties files. A library is a more abstract concept, in java, a library is usually packaged as a JAR (Java ARchive), or a collection of JARs.


2 Answers

If no one else gives a solution, it is a very simple case.

Simplest might be to use java's reflection, reading all classes from a jar and generate java source code.

Amd then there are libraries like ASM; byte code manipulation and also java dom source generation libraries.

like image 113
Joop Eggen Avatar answered Oct 15 '22 15:10

Joop Eggen


A simple solution with javassist (I did not test, but roughly, this should be it):

ClassPool pool = new ClassPool();
pool.appendClassPath("pathToYourJar");
CtClass clazz = pool.get("Your class");

CtMethod throwingMethod = CtMethod.make("throw new RuntimeException();",clazz);

for(CtMethod method : clazz.getDeclaredMethods()){
    method.setBody(throwingMethod,null);
}

clazz.writeFile("pathToYourNewClassDirectory");



//zip the classes in your new class directory into a jar,
// add a manifest if you need to, deploy to where you want it
like image 37
kutschkem Avatar answered Oct 15 '22 17:10

kutschkem