001/*
002 * Copyright 2010-2024 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.util.Set;
021
022import org.objectweb.asm.ClassReader;
023import org.vafer.jdependency.asm.DependenciesClassAdapter;
024
025/**
026 * internal - do not use
027 */
028public final class DependencyUtils {
029
030    private DependencyUtils() {}
031
032    /*
033    public static Set<String> getDependenciesOfJar( final InputStream pInputStream ) throws IOException {
034
035        final JarInputStream inputStream = new JarInputStream(pInputStream);
036        final NullOutputStream nullStream = new NullOutputStream();
037        final Set<String> dependencies = new HashSet<String>();
038
039        try {
040            while (true) {
041                final JarEntry entry = inputStream.getNextJarEntry();
042
043                if (entry == null) {
044                    break;
045                }
046
047                if (entry.isDirectory()) {
048                    // ignore directory entries
049                    IOUtils.copy(inputStream, nullStream);
050                    continue;
051                }
052
053                final String name = entry.getName();
054
055                if (name.endsWith(".class")) {
056                    final DependenciesClassAdapter v = new DependenciesClassAdapter();
057                    new ClassReader( inputStream ).accept( v, 0 );
058                    dependencies.addAll(v.getDependencies());
059                } else {
060                    IOUtils.copy(inputStream, nullStream);
061                }
062            }
063        } finally {
064            inputStream.close();
065        }
066
067        return dependencies;
068    }
069    */
070
071    public static Set<String> getDependenciesOfClass( final InputStream pInputStream ) throws IOException {
072        final DependenciesClassAdapter v = new DependenciesClassAdapter();
073        new ClassReader( pInputStream ).accept( v, ClassReader.EXPAND_FRAMES );
074        final Set<String> depNames = v.getDependencies();
075        return depNames;
076    }
077
078    public static Set<String> getDependenciesOfClass( final Class<?> pClass ) throws IOException {
079        final String resource = "/" + pClass.getName().replace('.', '/') + ".class";
080        return getDependenciesOfClass(pClass.getResourceAsStream(resource));
081    }
082
083}