To see method in .class file embedded in jar file | is it possible ?

后端 未结 4 2225
不知归路
不知归路 2021-02-14 05:04

I have a jar file containing so many *.class files. I need to SEARCH one method (i just have the method name alone with me now) in which class file.

Is it possible ?

4条回答
  •  庸人自扰
    2021-02-14 05:23

    You can use the following steps to find all class names that include the target method name in the Jar file.

    1. Get the Entries for a specified Jar File.
    2. Check each JarEntry for the names. If the name ends with '.class'. Then Populate the class name.
    3. Use the populated class name to get all methods through reflection.
    4. Compare the name and target method name. If they are equal, then print the method name and class name in the console.

    Use the above steps, we can find all the class names in the jar file with a specified target method name.

    I wrote a code and ran an example for finding class name in jar 'commons-lang-2.4.jar' with target method name 'removeCauseMethodName'.

    And the following message is displaying in console.

    Method [removeCauseMethodName] is included in Class [org.apache.commons.lang.exception.ExceptionUtils]

    From the message, we can see the class name, which includes the target method name.

    The code is as follows:

    Note: before running the code, we need to add jar 'commons-lang-2.4.jar' to the build path.

    import java.io.IOException;
    import java.lang.reflect.Method;
    import java.util.Enumeration;
    import java.util.jar.JarEntry;
    import java.util.jar.JarFile;
    
    public class SearchMetodInJarFile {
    
        private static final String CLASS_SUFFIX = ".class";
    
        public static void main(String[] args) throws IOException,
                SecurityException, ClassNotFoundException {
    
            /** target method name to be searched */
            String targetMethodClass = "removeCauseMethodName";
    
            /**
             * Specify a target method name as 'removeCauseMethodName'. Find class
             * name that includes the target method name in Jar File.
             */
            new SearchMetodInJarFile().searchMethodName(new JarFile(
                    "D:\\Develop\\workspace\\Test\\commons-lang-2.4.jar"),
                    targetMethodClass);
    
        }
    
        /**
         * Search target method name in multiple Jar files.
         */
        public void searchMethodName(JarFile[] jarFiles, String targetMethodName)
                throws SecurityException, ClassNotFoundException {
    
            for (JarFile jarFile : jarFiles) {
                searchMethodName(jarFile, targetMethodName);
            }
        }
    
        /**
         * Search target method name in one Jar file.
         */
        public void searchMethodName(JarFile jarFile, String targetMethodName)
                throws SecurityException, ClassNotFoundException {
            Enumeration entryEnum = jarFile.entries();
            while (entryEnum.hasMoreElements()) {
                doSearchMethodName(entryEnum.nextElement(), targetMethodName);
            }
        }
    
        /**
         * Check the name of JarEntry, if its name ends with '.class'. Then do the
         * following 3 steps: 1. Populate Class name. 2. Get the methods by
         * reflection. 3. Compare the target method name with the names. If the
         * methood name is equal to target method name. Then print the method name
         * and class name in console.
         */
        private void doSearchMethodName(JarEntry entry, String targetMethodName)
                throws SecurityException, ClassNotFoundException {
            String name = entry.getName();
            if (name.endsWith(CLASS_SUFFIX)) {
                /**
                 * Populate the class name
                 */
                name = name.replaceAll("/", ".")
                        .substring(0, name.lastIndexOf("."));
    
                /**
                 * Retrieve the methods via reflection.
                 */
                Method[] methods = Class.forName(name).getDeclaredMethods();
                for (Method m : methods) {
                    /**
                     * Print the message in console if the method name is expected.
                     */
                    if (targetMethodName.equals(m.getName())) {
                        System.out.println(String.format(
                                "Method [%s] is included in Class [%s]",
                                targetMethodName, name));
                        break;
                    }
                }
    
            }
        }
    }
    

    Hope this can help you some.

提交回复
热议问题