Git Product home page Git Product logo

easy-wrapper's Introduction

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

       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

        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

To run

gradle run

easy-wrapper's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.