Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/com/sun/tools/classfile/Dependencies.java
38899 views
/*1* Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package com.sun.tools.classfile;2526import java.util.Deque;27import java.util.HashMap;28import java.util.HashSet;29import java.util.LinkedList;30import java.util.List;31import java.util.Map;32import java.util.Set;33import java.util.regex.Pattern;3435import com.sun.tools.classfile.Dependency.Filter;36import com.sun.tools.classfile.Dependency.Finder;37import com.sun.tools.classfile.Dependency.Location;38import com.sun.tools.classfile.Type.ArrayType;39import com.sun.tools.classfile.Type.ClassSigType;40import com.sun.tools.classfile.Type.ClassType;41import com.sun.tools.classfile.Type.MethodType;42import com.sun.tools.classfile.Type.SimpleType;43import com.sun.tools.classfile.Type.TypeParamType;44import com.sun.tools.classfile.Type.WildcardType;45import static com.sun.tools.classfile.ConstantPool.*;4647/**48* A framework for determining {@link Dependency dependencies} between class files.49*50* A {@link Dependency.Finder finder} is used to identify the dependencies of51* individual classes. Some finders may return subtypes of {@code Dependency} to52* further characterize the type of dependency, such as a dependency on a53* method within a class.54*55* A {@link Dependency.Filter filter} may be used to restrict the set of56* dependencies found by a finder.57*58* Dependencies that are found may be passed to a {@link Dependencies.Recorder59* recorder} so that the dependencies can be stored in a custom data structure.60*/61public class Dependencies {62/**63* Thrown when a class file cannot be found.64*/65public static class ClassFileNotFoundException extends Exception {66private static final long serialVersionUID = 3632265927794475048L;6768public ClassFileNotFoundException(String className) {69super(className);70this.className = className;71}7273public ClassFileNotFoundException(String className, Throwable cause) {74this(className);75initCause(cause);76}7778public final String className;79}8081/**82* Thrown when an exception is found processing a class file.83*/84public static class ClassFileError extends Error {85private static final long serialVersionUID = 4111110813961313203L;8687public ClassFileError(Throwable cause) {88initCause(cause);89}90}9192/**93* Service provider interface to locate and read class files.94*/95public interface ClassFileReader {96/**97* Get the ClassFile object for a specified class.98* @param className the name of the class to be returned.99* @return the ClassFile for the given class100* @throws Dependencies.ClassFileNotFoundException if the classfile cannot be101* found102*/103public ClassFile getClassFile(String className)104throws ClassFileNotFoundException;105}106107/**108* Service provide interface to handle results.109*/110public interface Recorder {111/**112* Record a dependency that has been found.113* @param d114*/115public void addDependency(Dependency d);116}117118/**119* Get the default finder used to locate the dependencies for a class.120* @return the default finder121*/122public static Finder getDefaultFinder() {123return new APIDependencyFinder(AccessFlags.ACC_PRIVATE);124}125126/**127* Get a finder used to locate the API dependencies for a class.128* These include the superclass, superinterfaces, and classes referenced in129* the declarations of fields and methods. The fields and methods that130* are checked can be limited according to a specified access.131* The access parameter must be one of {@link AccessFlags#ACC_PUBLIC ACC_PUBLIC},132* {@link AccessFlags#ACC_PRIVATE ACC_PRIVATE},133* {@link AccessFlags#ACC_PROTECTED ACC_PROTECTED}, or 0 for134* package private access. Members with greater than or equal accessibility135* to that specified will be searched for dependencies.136* @param access the access of members to be checked137* @return an API finder138*/139public static Finder getAPIFinder(int access) {140return new APIDependencyFinder(access);141}142143/**144* Get a finder to do class dependency analysis.145*146* @return a Class dependency finder147*/148public static Finder getClassDependencyFinder() {149return new ClassDependencyFinder();150}151152/**153* Get the finder used to locate the dependencies for a class.154* @return the finder155*/156public Finder getFinder() {157if (finder == null)158finder = getDefaultFinder();159return finder;160}161162/**163* Set the finder used to locate the dependencies for a class.164* @param f the finder165*/166public void setFinder(Finder f) {167f.getClass(); // null check168finder = f;169}170171/**172* Get the default filter used to determine included when searching173* the transitive closure of all the dependencies.174* Unless overridden, the default filter accepts all dependencies.175* @return the default filter.176*/177public static Filter getDefaultFilter() {178return DefaultFilter.instance();179}180181/**182* Get a filter which uses a regular expression on the target's class name183* to determine if a dependency is of interest.184* @param pattern the pattern used to match the target's class name185* @return a filter for matching the target class name with a regular expression186*/187public static Filter getRegexFilter(Pattern pattern) {188return new TargetRegexFilter(pattern);189}190191/**192* Get a filter which checks the package of a target's class name193* to determine if a dependency is of interest. The filter checks if the194* package of the target's class matches any of a set of given package195* names. The match may optionally match subpackages of the given names as well.196* @param packageNames the package names used to match the target's class name197* @param matchSubpackages whether or not to match subpackages as well198* @return a filter for checking the target package name against a list of package names199*/200public static Filter getPackageFilter(Set<String> packageNames, boolean matchSubpackages) {201return new TargetPackageFilter(packageNames, matchSubpackages);202}203204/**205* Get the filter used to determine the dependencies included when searching206* the transitive closure of all the dependencies.207* Unless overridden, the default filter accepts all dependencies.208* @return the filter209*/210public Filter getFilter() {211if (filter == null)212filter = getDefaultFilter();213return filter;214}215216/**217* Set the filter used to determine the dependencies included when searching218* the transitive closure of all the dependencies.219* @param f the filter220*/221public void setFilter(Filter f) {222f.getClass(); // null check223filter = f;224}225226/**227* Find the dependencies of a class, using the current228* {@link Dependencies#getFinder finder} and229* {@link Dependencies#getFilter filter}.230* The search may optionally include the transitive closure of all the231* filtered dependencies, by also searching in the classes named in those232* dependencies.233* @param classFinder a finder to locate class files234* @param rootClassNames the names of the root classes from which to begin235* searching236* @param transitiveClosure whether or not to also search those classes237* named in any filtered dependencies that are found.238* @return the set of dependencies that were found239* @throws ClassFileNotFoundException if a required class file cannot be found240* @throws ClassFileError if an error occurs while processing a class file,241* such as an error in the internal class file structure.242*/243public Set<Dependency> findAllDependencies(244ClassFileReader classFinder, Set<String> rootClassNames,245boolean transitiveClosure)246throws ClassFileNotFoundException {247final Set<Dependency> results = new HashSet<Dependency>();248Recorder r = new Recorder() {249public void addDependency(Dependency d) {250results.add(d);251}252};253findAllDependencies(classFinder, rootClassNames, transitiveClosure, r);254return results;255}256257/**258* Find the dependencies of a class, using the current259* {@link Dependencies#getFinder finder} and260* {@link Dependencies#getFilter filter}.261* The search may optionally include the transitive closure of all the262* filtered dependencies, by also searching in the classes named in those263* dependencies.264* @param classFinder a finder to locate class files265* @param rootClassNames the names of the root classes from which to begin266* searching267* @param transitiveClosure whether or not to also search those classes268* named in any filtered dependencies that are found.269* @param recorder a recorder for handling the results270* @throws ClassFileNotFoundException if a required class file cannot be found271* @throws ClassFileError if an error occurs while processing a class file,272* such as an error in the internal class file structure.273*/274public void findAllDependencies(275ClassFileReader classFinder, Set<String> rootClassNames,276boolean transitiveClosure, Recorder recorder)277throws ClassFileNotFoundException {278Set<String> doneClasses = new HashSet<String>();279280getFinder(); // ensure initialized281getFilter(); // ensure initialized282283// Work queue of names of classfiles to be searched.284// Entries will be unique, and for classes that do not yet have285// dependencies in the results map.286Deque<String> deque = new LinkedList<String>(rootClassNames);287288String className;289while ((className = deque.poll()) != null) {290assert (!doneClasses.contains(className));291doneClasses.add(className);292293ClassFile cf = classFinder.getClassFile(className);294295// The following code just applies the filter to the dependencies296// followed for the transitive closure.297for (Dependency d: finder.findDependencies(cf)) {298recorder.addDependency(d);299if (transitiveClosure && filter.accepts(d)) {300String cn = d.getTarget().getClassName();301if (!doneClasses.contains(cn))302deque.add(cn);303}304}305}306}307308private Filter filter;309private Finder finder;310311/**312* A location identifying a class.313*/314static class SimpleLocation implements Location {315public SimpleLocation(String name) {316this.name = name;317this.className = name.replace('/', '.');318}319320public String getName() {321return name;322}323324public String getClassName() {325return className;326}327328public String getPackageName() {329int i = name.lastIndexOf('/');330return (i > 0) ? name.substring(0, i).replace('/', '.') : "";331}332333@Override334public boolean equals(Object other) {335if (this == other)336return true;337if (!(other instanceof SimpleLocation))338return false;339return (name.equals(((SimpleLocation) other).name));340}341342@Override343public int hashCode() {344return name.hashCode();345}346347@Override348public String toString() {349return name;350}351352private String name;353private String className;354}355356/**357* A dependency of one class on another.358*/359static class SimpleDependency implements Dependency {360public SimpleDependency(Location origin, Location target) {361this.origin = origin;362this.target = target;363}364365public Location getOrigin() {366return origin;367}368369public Location getTarget() {370return target;371}372373@Override374public boolean equals(Object other) {375if (this == other)376return true;377if (!(other instanceof SimpleDependency))378return false;379SimpleDependency o = (SimpleDependency) other;380return (origin.equals(o.origin) && target.equals(o.target));381}382383@Override384public int hashCode() {385return origin.hashCode() * 31 + target.hashCode();386}387388@Override389public String toString() {390return origin + ":" + target;391}392393private Location origin;394private Location target;395}396397398/**399* This class accepts all dependencies.400*/401static class DefaultFilter implements Filter {402private static DefaultFilter instance;403404static DefaultFilter instance() {405if (instance == null)406instance = new DefaultFilter();407return instance;408}409410public boolean accepts(Dependency dependency) {411return true;412}413}414415/**416* This class accepts those dependencies whose target's class name matches a417* regular expression.418*/419static class TargetRegexFilter implements Filter {420TargetRegexFilter(Pattern pattern) {421this.pattern = pattern;422}423424public boolean accepts(Dependency dependency) {425return pattern.matcher(dependency.getTarget().getClassName()).matches();426}427428private final Pattern pattern;429}430431/**432* This class accepts those dependencies whose class name is in a given433* package.434*/435static class TargetPackageFilter implements Filter {436TargetPackageFilter(Set<String> packageNames, boolean matchSubpackages) {437for (String pn: packageNames) {438if (pn.length() == 0) // implies null check as well439throw new IllegalArgumentException();440}441this.packageNames = packageNames;442this.matchSubpackages = matchSubpackages;443}444445public boolean accepts(Dependency dependency) {446String pn = dependency.getTarget().getPackageName();447if (packageNames.contains(pn))448return true;449450if (matchSubpackages) {451for (String n: packageNames) {452if (pn.startsWith(n + "."))453return true;454}455}456457return false;458}459460private final Set<String> packageNames;461private final boolean matchSubpackages;462}463464/**465* This class identifies class names directly or indirectly in the constant pool.466*/467static class ClassDependencyFinder extends BasicDependencyFinder {468public Iterable<? extends Dependency> findDependencies(ClassFile classfile) {469Visitor v = new Visitor(classfile);470for (CPInfo cpInfo: classfile.constant_pool.entries()) {471v.scan(cpInfo);472}473try {474v.addClass(classfile.super_class);475v.addClasses(classfile.interfaces);476v.scan(classfile.attributes);477478for (Field f : classfile.fields) {479v.scan(f.descriptor, f.attributes);480}481for (Method m : classfile.methods) {482v.scan(m.descriptor, m.attributes);483Exceptions_attribute e =484(Exceptions_attribute)m.attributes.get(Attribute.Exceptions);485if (e != null) {486v.addClasses(e.exception_index_table);487}488}489} catch (ConstantPoolException e) {490throw new ClassFileError(e);491}492493return v.deps;494}495}496497/**498* This class identifies class names in the signatures of classes, fields,499* and methods in a class.500*/501static class APIDependencyFinder extends BasicDependencyFinder {502APIDependencyFinder(int access) {503switch (access) {504case AccessFlags.ACC_PUBLIC:505case AccessFlags.ACC_PROTECTED:506case AccessFlags.ACC_PRIVATE:507case 0:508showAccess = access;509break;510default:511throw new IllegalArgumentException("invalid access 0x"512+ Integer.toHexString(access));513}514}515516public Iterable<? extends Dependency> findDependencies(ClassFile classfile) {517try {518Visitor v = new Visitor(classfile);519v.addClass(classfile.super_class);520v.addClasses(classfile.interfaces);521// inner classes?522for (Field f : classfile.fields) {523if (checkAccess(f.access_flags))524v.scan(f.descriptor, f.attributes);525}526for (Method m : classfile.methods) {527if (checkAccess(m.access_flags)) {528v.scan(m.descriptor, m.attributes);529Exceptions_attribute e =530(Exceptions_attribute) m.attributes.get(Attribute.Exceptions);531if (e != null)532v.addClasses(e.exception_index_table);533}534}535return v.deps;536} catch (ConstantPoolException e) {537throw new ClassFileError(e);538}539}540541boolean checkAccess(AccessFlags flags) {542// code copied from javap.Options.checkAccess543boolean isPublic = flags.is(AccessFlags.ACC_PUBLIC);544boolean isProtected = flags.is(AccessFlags.ACC_PROTECTED);545boolean isPrivate = flags.is(AccessFlags.ACC_PRIVATE);546boolean isPackage = !(isPublic || isProtected || isPrivate);547548if ((showAccess == AccessFlags.ACC_PUBLIC) && (isProtected || isPrivate || isPackage))549return false;550else if ((showAccess == AccessFlags.ACC_PROTECTED) && (isPrivate || isPackage))551return false;552else if ((showAccess == 0) && (isPrivate))553return false;554else555return true;556}557558private int showAccess;559}560561static abstract class BasicDependencyFinder implements Finder {562private Map<String,Location> locations = new HashMap<String,Location>();563564Location getLocation(String className) {565Location l = locations.get(className);566if (l == null)567locations.put(className, l = new SimpleLocation(className));568return l;569}570571class Visitor implements ConstantPool.Visitor<Void,Void>, Type.Visitor<Void, Void> {572private ConstantPool constant_pool;573private Location origin;574Set<Dependency> deps;575576Visitor(ClassFile classFile) {577try {578constant_pool = classFile.constant_pool;579origin = getLocation(classFile.getName());580deps = new HashSet<Dependency>();581} catch (ConstantPoolException e) {582throw new ClassFileError(e);583}584}585586void scan(Descriptor d, Attributes attrs) {587try {588scan(new Signature(d.index).getType(constant_pool));589scan(attrs);590} catch (ConstantPoolException e) {591throw new ClassFileError(e);592}593}594595void scan(CPInfo cpInfo) {596cpInfo.accept(this, null);597}598599void scan(Type t) {600t.accept(this, null);601}602603void scan(Attributes attrs) {604try {605Signature_attribute sa = (Signature_attribute)attrs.get(Attribute.Signature);606if (sa != null)607scan(sa.getParsedSignature().getType(constant_pool));608609scan((RuntimeVisibleAnnotations_attribute)610attrs.get(Attribute.RuntimeVisibleAnnotations));611scan((RuntimeVisibleParameterAnnotations_attribute)612attrs.get(Attribute.RuntimeVisibleParameterAnnotations));613} catch (ConstantPoolException e) {614throw new ClassFileError(e);615}616}617618private void scan(RuntimeAnnotations_attribute attr) throws ConstantPoolException {619if (attr == null) {620return;621}622for (int i = 0; i < attr.annotations.length; i++) {623int index = attr.annotations[i].type_index;624scan(new Signature(index).getType(constant_pool));625}626}627628private void scan(RuntimeParameterAnnotations_attribute attr) throws ConstantPoolException {629if (attr == null) {630return;631}632for (int param = 0; param < attr.parameter_annotations.length; param++) {633for (int i = 0; i < attr.parameter_annotations[param].length; i++) {634int index = attr.parameter_annotations[param][i].type_index;635scan(new Signature(index).getType(constant_pool));636}637}638}639640void addClass(int index) throws ConstantPoolException {641if (index != 0) {642String name = constant_pool.getClassInfo(index).getBaseName();643if (name != null)644addDependency(name);645}646}647648void addClasses(int[] indices) throws ConstantPoolException {649for (int i: indices)650addClass(i);651}652653private void addDependency(String name) {654deps.add(new SimpleDependency(origin, getLocation(name)));655}656657// ConstantPool.Visitor methods658659public Void visitClass(CONSTANT_Class_info info, Void p) {660try {661if (info.getName().startsWith("["))662new Signature(info.name_index).getType(constant_pool).accept(this, null);663else664addDependency(info.getBaseName());665return null;666} catch (ConstantPoolException e) {667throw new ClassFileError(e);668}669}670671public Void visitDouble(CONSTANT_Double_info info, Void p) {672return null;673}674675public Void visitFieldref(CONSTANT_Fieldref_info info, Void p) {676return visitRef(info, p);677}678679public Void visitFloat(CONSTANT_Float_info info, Void p) {680return null;681}682683public Void visitInteger(CONSTANT_Integer_info info, Void p) {684return null;685}686687public Void visitInterfaceMethodref(CONSTANT_InterfaceMethodref_info info, Void p) {688return visitRef(info, p);689}690691public Void visitInvokeDynamic(CONSTANT_InvokeDynamic_info info, Void p) {692return null;693}694695public Void visitLong(CONSTANT_Long_info info, Void p) {696return null;697}698699public Void visitMethodHandle(CONSTANT_MethodHandle_info info, Void p) {700return null;701}702703public Void visitMethodType(CONSTANT_MethodType_info info, Void p) {704return null;705}706707public Void visitMethodref(CONSTANT_Methodref_info info, Void p) {708return visitRef(info, p);709}710711public Void visitNameAndType(CONSTANT_NameAndType_info info, Void p) {712try {713new Signature(info.type_index).getType(constant_pool).accept(this, null);714return null;715} catch (ConstantPoolException e) {716throw new ClassFileError(e);717}718}719720public Void visitString(CONSTANT_String_info info, Void p) {721return null;722}723724public Void visitUtf8(CONSTANT_Utf8_info info, Void p) {725return null;726}727728private Void visitRef(CPRefInfo info, Void p) {729try {730visitClass(info.getClassInfo(), p);731return null;732} catch (ConstantPoolException e) {733throw new ClassFileError(e);734}735}736737// Type.Visitor methods738739private void findDependencies(Type t) {740if (t != null)741t.accept(this, null);742}743744private void findDependencies(List<? extends Type> ts) {745if (ts != null) {746for (Type t: ts)747t.accept(this, null);748}749}750751public Void visitSimpleType(SimpleType type, Void p) {752return null;753}754755public Void visitArrayType(ArrayType type, Void p) {756findDependencies(type.elemType);757return null;758}759760public Void visitMethodType(MethodType type, Void p) {761findDependencies(type.paramTypes);762findDependencies(type.returnType);763findDependencies(type.throwsTypes);764findDependencies(type.typeParamTypes);765return null;766}767768public Void visitClassSigType(ClassSigType type, Void p) {769findDependencies(type.superclassType);770findDependencies(type.superinterfaceTypes);771return null;772}773774public Void visitClassType(ClassType type, Void p) {775findDependencies(type.outerType);776addDependency(type.getBinaryName());777findDependencies(type.typeArgs);778return null;779}780781public Void visitTypeParamType(TypeParamType type, Void p) {782findDependencies(type.classBound);783findDependencies(type.interfaceBounds);784return null;785}786787public Void visitWildcardType(WildcardType type, Void p) {788findDependencies(type.boundType);789return null;790}791}792}793}794795796