001/* 002 * Copyright 2010-2019 The jdependency developers. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.vafer.jdependency.utils; 017 018import java.io.IOException; 019import java.io.InputStream; 020import java.io.File; 021import java.util.HashSet; 022import java.util.Set; 023import java.util.jar.JarEntry; 024import java.util.jar.JarInputStream; 025 026import org.apache.commons.io.IOUtils; 027import org.apache.commons.io.output.NullOutputStream; 028import org.objectweb.asm.ClassReader; 029import org.vafer.jdependency.asm.DependenciesClassAdapter; 030 031/** 032 * internal - do not use 033 */ 034final public class DependencyUtils { 035 036 /* 037 public static Set<String> getDependenciesOfJar( final InputStream pInputStream ) throws IOException { 038 039 final JarInputStream inputStream = new JarInputStream(pInputStream); 040 final NullOutputStream nullStream = new NullOutputStream(); 041 final Set<String> dependencies = new HashSet<String>(); 042 043 try { 044 while (true) { 045 final JarEntry entry = inputStream.getNextJarEntry(); 046 047 if (entry == null) { 048 break; 049 } 050 051 if (entry.isDirectory()) { 052 // ignore directory entries 053 IOUtils.copy(inputStream, nullStream); 054 continue; 055 } 056 057 final String name = entry.getName(); 058 059 if (name.endsWith(".class")) { 060 final DependenciesClassAdapter v = new DependenciesClassAdapter(); 061 new ClassReader( inputStream ).accept( v, 0 ); 062 dependencies.addAll(v.getDependencies()); 063 } else { 064 IOUtils.copy(inputStream, nullStream); 065 } 066 } 067 } finally { 068 inputStream.close(); 069 } 070 071 return dependencies; 072 } 073 */ 074 075 public static Set<String> getDependenciesOfClass( final InputStream pInputStream ) throws IOException { 076 final DependenciesClassAdapter v = new DependenciesClassAdapter(); 077 new ClassReader( pInputStream ).accept( v, ClassReader.EXPAND_FRAMES ); 078 final Set<String> depNames = v.getDependencies(); 079 return depNames; 080 } 081 082 public static Set<String> getDependenciesOfClass( final Class<?> pClass ) throws IOException { 083 final String resource = "/" + pClass.getName().replace('.', '/') + ".class"; 084 return getDependenciesOfClass(pClass.getResourceAsStream(resource)); 085 } 086 087}