415N/A/*
553N/A * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
415N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
415N/A *
415N/A * This code is free software; you can redistribute it and/or modify it
415N/A * under the terms of the GNU General Public License version 2 only, as
415N/A * published by the Free Software Foundation.
415N/A *
415N/A * This code is distributed in the hope that it will be useful, but WITHOUT
415N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
415N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
415N/A * version 2 for more details (a copy is included in the LICENSE file that
415N/A * accompanied this code).
415N/A *
415N/A * You should have received a copy of the GNU General Public License version
415N/A * 2 along with this work; if not, write to the Free Software Foundation,
415N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
415N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
415N/A */
415N/A
415N/Aimport java.io.IOException;
415N/Aimport java.io.InputStream;
415N/Aimport java.util.Enumeration;
415N/Aimport java.util.jar.JarEntry;
415N/Aimport java.util.jar.JarFile;
415N/A
415N/Aimport com.sun.tools.classfile.AccessFlags;
415N/Aimport com.sun.tools.classfile.ClassFile;
415N/Aimport com.sun.tools.classfile.ConstantPoolException;
415N/Aimport com.sun.tools.classfile.Method;
415N/Aimport java.util.Comparator;
415N/Aimport java.util.Set;
415N/Aimport java.util.TreeSet;
415N/A
415N/Apublic class FindNativeFiles {
415N/A public static void main(String[] args) throws IOException, ConstantPoolException {
415N/A new FindNativeFiles().run(args);
415N/A }
415N/A
415N/A public void run(String[] args) throws IOException, ConstantPoolException {
415N/A JarFile jar = new JarFile(args[0]);
415N/A Set<JarEntry> entries = getNativeClasses(jar);
415N/A for (JarEntry e: entries) {
415N/A String name = e.getName();
415N/A String className = name.substring(0, name.length() - 6).replace("/", ".");
415N/A System.out.println(className);
415N/A }
415N/A }
415N/A
415N/A Set<JarEntry> getNativeClasses(JarFile jar) throws IOException, ConstantPoolException {
415N/A Set<JarEntry> results = new TreeSet<JarEntry>(new Comparator<JarEntry>() {
415N/A public int compare(JarEntry o1, JarEntry o2) {
415N/A return o1.getName().compareTo(o2.getName());
415N/A }
415N/A });
415N/A Enumeration<JarEntry> e = jar.entries();
415N/A while (e.hasMoreElements()) {
415N/A JarEntry je = e.nextElement();
415N/A if (isNativeClass(jar, je))
415N/A results.add(je);
415N/A }
415N/A return results;
415N/A }
415N/A
415N/A boolean isNativeClass(JarFile jar, JarEntry entry) throws IOException, ConstantPoolException {
415N/A String name = entry.getName();
415N/A if (name.startsWith("META-INF") || !name.endsWith(".class"))
415N/A return false;
415N/A //String className = name.substring(0, name.length() - 6).replace("/", ".");
415N/A //System.err.println("check " + className);
415N/A InputStream in = jar.getInputStream(entry);
415N/A ClassFile cf = ClassFile.read(in);
415N/A in.close();
415N/A for (int i = 0; i < cf.methods.length; i++) {
415N/A Method m = cf.methods[i];
415N/A if (m.access_flags.is(AccessFlags.ACC_NATIVE)) {
415N/A // System.err.println(className);
415N/A return true;
415N/A }
415N/A }
415N/A return false;
415N/A }
415N/A}