Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cedric05/easy-wrapper
java wrapper class around class for loaded class from different classloader
https://github.com/cedric05/easy-wrapper
classloader-hell java
Last synced: about 1 month ago
JSON representation
java wrapper class around class for loaded class from different classloader
- Host: GitHub
- URL: https://github.com/cedric05/easy-wrapper
- Owner: cedric05
- Created: 2019-10-27T11:01:04.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2019-10-27T11:49:26.000Z (about 5 years ago)
- Last Synced: 2024-10-05T23:21:46.795Z (3 months ago)
- Topics: classloader-hell, java
- Language: Java
- Homepage:
- Size: 6.84 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Wrapper
Having to load class from different classloaders and invoking methods involves is tough job.
Here we are using java's proxyInvocationHandler to loosen up things
In this example, it uses a seperate interface with having all the methods which user wants to invoke from other classloader.
Now that we have Object and Wrapper Interface, This example creates Proxy object wrapping around target class Object using java's.
when ever user wants to invoke method, Invocation Handler observes that and calls actual method. This logic has been used in lot more projects like spring. but here it is being used to load/simplify invocation from different classloaders.
# For Example
code like this
```java
try {
Class recClass = urlClassLoader.loadClass("Rectangle");
Object obj = recClass.newInstance();
Method setLengthMethod = recClass.getMethod("setLength", int.class);
Method setBreadthMethod = recClass.getMethod("setBreadth", int.class);
setLengthMethod.invoke(obj, 3);
setBreadthMethod.invoke(obj, 4);
Method getArea = recClass.getMethod("getArea");
Method getLengthMethod = recClass.getMethod("getLength");
Method getBreadthMethod = recClass.getMethod("getBreadth");
int area = (int) getArea.invoke(obj);
System.out.printf("area of rectangle with length %s bread %s is %s\n", getLengthMethod.invoke(obj),
getBreadthMethod.invoke(obj), area);
} catch (InstantiationException ex) {
System.err.println("Not able to create Instance of Class");
} catch (IllegalAccessException ex) {
System.err.println("Not able to access Class");
} catch (ClassNotFoundException ex) {
System.err.println("Not able to find Class");
} catch (IllegalArgumentException e) {
System.err.println("Illegal state, Arguments passed are of different types");
} catch (InvocationTargetException e) {
System.err.println("Not able to invoke Class");
} catch (NoSuchMethodException e) {
System.err.println("Not able find method");
e.printStackTrace();
} catch (SecurityException e) {
System.err.println("Not able to invoke method");
}```
could be **simplified** to
```java
Class appClass = urlClassLoader.loadClass("Rectangle");
Object obj = appClass.newInstance();
Wrapper wrap = wrap(obj);
wrap.setLength(3);
wrap.setBreadth(4);
wrap.getArea();
System.out.printf("area of rectangle with length %s bread %s is %s\n", wrap.getLength(), wrap.getBreadth(),
wrap.getArea());
```
with a [Wrapper Interface](src/main/java/Wrapper.java)To run
``` bash
gradle run
```