How to write a Class object back to .class file? - java

I know we can load a Object from .class file and my question is how to do it reversely.
I use Class.forName("classname") to get a Class object and how can I write this back to the .class file?
Serialization class is not the point for this issue because the loaded file may not be implement Serializable interface.
The reason why I ask this is I need convert Class object to java source text string. If anyone knows how to convert Object class to source directly, it might be great helpful.

If you want to add functions etc to a class you can use.
http://www.jboss.org/javassist
example:
clazz = fullclass name, method = "public void doxxx(){ int x =0;x++}"
private static void createMethod(String clazz,String method){
ClassPool pool = ClassPool.getDefault();
try {
Class<?> class1 = Class.forName(clazz);
class1.getProtectionDomain().getCodeSource().getLocation();
pool.insertClassPath(new ClassClassPath(class1));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
CtClass pt;
try {
pt = pool.get(clazz);
CtMethod m = CtNewMethod.make(method, pt);
pt.addMethod(m);
pt.writeFile();
pt.toClass();
} catch (NotFoundException e) {
e.printStackTrace();
} catch (CannotCompileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

Related

I am get error as ClassCastException in below code

i am new to java and i am learning deserialization and while doing so i am getting classcastexception
I did serialization at one class and deserialization at another
serialization
zipfile f = new zipfile(30,"kavin");
ArrayList<zipfile> a = new ArrayList<zipfile>(101);
a.add(f);
String file = "def.txt";
try {
FileOutputStream fi = new FileOutputStream(file);
ObjectOutputStream s = new ObjectOutputStream(fi);
s.writeObject(f);
System.out.println(f.age);
s.close();
fi.close();
} catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
deserialization
String file = "def.txt";
try {
FileInputStream fi = new FileInputStream(file);
ObjectInputStream s = new ObjectInputStream(fi);
f=(deserialization)s.readObject();
System.out.println(f.age);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
output
Exception in thread "main" java.lang.ClassCastException: demo2.zipfile cannot be cast to demo2.deserialization
at demo2.deserialization.main(deserialization.java:69)
The following must be kept in mind when dealing with Serialization:
If a parent class has implemented Serializable interface then child class doesn’t need to implement it but vice-versa is not true.
Only non-static data members are saved via Serialization process.
Static data members and transient data members are not saved via Serialization process. So, if you don’t want to save the value of a non-static data member then make it transient.
The constructor of the object is never called when an object is deserialized.
Associated objects must be implementing the Serializable interface.
Try to look thru your code and find if one of the above-mentioned cases is the problem.
For more help, refer to the source at - Serialization in Java

Java Reflection : Initialize object using interface but using String value for class name

Lets say I have the following setup
an interface
public interface TestInterface {
public void draw();
}
and two implementations
public class Square implements TestInterface {
#Override
public void draw() {
System.out.println("Square");
}
}
and
public class Circle implements TestInterface {
#Override
public void draw() {
System.out.println("Circle");
}
}
Now I can easily do
TestInterface a = new Square();
a.draw();
and I correctly get Square. Next, I wanted to try out reflection.
Class<?> clazz = null;
try {
clazz = Class.forName(className);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Constructor<?> constructor = null;
try {
constructor = clazz.getConstructor();
} catch (NoSuchMethodException | SecurityException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Object instance = null;
try {
instance = constructor.newInstance();
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Method method = null;
try {
method = instance.getClass().getMethod(methodName);
} catch (NoSuchMethodException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
method.invoke(instance);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
and for className as some.package.Square and methodName as draw I again get Square. Which is correct.
The problem is that my application has access to the interface but not the actual implementations. Hence I know which methods to invoke, but I have to specify the implemented classes as well and I dont know which package they may reside in. What if I only know the name of the class but not the package?
Is there a way where I can still use the initialization form of
TestInterface a = new <some_parameter>();
a.draw();
Is there a way to generalize it? Or is the approach using reflection that I showed above, the only way to achieve something like this? Lastly, would it make any difference if I used an abstract class instead of an interface?
You need to pass:
#param className the fully qualified name of the desired class.
When you have for example three class with the Same name but in diferent packages
--package1
----Test
--package2
----Test
Main
Test
And in Main you have:
public static void main(String[] args) throws UnsupportedEncodingException {
Class<?> clazz = null;
try {
clazz = Class.forName("Test");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
It will call the one that is level of Main. For calling the others you will need to pass the fully quallifed name.
clazz = Class.forName("package1.Test");
clazz = Class.forName("package2.Test");
What if I only know the name of the class but not the package? So you need to know in what level you want. Because as you know that different packages the classes can have same names. So which Class do you need if you have that issue.
You have to know the full name of the class. Only the name of the class is not enough to load it in the memory and to use it through reflection. However, you can determine the implementations of your interface using the reflections library.
Maybe this discussion thread can help you: How can I get a list of all the implementations of an interface programmatically in Java?

Is this incorrect use or bad practice of class loaders?

My program is designed to launch from a runnable jar file, set everything up if needs be, and then load a class in another jar file to initiate the program. This allows for self updating, restarts, etc. Well, the class loading code I have seems a bit funky to me. Below is the code I am using to do load the program. Is this incorrect use or bad practice?
try {
Preferences.userRoot().put("clientPath", Run.class.getProtectionDomain().getCodeSource().getLocation().toURI().toString()); //Original client location; helps with restarts
} catch (URISyntaxException e1) {
e1.printStackTrace();
}
try {
Preferences.userRoot().flush();
} catch (BackingStoreException e1) {
e1.printStackTrace();
}
File file = new File(path); // path of the jar we will be launching to initiate the program outside of the Run class
URL url = null;
try {
url = file.toURI().toURL(); // converts the file path to a url
} catch (MalformedURLException e) {
e.printStackTrace();
}
URL[] urls = new URL[] { url };
ClassLoader cl = new URLClassLoader(urls);
Class cls = null;
try {
cls = cl.loadClass("com.hexbit.EditorJ.Load"); // the class we are loading to initiate the program
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
cls.newInstance(); // starts the class that has been loaded and the program is on its way
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
The biggest problem you have is that when you get an Exception you pretend that logging the exception makes it ok to continue as if nothing happened.
If you aggregate the try/catch blocks, your code will be much shorter and easier to read, and it won't assume that exceptions don't really matter.
Try this example
public static Object load(String path, String className) {
try {
URL url = new File(path).toURI().toURL();
ClassLoader cl = new URLClassLoader(new URL[] { url });
return cl.loadClass(className).newInstance();
} catch (Exception e) {
throw new IllegalStateException("Unable to load "+className+" " + e);
}
}

Java Generic Method to Instantiate any Class with any Constructor using Reflection

As we know Java uses erasure, so any Generic class cannot do
T t = new T();
So I was trying out Java reflection to have a class with Static methods, to instantiate any Class with any Constructor. Here is the code.
import java.lang.reflect.*;
public class GenericNewInstance {
public static <T> T createInstance(Class<T> cObj) {
try {
return cObj.newInstance();
} catch (InstantiationException e) {
System.out.println("Instantiation Exception");
return null;
} catch (IllegalAccessException e) {
System.out.println("Illegal Access Exception");
return null;
}
}
public static <T> T createInstanceUsingRelection(Class<T> c, Object... initArgs) {
Constructor<T> cTor = null;
Class<?>[] cObjs = new Class<?>[initArgs.length];
int i = 0;
for(Object o : initArgs) {
cObjs[i++] = o.getClass();
}
try {
cTor = c.getConstructor(cObjs);
} catch (SecurityException e) {
System.out.println("security exception. Cannot get Constructor");
return null;
} catch (NoSuchMethodException e) {
System.out.println("NoSuchMethodException Cannot get constructor");
return null;
}
try {
return cTor.newInstance(initArgs);
} catch (IllegalArgumentException e) {
System.out.println("Illegal Argument Exception");
return null;
} catch (InstantiationException e) {
System.out.println("Instantiation Exception");
return null;
} catch (IllegalAccessException e) {
System.out.println("Illegal Access Exception");
return null;
} catch (InvocationTargetException e) {
System.out.println("Invocation Target Exception");
return null;
}
}
}
Example for using this.
Integer i = GenericNewInstance.createInstanceUsingRelection(Integer.class, "0");
So my questions:
Is this the right way to implement it? (or is it verbose?)
What are the typical use cases of doing this?
Can/Should we avoid using Reflection while using Generics?
Your code will fail at c.getConstructor(cObjs) since this doesn't take into account the type hierarchy. If any argument is a subtype of the constructor's declared param type, this call will not return it. You'll need quite a lot more type juggling to get it working. I advise you to take a look at the code that already solves this problem. Perhaps you can even use that library as-is, your choice. It's the implementation code for Clojure, a JVM-based dynamic language that needs exactly this stuff. The library is available from the Maven central repo.
BTW Your exception handling is redundant. Either just declare throws Exception or catch any Exception and wrap it in a RuntimeException. When something fails, the original exception is the best diagnostic.

How to load a Java class dynamically on android/dalvik?

I'm wondering if and how one can load dex or class files dynamically
in dalvik, some quick'n'dirty test function I wrote was this:
public void testLoader() {
InputStream in;
int len;
byte[] data = new byte[2048];
try {
in = context.getAssets().open("f.dex");
len = in.read(data);
in.close();
DexFile d;
Class c = defineClass("net.webvm.FooImpl", data, 0, len);
Foo foo = (Foo)c.newInstance();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
whereas the Foo interface is this
public interface Foo {
int get42();
}
and f.dex contains some dx'ed implementation of that interface:
public class FooImpl implements Foo {
public int get42() {
return 42;
}
}
The above test driver throws at defineClass() and it doesn't
work and I investigated the dalvik code and found this:
http://www.google.com/codesearch/p?hl=en#atE6BTe41-M/vm/Jni.c&q=Jni.c...
So I'm wondering if anyone can enlighten me if this is possible in
some other way or not supposed to be possible. If it is not possible,
can anyone provide reasons why this is not possible?
There's an example of DexClassLoader in the Dalvik test suite. It accesses the classloader reflectively, but if you're building against the Android SDK you can just do this:
String jarFile = "path/to/jarfile.jar";
DexClassLoader classLoader = new DexClassLoader(
jarFile, "/tmp", null, getClass().getClassLoader());
Class<?> myClass = classLoader.loadClass("MyClass");
For this to work, the jar file should contain an entry named classes.dex. You can create such a jar with the dx tool that ships with your SDK.

Categories

Resources