Java Jar Usage scanJarPackageClass(List> classes, String packageDirName, String packageName, JarFile jar, boolean recursive)

Here you can find the source of scanJarPackageClass(List> classes, String packageDirName, String packageName, JarFile jar, boolean recursive)

Description

scan Jar Package Class

License

Apache License

Declaration

private static void scanJarPackageClass(List<Class<?>> classes, String packageDirName, String packageName,
            JarFile jar, boolean recursive) 

Method Source Code

//package com.java2s;
/**//from   ww  w .j  a v a 2s . co  m
 * 
 * Copyright 2014 The Darks ORM Project (Liu lihua)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Enumeration;

import java.util.List;

import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class Main {
    private static void scanJarPackageClass(List<Class<?>> classes, String packageDirName, String packageName,
            JarFile jar, boolean recursive) {
        Enumeration<JarEntry> entries = jar.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
            String name = entry.getName();
            if (name.charAt(0) == '/') {
                name = name.substring(1);
            }
            if (name.startsWith(packageDirName)) {
                int idx = name.lastIndexOf('/');
                if (idx != -1) {
                    packageName = name.substring(0, idx).replace('/', '.');
                }
                if ((idx != -1) || recursive) {
                    if (name.endsWith(".class") && !entry.isDirectory()) {
                        String className = name.substring(packageName.length() + 1, name.length() - 6);
                        try {
                            classes.add(Class.forName(packageName + '.' + className));
                        } catch (ClassNotFoundException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}

Related

  1. listFiles(JarFile jarFile, String path)
  2. process(Object obj)
  3. processJarFile(String f)
  4. recordFactoryDefault(String className, Attributes attributes)
  5. safeClose(JarFile jf)