Skip to content

Commit

Permalink
Rename ClassSignature to ClassMetadata
Browse files Browse the repository at this point in the history
  • Loading branch information
uschindler committed Apr 16, 2021
1 parent accce12 commit fe7737d
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 38 deletions.
28 changes: 14 additions & 14 deletions src/main/java/de/thetaphi/forbiddenapis/Checker.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ public static enum Option {
final EnumSet<Option> options;

/** Classes to check: key is the binary name (dotted) */
final Map<String,ClassSignature> classesToCheck = new HashMap<>();
final Map<String,ClassMetadata> classesToCheck = new HashMap<>();
/** Cache of loaded classes: key is the binary name (dotted) */
final Map<String,ClassSignature> classpathClassCache = new HashMap<>();
final Map<String,ClassMetadata> classpathClassCache = new HashMap<>();

final Signatures forbiddenSignatures;

Expand Down Expand Up @@ -185,7 +185,7 @@ public Checker(Logger logger, ClassLoader loader, EnumSet<Option> options) {
}

/** Loads the class from Java9's module system and uses reflection to get methods and fields. */
private ClassSignature loadClassFromJigsaw(String classname) throws IOException {
private ClassMetadata loadClassFromJigsaw(String classname) throws IOException {
if (method_Class_getModule == null || method_Module_getName == null) {
return null; // not Jigsaw Module System
}
Expand All @@ -200,7 +200,7 @@ private ClassSignature loadClassFromJigsaw(String classname) throws IOException
return null; // not found
}

return new ClassSignature(clazz, AsmUtils.isRuntimeModule(moduleName));
return new ClassMetadata(clazz, AsmUtils.isRuntimeModule(moduleName));
}

private boolean isRuntimePath(URL url) throws IOException {
Expand Down Expand Up @@ -233,9 +233,9 @@ private boolean isRuntimeClass(URLConnection conn) throws IOException {

/** Reads a class (binary name) from the given {@link ClassLoader}. If not found there, falls back to the list of classes to be checked. */
@Override
public ClassSignature getClassFromClassLoader(final String clazz) throws ClassNotFoundException,IOException {
public ClassMetadata getClassFromClassLoader(final String clazz) throws ClassNotFoundException,IOException {
if (classpathClassCache.containsKey(clazz)) {
final ClassSignature c = classpathClassCache.get(clazz);
final ClassMetadata c = classpathClassCache.get(clazz);
if (c == null) {
throw new ClassNotFoundException(clazz);
}
Expand All @@ -255,7 +255,7 @@ public ClassSignature getClassFromClassLoader(final String clazz) throws ClassNo
// if class is too new for this JVM, we try to load it as Class<?> via Jigsaw
// (only if it's a runtime class):
if (isRuntimeClass) {
final ClassSignature c = loadClassFromJigsaw(clazz);
final ClassMetadata c = loadClassFromJigsaw(clazz);
if (c != null) {
classpathClassCache.put(clazz, c);
return c;
Expand All @@ -265,18 +265,18 @@ public ClassSignature getClassFromClassLoader(final String clazz) throws ClassNo
"The class file format of '%s' (loaded from location '%s') is too recent to be parsed by ASM.",
clazz, url.toExternalForm()));
}
final ClassSignature c = new ClassSignature(cr, isRuntimeClass, false);
final ClassMetadata c = new ClassMetadata(cr, isRuntimeClass, false);
classpathClassCache.put(clazz, c);
return c;
} else {
final ClassSignature c = loadClassFromJigsaw(clazz);
final ClassMetadata c = loadClassFromJigsaw(clazz);
if (c != null) {
classpathClassCache.put(clazz, c);
return c;
}
}
// try to get class from our list of classes we are checking:
final ClassSignature c = classesToCheck.get(clazz);
final ClassMetadata c = classesToCheck.get(clazz);
if (c != null) {
classpathClassCache.put(clazz, c);
return c;
Expand All @@ -288,7 +288,7 @@ public ClassSignature getClassFromClassLoader(final String clazz) throws ClassNo
}

@Override
public ClassSignature lookupRelatedClass(String internalName, String internalNameOrig) {
public ClassMetadata lookupRelatedClass(String internalName, String internalNameOrig) {
final Type type = Type.getObjectType(internalName);
if (type.getSort() != Type.OBJECT) {
return null;
Expand Down Expand Up @@ -361,7 +361,7 @@ public void addClassToCheck(final InputStream in, String name) throws IOExceptio
throw new IllegalArgumentException(String.format(Locale.ENGLISH,
"The class file format of '%s' is too recent to be parsed by ASM.", name));
}
final ClassSignature metadata = new ClassSignature(reader, false, true);
final ClassMetadata metadata = new ClassMetadata(reader, false, true);
classesToCheck.put(metadata.getBinaryClassName(), metadata);
}

Expand Down Expand Up @@ -407,7 +407,7 @@ public void addSuppressAnnotation(String annoName) {
}

/** Parses a class and checks for valid method invocations */
private int checkClass(ClassSignature c, Pattern suppressAnnotationsPattern) throws ForbiddenApiException {
private int checkClass(ClassMetadata c, Pattern suppressAnnotationsPattern) throws ForbiddenApiException {
final String className = c.getBinaryClassName();
final ClassScanner scanner = new ClassScanner(c, this, forbiddenSignatures, suppressAnnotationsPattern);
try {
Expand Down Expand Up @@ -456,7 +456,7 @@ public void run() throws ForbiddenApiException {
logger.info("Scanning classes for violations...");
int errors = 0;
final Pattern suppressAnnotationsPattern = AsmUtils.glob2Pattern(suppressAnnotations.toArray(new String[suppressAnnotations.size()]));
for (final ClassSignature c : classesToCheck.values()) {
for (final ClassMetadata c : classesToCheck.values()) {
errors += checkClass(c, suppressAnnotationsPattern);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

/** Utility class that is used to get an overview of all fields and implemented
* methods of a class. It make the signatures available as Sets. */
final class ClassSignature implements Constants {
final class ClassMetadata implements Constants {
private ClassReader reader;

public final boolean isRuntimeClass, isNonPortableRuntime, isInterface;
Expand All @@ -43,7 +43,7 @@ final class ClassSignature implements Constants {
public final String[] interfaces;

/** Builds the information from an ASM ClassReader */
public ClassSignature(final ClassReader classReader, boolean isRuntimeClass, boolean withReader) {
public ClassMetadata(final ClassReader classReader, boolean isRuntimeClass, boolean withReader) {
this.reader = withReader ? classReader : null;
this.isRuntimeClass = isRuntimeClass;
this.className = classReader.getClassName();
Expand Down Expand Up @@ -81,7 +81,7 @@ public FieldVisitor visitField(int access, String name, String desc, String sign
}

/** Alternative ctor that can be used to build the information via reflection from an already loaded class. Useful for Java 9 Jigsaw. */
public ClassSignature(final Class<?> clazz, boolean isRuntimeClass) {
public ClassMetadata(final Class<?> clazz, boolean isRuntimeClass) {
this.reader = null; // no reader available!
this.isRuntimeClass = isRuntimeClass;
this.className = Type.getType(clazz).getInternalName();
Expand Down
32 changes: 16 additions & 16 deletions src/main/java/de/thetaphi/forbiddenapis/ClassScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

public final class ClassScanner extends ClassVisitor implements Constants {
private final boolean forbidNonPortableRuntime;
final ClassSignature metadata;
final ClassMetadata metadata;
final RelatedClassLookup lookup;
final List<ForbiddenViolation> violations = new ArrayList<>();

Expand All @@ -64,7 +64,7 @@ public final class ClassScanner extends ClassVisitor implements Constants {
final BitSet suppressedGroups = new BitSet();
boolean classSuppressed = false;

public ClassScanner(ClassSignature metadata, RelatedClassLookup lookup, Signatures forbiddenSignatures, final Pattern suppressAnnotations) {
public ClassScanner(ClassMetadata metadata, RelatedClassLookup lookup, Signatures forbiddenSignatures, final Pattern suppressAnnotations) {
super(Opcodes.ASM9);
this.metadata = metadata;
this.lookup = lookup;
Expand Down Expand Up @@ -100,7 +100,7 @@ String checkClassUse(Type type, String what, boolean deep, String origInternalNa
}
if (deep && forbidNonPortableRuntime) {
final String binaryClassName = type.getClassName();
final ClassSignature c = lookup.lookupRelatedClass(type.getInternalName(), origInternalName);
final ClassMetadata c = lookup.lookupRelatedClass(type.getInternalName(), origInternalName);
if (c != null && c.isNonPortableRuntime) {
return String.format(Locale.ENGLISH,
"Forbidden %s use: %s [non-portable or internal runtime class]",
Expand All @@ -119,10 +119,10 @@ String checkClassUse(String internalName, String what, String origInternalName)
static interface AncestorVisitor {
final String STOP = new String("STOP");

String visit(ClassSignature c, String origName, boolean isInterfaceOfAncestor, boolean previousInRuntime);
String visit(ClassMetadata c, String origName, boolean isInterfaceOfAncestor, boolean previousInRuntime);
}

String visitAncestors(ClassSignature cls, AncestorVisitor visitor, boolean visitSelf, boolean visitInterfacesFirst) {
String visitAncestors(ClassMetadata cls, AncestorVisitor visitor, boolean visitSelf, boolean visitInterfacesFirst) {
if (visitSelf) {
final String result = visitor.visit(cls, cls.className, cls.isInterface, cls.isRuntimeClass);
if (result != null && result != AncestorVisitor.STOP) {
Expand All @@ -132,9 +132,9 @@ String visitAncestors(ClassSignature cls, AncestorVisitor visitor, boolean visit
return visitAncestorsRecursive(cls, cls.className, visitor, cls.isRuntimeClass, visitInterfacesFirst);
}

private String visitSuperclassRecursive(ClassSignature cls, String origName, AncestorVisitor visitor, boolean previousInRuntime, boolean visitInterfacesFirst) {
private String visitSuperclassRecursive(ClassMetadata cls, String origName, AncestorVisitor visitor, boolean previousInRuntime, boolean visitInterfacesFirst) {
if (cls.superName != null) {
final ClassSignature c = lookup.lookupRelatedClass(cls.superName, origName);
final ClassMetadata c = lookup.lookupRelatedClass(cls.superName, origName);
if (c != null) {
String result = visitor.visit(c, origName, false, previousInRuntime);
if (result != AncestorVisitor.STOP) {
Expand All @@ -151,10 +151,10 @@ private String visitSuperclassRecursive(ClassSignature cls, String origName, Anc
return null;
}

private String visitInterfacesRecursive(ClassSignature cls, String origName, AncestorVisitor visitor, boolean previousInRuntime, boolean visitInterfacesFirst) {
private String visitInterfacesRecursive(ClassMetadata cls, String origName, AncestorVisitor visitor, boolean previousInRuntime, boolean visitInterfacesFirst) {
if (cls.interfaces != null) {
for (String intf : cls.interfaces) {
final ClassSignature c = lookup.lookupRelatedClass(intf, origName);
final ClassMetadata c = lookup.lookupRelatedClass(intf, origName);
if (c == null) continue;
String result = visitor.visit(c, origName, true, previousInRuntime);
if (result != AncestorVisitor.STOP) {
Expand All @@ -171,7 +171,7 @@ private String visitInterfacesRecursive(ClassSignature cls, String origName, Anc
return null;
}

private String visitAncestorsRecursive(ClassSignature cls, String origName, AncestorVisitor visitor, boolean previousInRuntime, boolean visitInterfacesFirst) {
private String visitAncestorsRecursive(ClassMetadata cls, String origName, AncestorVisitor visitor, boolean previousInRuntime, boolean visitInterfacesFirst) {
String result;
if (visitInterfacesFirst) {
result = visitInterfacesRecursive(cls, origName, visitor, previousInRuntime, visitInterfacesFirst);
Expand All @@ -195,7 +195,7 @@ private String visitAncestorsRecursive(ClassSignature cls, String origName, Ance
// TODO: convert to lambda method with method reference
private final AncestorVisitor classRelationAncestorVisitor = new AncestorVisitor() {
@Override
public String visit(ClassSignature c, String origName, boolean isInterfaceOfAncestor, boolean previousInRuntime) {
public String visit(ClassMetadata c, String origName, boolean isInterfaceOfAncestor, boolean previousInRuntime) {
if (previousInRuntime && c.isNonPortableRuntime) {
return null; // something inside the JVM is extending internal class/interface
}
Expand All @@ -213,7 +213,7 @@ String checkType(Type type) {
if (violation != null) {
return violation;
}
final ClassSignature c = lookup.lookupRelatedClass(internalName, internalName);
final ClassMetadata c = lookup.lookupRelatedClass(internalName, internalName);
return (c == null) ? null : visitAncestors(c, classRelationAncestorVisitor, false, false);
case Type.ARRAY:
type = type.getElementType();
Expand Down Expand Up @@ -393,13 +393,13 @@ private String checkMethodAccess(String owner, final Method method) {
if (CONSTRUCTOR_METHOD_NAME.equals(method.getName())) {
return null; // don't look into superclasses or interfaces to find constructors!
}
final ClassSignature c = lookup.lookupRelatedClass(owner, owner);
final ClassMetadata c = lookup.lookupRelatedClass(owner, owner);
if (c == null) {
return null;
}
return visitAncestors(c, new AncestorVisitor() {
@Override
public String visit(ClassSignature c, String origName, boolean isInterfaceOfAncestor, boolean previousInRuntime) {
public String visit(ClassMetadata c, String origName, boolean isInterfaceOfAncestor, boolean previousInRuntime) {
final Method lookupMethod;
if (c.signaturePolymorphicMethods.contains(method.getName())) {
// convert the invoked descriptor to a signature polymorphic one for the lookup
Expand Down Expand Up @@ -439,13 +439,13 @@ private String checkFieldAccess(String owner, final String field) {
if (violation != null) {
return violation;
}
final ClassSignature c = lookup.lookupRelatedClass(owner, owner);
final ClassMetadata c = lookup.lookupRelatedClass(owner, owner);
if (c == null) {
return null;
}
return visitAncestors(c, new AncestorVisitor() {
@Override
public String visit(ClassSignature c, String origName, boolean isInterfaceOfAncestor, boolean previousInRuntime) {
public String visit(ClassMetadata c, String origName, boolean isInterfaceOfAncestor, boolean previousInRuntime) {
if (!c.fields.contains(field)) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
import java.io.IOException;

public interface RelatedClassLookup {
ClassSignature lookupRelatedClass(String internalName, String internalNameOrig);
ClassSignature getClassFromClassLoader(String clazz) throws ClassNotFoundException,IOException;
ClassMetadata lookupRelatedClass(String internalName, String internalNameOrig);
ClassMetadata getClassFromClassLoader(String clazz) throws ClassNotFoundException,IOException;
}
2 changes: 1 addition & 1 deletion src/main/java/de/thetaphi/forbiddenapis/Signatures.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ private void addSignature(final String line, final String defaultMessage, final
}
classPatterns.add(new ClassPatternRule(clazz, message));
} else {
final ClassSignature c;
final ClassMetadata c;
try {
c = lookup.getClassFromClassLoader(clazz);
} catch (ClassNotFoundException cnfe) {
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/de/thetaphi/forbiddenapis/CheckerSetupTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ public void testEmptyCtor() throws Exception {
@Test
public void testRuntimeClassSignatures() throws Exception {
String internalName = "java/lang/String";
ClassSignature cs = checker.lookupRelatedClass(internalName, internalName);
ClassMetadata cs = checker.lookupRelatedClass(internalName, internalName);
assertTrue(cs.isRuntimeClass);
assertTrue(cs.signaturePolymorphicMethods.isEmpty());
}

@Test
public void testSignaturePolymorphic() throws Exception {
String internalName = "java/lang/invoke/MethodHandle";
ClassSignature cs = checker.lookupRelatedClass(internalName, internalName);
ClassMetadata cs = checker.lookupRelatedClass(internalName, internalName);
assertTrue(cs.signaturePolymorphicMethods.contains("invoke"));
assertTrue(cs.signaturePolymorphicMethods.contains("invokeExact"));
// System.out.println(cs.signaturePolymorphicMethods);
Expand Down

0 comments on commit fe7737d

Please sign in to comment.