summaryrefslogtreecommitdiff
path: root/dev-java/java-dep-check
diff options
context:
space:
mode:
authorV3n3RiX <venerix@redcorelinux.org>2017-10-09 18:53:29 +0100
committerV3n3RiX <venerix@redcorelinux.org>2017-10-09 18:53:29 +0100
commit4f2d7949f03e1c198bc888f2d05f421d35c57e21 (patch)
treeba5f07bf3f9d22d82e54a462313f5d244036c768 /dev-java/java-dep-check
reinit the tree, so we can have metadata
Diffstat (limited to 'dev-java/java-dep-check')
-rw-r--r--dev-java/java-dep-check/Manifest7
-rw-r--r--dev-java/java-dep-check/files/Main-0.3.java335
-rw-r--r--dev-java/java-dep-check/files/Main-0.4.java436
-rw-r--r--dev-java/java-dep-check/java-dep-check-0.3-r1.ebuild31
-rw-r--r--dev-java/java-dep-check/java-dep-check-0.4.ebuild31
-rw-r--r--dev-java/java-dep-check/metadata.xml8
6 files changed, 848 insertions, 0 deletions
diff --git a/dev-java/java-dep-check/Manifest b/dev-java/java-dep-check/Manifest
new file mode 100644
index 000000000000..2193e9ca50eb
--- /dev/null
+++ b/dev-java/java-dep-check/Manifest
@@ -0,0 +1,7 @@
+AUX Main-0.3.java 11702 SHA256 c74c45307ef58b3575cdb58de83dd488d98b9229d03ff8276775d1e6e967072f SHA512 a70c0161fe72e8f4b4476cbdad5ae3074fbb7321214723a7b1b7778fd06de4eb616479de2b5d6ce9a6dab2e919d721ce2b177a515039a0b9592aa2b5e1017e88 WHIRLPOOL 523eed4b9409e96b7413abe5290278f7be22efc57cd8d432c022e0e57543fae645df5154baec734cb0383962b0a56bb2b12c6ff033e7fc1ed212f124176a8760
+AUX Main-0.4.java 14943 SHA256 757eb5cce30c518a2675028be96b19bd2cf792476040adea286c04994a1455aa SHA512 340e6912285ee5c61116cd838c2abf68c573e57053d0b923fcd3929e19253d7cf29a281b226a3624a69b358f112bdf4a554b6c23b850933d60d7a1fd059f7a1e WHIRLPOOL 8e4e78324bd2f71ad3efde5fef228e266b834fdb64762c92d35c8afdd38c1bce3d89cbe0d97ab8258358d190458837f24cbb11ef34fa01c695769fe3629a9ae8
+EBUILD java-dep-check-0.3-r1.ebuild 645 SHA256 2948254a6f3ba21f118a18f1ad0bf9fd37d5641bd3263680df297261732d95a6 SHA512 6da35a65c72ef3d61f4544d49fadac6d743d8b348130ae811b620d62b428227ad4a8cc889d78dd409291dc59a621f459c27993033d7daa2eee5f23ce4d786ebe WHIRLPOOL c9df315509e12cdfd063a73d9d2e94ed87dec8324e563434a14a2fc93fcbc03558fd22ad2aa741a3b5a1b32c75c0354bf3bab56b547a1e151d6a934f39155b1f
+EBUILD java-dep-check-0.4.ebuild 642 SHA256 661252553f7b5c2f862259a48430dba681c440409c083a48f2f3220a5d15f154 SHA512 61463bfcced95396791da6dcd12135c0724294956c74743492c4fb8a21199aa855250797e6aa6a5b34ad4a5b2e6c0bc34bd77745e4769f4aae6335821ce048e1 WHIRLPOOL fd9fbb1e863085e34e4999111ade059f60611321724280b3c0e019f199f18bb5b7c76b013be49b6f14d85649f58623edac9a3852444d2a9427939e21d900e255
+MISC ChangeLog 3691 SHA256 29e43b0aba0a7e426220811d60e225e159ad3abcfe8e62ee3507242a3ad3cb7b SHA512 c2ce4395bec863212696567df89a79fc9c4f5d5582bb57d496bea40ce94cb0da220ae40552f6b661d1714ab2c29c792cd1f3817b424c695d0d516289982a3dd1 WHIRLPOOL bf79bb45e3a16e722f55f7cfd6f5640f4bce720a6fa0768ab200b1ba39bb0525b90813d53b399e989072b14e840cf26f2e057d283e84d8ee49a304f815b99c58
+MISC ChangeLog-2015 1625 SHA256 2272c7ec29fec10ed8e7925bb133621b01bc75b21a4a27403316a8b862da4392 SHA512 1d06c346e8b41c17e44b5d3313b3d5953aac7fcebd1468fccf3095e2b9a5c1359b9c09f6648e3c72f945e24019a43121025039bc7451a65094d9d8f9fb0e374f WHIRLPOOL e3cd234f153ad929948cc2d1dd6f1f3f102d0a247fad1535db0cde92bbd4bfe6c8f74f823442d5a5346751567100693f2aa06e89f6789abbee25c97f657a0289
+MISC metadata.xml 236 SHA256 3e778d35b34b5cc1a4dd7ce105fe7d2a99b25fb6dfe24709455da94baa8a0548 SHA512 1755c2590e59060f697294c5ad125b83f2d50e05a71f7091b7de75953941914cd2db3a893bd6154a2c90fa4aff5e313e75e3e174dbba1edd5c112787da5be253 WHIRLPOOL 7967660784d0d28e9a3c3c222c5bf85b3a8b59fd84b61780e4e7cc1aa5d8bd87bee40e4c5c42e646f15d6641fe182340bcbe8be94c785f40e0ac870c7a96e5cc
diff --git a/dev-java/java-dep-check/files/Main-0.3.java b/dev-java/java-dep-check/files/Main-0.3.java
new file mode 100644
index 000000000000..e1f02a024eef
--- /dev/null
+++ b/dev-java/java-dep-check/files/Main-0.3.java
@@ -0,0 +1,335 @@
+/*
+ * Main.java The main application class.
+ *
+ * Created on May 1, 2007, 6:32 PM
+ *
+ * Copyright (C) 2007,2008 Petteri Räty <betelgeuse@gentoo.org>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package javadepchecker;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Enumeration;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
+import org.apache.commons.cli.PosixParser;
+import org.objectweb.asm.AnnotationVisitor;
+import org.objectweb.asm.ClassReader;
+import org.objectweb.asm.FieldVisitor;
+import org.objectweb.asm.Label;
+import org.objectweb.asm.MethodVisitor;
+import org.objectweb.asm.Type;
+import org.objectweb.asm.commons.EmptyVisitor;
+
+/**
+ *
+ * @author betelgeuse
+ * @author serkan
+ */
+public final class Main extends EmptyVisitor {
+
+ static private String image = "";
+ private Set<String> deps = new HashSet<String>();
+ private Set<String> current = new HashSet<String>();
+
+ /** Creates a new instance of Main */
+ public Main() {
+ }
+
+ private static Collection<String> getPackageJars(String pkg) {
+ ArrayList<String> jars = new ArrayList<String>();
+ try {
+ Process p = Runtime.getRuntime().exec("java-config -p " + pkg);
+ p.waitFor();
+ BufferedReader in;
+ in = new BufferedReader(new InputStreamReader(p.getInputStream()));
+ String output = in.readLine();
+ if (output!=null/* package somehow missing*/ && !output.trim().equals("")) {
+ for (String jar : output.split(":")) {
+ jars.add(jar);
+ }
+ }
+ } catch (InterruptedException ex) {
+ Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
+ } catch (IOException ex) {
+ Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
+ }
+ return jars;
+ }
+
+ public void processJar(JarFile jar) throws IOException {
+ for (Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements();) {
+ JarEntry entry = e.nextElement();
+ String name = entry.getName();
+ if (!entry.isDirectory() && name.endsWith(".class")) {
+ this.current.add(name);
+ InputStream stream = jar.getInputStream(entry);
+ new ClassReader(stream).accept(this, 0);
+ }
+ }
+ }
+
+ private static boolean depNeeded(String pkg, Collection<String> deps) throws IOException {
+ Collection<String> jars = getPackageJars(pkg);
+ // We have a virtual with VM provider here
+ if (jars.size() == 0) {
+ return true;
+ }
+ for (String jarName : jars) {
+ JarFile jar = new JarFile(jarName);
+ for (Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements();) {
+ String name = e.nextElement().getName();
+ if (deps.contains(name)) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ private static boolean depsFound(Collection<String> pkgs, Collection<String> deps) throws IOException {
+ boolean found = true;
+ Collection<String> jars = new ArrayList<String>();
+ String[] bootClassPathJars = System.getProperty("sun.boot.class.path").split(":");
+ // Do we need "java-config -r" here?
+ for (String jar : bootClassPathJars) {
+ File jarFile = new File(jar);
+ if (jarFile.exists()) {
+ jars.add(jar);
+ }
+ }
+ for (Iterator<String> pkg = pkgs.iterator(); pkg.hasNext();) {
+ jars.addAll(getPackageJars(pkg.next()));
+ }
+
+ if (jars.size() == 0) {
+ return false;
+ }
+ ArrayList<String> jarClasses = new ArrayList<String>();
+ for (String jarName : jars) {
+ JarFile jar = new JarFile(jarName);
+ for (Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements();) {
+ jarClasses.add(e.nextElement().getName());
+ }
+ }
+ for (String dep : deps) {
+ if (!jarClasses.contains(dep)) {
+ if (found) {
+ System.out.println("Class files not found via DEPEND in package.env");
+ }
+ System.out.println("\t" + dep);
+ found = false;
+ }
+ }
+ return found;
+ }
+
+ private static boolean checkPkg(File env) {
+ boolean needed = true;
+ boolean found = true;
+ HashSet<String> pkgs = new HashSet<String>();
+ Collection<String> deps = null;
+
+ BufferedReader in = null;
+ try {
+ Pattern dep_re = Pattern.compile("^DEPEND=\"([^\"]*)\"$");
+ Pattern cp_re = Pattern.compile("^CLASSPATH=\"([^\"]*)\"$");
+
+ String line;
+ in = new BufferedReader(new FileReader(env));
+ while ((line = in.readLine()) != null) {
+ Matcher m = dep_re.matcher(line);
+ if (m.matches()) {
+ String atoms = m.group(1);
+ for (String atom : atoms.split(":")) {
+ String pkg = atom;
+ if (atom.contains("@")) {
+ pkg = atom.split("@")[1];
+ }
+ pkgs.add(pkg);
+ }
+ continue;
+ }
+ m = cp_re.matcher(line);
+ if (m.matches()) {
+ Main classParser = new Main();
+ for (String jar : m.group(1).split(":")) {
+ if (jar.endsWith(".jar")) {
+ classParser.processJar(new JarFile(image + jar));
+ }
+ }
+ deps = classParser.getDeps();
+ }
+ }
+
+ for (String pkg : pkgs) {
+ if (!depNeeded(pkg, deps)) {
+ if (needed) {
+ System.out.println("Possibly unneeded dependencies found");
+ }
+ System.out.println("\t" + pkg);
+ needed = false;
+ }
+ }
+ found = depsFound(pkgs, deps);
+
+ } catch (IOException ex) {
+ Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
+ } finally {
+ try {
+ in.close();
+ } catch (IOException ex) {
+ Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
+ }
+ }
+ return needed && found;
+ }
+
+ /**
+ * @param args the command line arguments
+ */
+ public static void main(String[] args) throws IOException {
+ int exit = 0;
+ try {
+ CommandLineParser parser = new PosixParser();
+ Options options = new Options();
+ options.addOption("h", "help", false, "print help");
+ options.addOption("i", "image", true, "image directory");
+ options.addOption("v", "verbose", false, "print verbose output");
+ CommandLine line = parser.parse(options, args);
+ String[] files = line.getArgs();
+ if (line.hasOption("h") || files.length == 0) {
+ HelpFormatter h = new HelpFormatter();
+ h.printHelp("java-dep-check [-i <image>] <package.env>+", options);
+ } else {
+ image = line.getOptionValue("i", "");
+
+ for (String arg : files) {
+ if (line.hasOption('v')) {
+ System.out.println("Checking " + arg);
+ }
+ if (!checkPkg(new File(arg))) {
+ exit = 1;
+ }
+ }
+ }
+ } catch (ParseException ex) {
+ Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
+ }
+ System.exit(exit);
+ }
+
+ private void addDep(String dep) {
+ deps.add(dep + ".class");
+ }
+
+ private void addDep(Type dep) {
+ if (dep.getSort() == Type.ARRAY) {
+ addDep(dep.getElementType());
+ }
+ if (dep.getSort() == Type.OBJECT) {
+ addDep(dep.getInternalName());
+ }
+ }
+
+ private Collection<String> getDeps() {
+ ArrayList<String> result = new ArrayList<String>();
+ for (String s : deps) {
+ if (!current.contains(s)) {
+ result.add(s);
+ }
+ }
+ return result;
+ }
+
+ @Override
+ public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
+ if(superName != null) {
+ addDep(superName);
+ }
+ for (String iface : interfaces) {
+ addDep(iface);
+ }
+ }
+
+ @Override
+ public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
+ addDep(Type.getType(desc));
+ return null;
+ }
+
+ @Override
+ public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
+ for (Type param : Type.getArgumentTypes(desc)) {
+ addDep(param);
+ }
+
+ if (exceptions != null) {
+ for (String exception : exceptions) {
+ addDep(exception);
+ }
+ }
+ addDep(Type.getReturnType(desc));
+ return new EmptyVisitor() {
+ @Override
+ public void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index) {
+ addDep(Type.getType(desc));
+ }
+
+ @Override
+ public void visitFieldInsn(int opcode, String owner, String name, String desc) {
+ addDep(Type.getObjectType(owner));
+ addDep(Type.getType(desc));
+ }
+
+ @Override
+ public void visitMethodInsn(int opcode, String owner, String name, String desc) {
+ addDep(Type.getObjectType(owner));
+ }
+
+ @Override
+ public AnnotationVisitor visitParameterAnnotation(int parameter, String desc, boolean visible) {
+ return Main.this.visitAnnotation(desc, visible);
+ }
+ };
+ }
+
+ @Override
+ public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
+ addDep(Type.getType(desc));
+ return null;
+ }
+}
diff --git a/dev-java/java-dep-check/files/Main-0.4.java b/dev-java/java-dep-check/files/Main-0.4.java
new file mode 100644
index 000000000000..ea877e92fce8
--- /dev/null
+++ b/dev-java/java-dep-check/files/Main-0.4.java
@@ -0,0 +1,436 @@
+/**
+ * This file is part of javadepchecker
+ *
+ * Copyright (C) 2016 Gentoo Foundation
+ *
+ * javadepchecker is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package javadepchecker;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import java.util.Properties;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
+import org.apache.commons.cli.PosixParser;
+
+import org.objectweb.asm.AnnotationVisitor;
+import org.objectweb.asm.ClassReader;
+import org.objectweb.asm.ClassVisitor;
+import org.objectweb.asm.FieldVisitor;
+import org.objectweb.asm.Label;
+import org.objectweb.asm.MethodVisitor;
+import org.objectweb.asm.Opcodes;
+import org.objectweb.asm.Type;
+
+/**
+ * Main Class of javadepchecker
+ * Gentoo Java Utility to scan class files for unneeded dependencies and
+ * ophaned class files
+ *
+ * @author Petteri Räty <betelgeuse@gentoo.org>
+ * @author Serkan Kaba <serkan@gentoo.org>
+ * @author William L. Thomson Jr., <wlt@o-sinc.com>
+ */
+public final class Main extends ClassVisitor {
+
+ static private String image = "";
+ private Set<String> mDeps = new HashSet<>();
+ private Set<String> mCurrent = new HashSet<>();
+
+ /**
+ * Empty Constructor, sets ASM op code version
+ */
+ public Main() {
+ super(Opcodes.ASM5);
+ }
+
+ /**
+ * Get jar names from the Gentoo package and store in a collection
+ *
+ * @param pkg Gentoo package name
+ * @return a collection of jar names
+ */
+ private static Collection<String> getPackageJars(String pkg) {
+ ArrayList<String> jars = new ArrayList<>();
+ try {
+ Process p = Runtime.getRuntime().exec("java-config -p " + pkg);
+ p.waitFor();
+ BufferedReader in;
+ in = new BufferedReader(new InputStreamReader(p.getInputStream()));
+ String output = in.readLine();
+ if (output!=null/* package somehow missing*/ &&
+ !output.trim().isEmpty()) {
+ jars.addAll(Arrays.asList(output.split(":")));
+ }
+ } catch (InterruptedException | IOException ex) {
+ Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
+ }
+ return jars;
+ }
+
+ /**
+ * Scan jar for classes to be processed by ASM
+ *
+ * @param jar jar file to be processed
+ * @throws IOException
+ */
+ public void processJar(JarFile jar) throws IOException {
+ Collections.list(jar.entries())
+ .stream()
+ .filter((JarEntry entry) -> (!entry.isDirectory() && entry.getName().endsWith("class")))
+ .forEach((JarEntry entry) -> {
+ InputStream is = null;
+ try {
+ Main.this.mCurrent.add(entry.getName());
+ is = jar.getInputStream(entry);
+ new ClassReader(is).accept(Main.this, 0);
+ } catch (IOException ex) {
+ Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
+ } finally {
+ try {
+ if(is!=null)
+ is.close();
+ } catch (IOException ex) {
+ Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
+ }
+ }
+ });
+ }
+
+ /**
+ * Check if a dependency is needed by a given package
+ *
+ * @param pkg Gentoo package name
+ * @param deps collection of dependencies for the package
+ * @return boolean if the dependency is needed or not
+ * @throws IOException
+ */
+ private static boolean depNeeded(String pkg,
+ Collection<String> deps) throws IOException {
+ Collection<String> jars = getPackageJars(pkg);
+
+ // We have a virtual with VM provider here
+ if (jars.isEmpty()) {
+ return true;
+ }
+ for (String jarName : jars) {
+ JarFile jar = new JarFile(jarName);
+ for (Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements();) {
+ String name = e.nextElement().getName();
+ if (deps.contains(name)) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Check for orphaned class files not owned by any package in dependencies
+ *
+ * @param pkg Gentoo package name
+ * @param deps collection of dependencies for the package
+ * @return boolean if the dependency is found or not
+ * @throws IOException
+ */
+ private static boolean depsFound(Collection<String> pkgs,
+ Collection<String> deps) throws IOException {
+ boolean found = true;
+ Collection<String> jars = new ArrayList<>();
+ String[] bootClassPathJars = System.getProperty("sun.boot.class.path").split(":");
+ // Do we need "java-config -r" here?
+ for (String jar : bootClassPathJars) {
+ File jarFile = new File(jar);
+ if (jarFile.exists()) {
+ jars.add(jar);
+ }
+ }
+ pkgs.forEach((String pkg) -> {
+ jars.addAll(getPackageJars(pkg));
+ });
+
+ if (jars.isEmpty()) {
+ return false;
+ }
+ ArrayList<String> jarClasses = new ArrayList<>();
+ jars.forEach((String jarName) -> {
+ try {
+ JarFile jar = new JarFile(jarName);
+ Collections.list(jar.entries()).forEach((JarEntry entry) -> {
+ jarClasses.add(entry.getName());
+ });
+ } catch (IOException ex) {
+ Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
+ }
+ });
+ for (String dep : deps) {
+ if (!jarClasses.contains(dep)) {
+ if (found) {
+ System.out.println("Class files not found via DEPEND in package.env");
+ }
+ System.out.println("\t" + dep);
+ found = false;
+ }
+ }
+ return found;
+ }
+
+ /**
+ * Core method, this one fires off all others and is the one called from
+ * Main. Check this package for unneeded dependencies and orphaned class
+ * files
+ *
+ * @param env
+ * @return
+ */
+ private static boolean checkPkg(File env) {
+ boolean needed = true;
+ boolean found = true;
+ HashSet<String> pkgs = new HashSet<>();
+ Collection<String> deps = null;
+ InputStream is = null;
+
+ try {
+ // load package.env
+ Properties props = new Properties();
+ is = new FileInputStream(env);
+ props.load(is);
+
+ // load package deps, add to hashset if exist
+ String depend = props.getProperty("DEPEND");
+ if(depend!=null &&
+ !depend.isEmpty()) {
+ for (String atom : depend.replaceAll("\"","").split(":")) {
+ String pkg = atom;
+ if (atom.contains("@")) {
+ pkg = atom.split("@")[1];
+ }
+ pkgs.add(pkg);
+ }
+ }
+
+ // load package classpath
+ String classpath = props.getProperty("CLASSPATH");
+ if(classpath!=null &&
+ !classpath.isEmpty()) {
+ Main classParser = new Main();
+ for (String jar : classpath.replaceAll("\"","").split(":")) {
+ if (jar.endsWith(".jar")) {
+ classParser.processJar(new JarFile(image + jar));
+ }
+ }
+ deps = classParser.getDeps();
+ }
+
+ for (String pkg : pkgs) {
+ if (!depNeeded(pkg, deps)) {
+ if (needed) {
+ System.out.println("Possibly unneeded dependencies found");
+ }
+ System.out.println("\t" + pkg);
+ needed = false;
+ }
+ }
+ found = depsFound(pkgs, deps);
+
+ } catch (IOException ex) {
+ Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
+ } finally {
+ try {
+ if(is!=null)
+ is.close();
+ } catch (IOException ex) {
+ Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
+ }
+ }
+ return needed && found;
+ }
+
+ /** Main method, parse command line opts, invoke the package checker
+ * @param args the command line arguments
+ * @throws java.io.IOException
+ */
+ public static void main(String[] args) throws IOException {
+ int exit = 0;
+ try {
+ CommandLineParser parser = new PosixParser();
+ Options options = new Options();
+ options.addOption("h", "help", false, "print help");
+ options.addOption("i", "image", true, "image directory");
+ options.addOption("v", "verbose", false, "print verbose output");
+ CommandLine line = parser.parse(options, args);
+ String[] files = line.getArgs();
+ if (line.hasOption("h") || files.length == 0) {
+ HelpFormatter h = new HelpFormatter();
+ h.printHelp("java-dep-check [-i <image>] <package.env>+", options);
+ } else {
+ image = line.getOptionValue("i", "");
+
+ for (String arg : files) {
+ if (line.hasOption('v')) {
+ System.out.println("Checking " + arg);
+ }
+ if (!checkPkg(new File(arg))) {
+ exit = 1;
+ }
+ }
+ }
+ } catch (ParseException ex) {
+ Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
+ }
+ System.exit(exit);
+ }
+
+ /**
+ * Add dependency to deps hashset
+ *
+ * @param dep dependent class name
+ */
+ private void addDep(String dep) {
+ mDeps.add(dep + ".class");
+ }
+
+ /**
+ * Add dependency type to deps hashset
+ *
+ * @param dep dependent class name
+ */
+ private void addDep(Type dep) {
+ if (dep.getSort() == Type.ARRAY) {
+ addDep(dep.getElementType());
+ }
+ if (dep.getSort() == Type.OBJECT) {
+ addDep(dep.getInternalName());
+ }
+ }
+
+ /**
+ * Get deps not contained in the current hashset
+ *
+ * @return a collection of deps
+ */
+ private Collection<String> getDeps() {
+ ArrayList<String> result = new ArrayList<>();
+ mDeps.stream().filter((s) -> (!mCurrent.contains(s))).forEach((s) -> {
+ result.add(s);
+ });
+ return result;
+ }
+
+ @Override
+ public void visit(int version,
+ int access,
+ String name,
+ String signature,
+ String superName,
+ String[] interfaces) {
+ if(superName != null) {
+ addDep(superName);
+ }
+ for (String iface : interfaces) {
+ addDep(iface);
+ }
+ }
+
+ @Override
+ public FieldVisitor visitField(int access,
+ String name,
+ String desc,
+ String signature,
+ Object value) {
+ addDep(Type.getType(desc));
+ return null;
+ }
+
+ @Override
+ public MethodVisitor visitMethod(int access,
+ String name,
+ String desc,
+ String signature,
+ String[] exceptions) {
+ for (Type param : Type.getArgumentTypes(desc)) {
+ addDep(param);
+ }
+
+ if (exceptions != null) {
+ for (String exception : exceptions) {
+ addDep(exception);
+ }
+ }
+ addDep(Type.getReturnType(desc));
+ return new MethodVisitor(Opcodes.ASM5) {
+ @Override
+ public void visitLocalVariable(String name,
+ String desc,
+ String signature,
+ Label start,
+ Label end,
+ int index) {
+ addDep(Type.getType(desc));
+ }
+
+ @Override
+ public void visitFieldInsn(int opcode,
+ String owner,
+ String name,
+ String desc) {
+ addDep(Type.getObjectType(owner));
+ addDep(Type.getType(desc));
+ }
+
+ @Override
+ public void visitMethodInsn(int opcode,
+ String owner,
+ String name,
+ String desc,
+ boolean itf) {
+ addDep(Type.getObjectType(owner));
+ }
+
+ @Override
+ public AnnotationVisitor visitParameterAnnotation(int parameter,
+ String desc,
+ boolean visible) {
+ return Main.this.visitAnnotation(desc, visible);
+ }
+ };
+ }
+
+ @Override
+ public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
+ addDep(Type.getType(desc));
+ return null;
+ }
+}
diff --git a/dev-java/java-dep-check/java-dep-check-0.3-r1.ebuild b/dev-java/java-dep-check/java-dep-check-0.3-r1.ebuild
new file mode 100644
index 000000000000..54d806570f99
--- /dev/null
+++ b/dev-java/java-dep-check/java-dep-check-0.3-r1.ebuild
@@ -0,0 +1,31 @@
+# Copyright 1999-2015 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+
+EAPI=5
+
+inherit java-pkg-2 java-pkg-simple
+
+DESCRIPTION="Java Dependency checker"
+HOMEPAGE="https://wiki.gentoo.org/wiki/Project:Java"
+LICENSE="GPL-2"
+SLOT="0"
+KEYWORDS="amd64 x86"
+
+COMMON_DEP="
+ dev-java/commons-cli:1
+ dev-java/asm:3"
+RDEPEND=">=virtual/jre-1.5
+ ${COMMON_DEP}"
+DEPEND=">=virtual/jdk-1.5
+ ${COMMON_DEP}"
+
+JAVA_GENTOO_CLASSPATH="asm-3,commons-cli-1"
+
+src_unpack() {
+ cp "${FILESDIR}/Main-${PV}.java" Main.java || die
+}
+
+src_install() {
+ java-pkg-simple_src_install
+ java-pkg_dolauncher ${PN} --main javadepchecker.Main
+}
diff --git a/dev-java/java-dep-check/java-dep-check-0.4.ebuild b/dev-java/java-dep-check/java-dep-check-0.4.ebuild
new file mode 100644
index 000000000000..b9acd52392e4
--- /dev/null
+++ b/dev-java/java-dep-check/java-dep-check-0.4.ebuild
@@ -0,0 +1,31 @@
+# Copyright 2016 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+
+EAPI=5
+
+inherit java-pkg-2 java-pkg-simple
+
+DESCRIPTION="Java Dependency checker"
+HOMEPAGE="https://wiki.gentoo.org/wiki/Project:Java"
+LICENSE="GPL-2"
+SLOT="0"
+KEYWORDS="~amd64 ~x86"
+
+COMMON_DEP="
+ dev-java/commons-cli:1
+ dev-java/asm:4"
+RDEPEND=">=virtual/jre-1.8
+ ${COMMON_DEP}"
+DEPEND=">=virtual/jdk-1.8
+ ${COMMON_DEP}"
+
+JAVA_GENTOO_CLASSPATH="asm-4,commons-cli-1"
+
+src_unpack() {
+ cp "${FILESDIR}/Main-${PV}.java" Main.java || die
+}
+
+src_install() {
+ java-pkg-simple_src_install
+ java-pkg_dolauncher ${PN} --main javadepchecker.Main
+}
diff --git a/dev-java/java-dep-check/metadata.xml b/dev-java/java-dep-check/metadata.xml
new file mode 100644
index 000000000000..952a4b9b605c
--- /dev/null
+++ b/dev-java/java-dep-check/metadata.xml
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE pkgmetadata SYSTEM "http://www.gentoo.org/dtd/metadata.dtd">
+<pkgmetadata>
+ <maintainer type="project">
+ <email>java@gentoo.org</email>
+ <name>Java</name>
+ </maintainer>
+</pkgmetadata>