简体   繁体   中英

eclipse plugin creating classes from jar's used source code

Would it be possible to write Eclipse plugin, that:

  1. Whenever in our code we use ClassA.staticMethod1(); ( ClassA come from included external jar)

  2. Plugin creates ClassA in our project.

  3. It copies that one used method only (and all needed imports and dependent methods) from jar to newly created ClassA - Unneeded class methods aren't copied to project and are still in external jar.

  4. When jar is removed all works fine.

What is your solution to achieve this?

thanks in advance



EDIT to clarify for @Thorbjørn Ravn Andersen:

given class is in a jar:

package com.ext.jar;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Utilities {

    public static Object giveFirstThing(){
        // some random method content to show what has to be removed and what has to stay
        List list = new ArrayList();    
        Object o = doThis();
        return null;        
    }   

    public static Object giveSecondThing(){
        List list = new LinkedList();       
        Object o = doThat();
        return null;        
    }       
    private static Object doThis(){
        Map<Integer, String> myMap = new HashMap<Integer, String>();
        return null;        
    }
    private static Object doThat(String ... param){
        Set set;
        return null;        
    }       
}

This class (in sources project), that uses only part (in this case 1 method, which uses other method) of that jar's class:

package com.foo.bar;

import com.ext.jar.Utilities;

public class Runner {

    public static void main(String[] args) {

        Utilities.giveFirstThing();

    }
}

The result is: class from jar is recreated in my project, as a normal compilable class, without methods and Imports, that I didn't need ( so jar can be safety removed from project ):

package com.ext.jar;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Utilities {

    public static Object giveFirstThing(){
        // some random method content to show what has to be removed and what has to stay
        List list = new ArrayList();    
        Object o = doThis();
        return null;        
    }   

    private static Object doThis(){
        Map<Integer, String> myMap = new HashMap<Integer, String>();
        return null;        
    }

}

SUM UP:

2 (of 4 total) method where needed, so they are copied.

4 (of 6 total) imports where needed, so they are copied too.

rest of class is ATM useless, so everything else is not copied.

EDIT2: I've added bounty, as a sign that I wish to find solution to this problem, which I believe could be useful open-source project. :)

Although I am sure you probably could write an Eclipse plugin to do what you require, it sounds like a lot of work that you don't need to do.

I think the plugin you describe is essentially trying to achieve two things:

  1. Reduce the overall size of that library thus, reducing the overall size of your application.
  2. The ability to not require linking to a external library

The idea of essentially reducing an external jar library to only the core functionality you require is basically one role that obfuscation is used for. Many open-source obfuscation tools are available for Java the most popular probably being Proguard . Proguard is highly configurable and useful tool that can be used to remove shrink code, optimize and then obfusticate it.

The shrinking of current code from an obfuscation tool would fulfil the first goal that I think you are trying to achieve.

On top of that obfuscation tools would achieve much better results than the techique described by the plugin. See some results of Proguard here .

The second goal that you are trying to achieve seems a little odd. There is also nothing wrong with using external libraries. From a development point of view you want to know what your application dependencies are.

Dependencies on libraries in large projects need to be assessed (for things like risk etc.) and ultimately need to be managed. The idea of editing the library by removing the parts that you don't require might put you in danger of breaking licenses. It would also be an absolute nightmare if the original library is lost and all that existed was the created classes.

It would be much easier to state what your application depends and use a library dependency tool. Apache Ivy is a tool which does this and ultimately helps others who you collaborate with, by showing what your application depends and giving them quick an easy ways to get those libraries.

These types of tools tend to be used in larger projects where there are numerous dependencies and specific versions that must be used. For small projects you will mostly find they state that it requires only one or two libraries within the projects readme or build instructions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM