Skip to content

Commit

Permalink
Refactor ClassSignature to calculate isNonPortableRuntime upfront on …
Browse files Browse the repository at this point in the history
…instantiation
  • Loading branch information
uschindler committed Apr 15, 2021
1 parent b76d8c9 commit b131231
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/main/java/de/thetaphi/forbiddenapis/ClassScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,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);
if (c != null && c.isRuntimeClass && !AsmUtils.isPortableRuntimeClass(binaryClassName)) {
if (c != null && c.isNonPortableRuntime) {
return String.format(Locale.ENGLISH,
"Forbidden %s use: %s [non-portable or internal runtime class]",
what, binaryClassName
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/de/thetaphi/forbiddenapis/ClassSignature.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
final class ClassSignature implements Constants {
private ClassReader reader;

public final boolean isRuntimeClass;
public final boolean isRuntimeClass, isNonPortableRuntime;
public final Set<Method> methods;
public final Set<String> fields, signaturePolymorphicMethods;
public final String className, superName;
Expand Down Expand Up @@ -76,6 +76,7 @@ public FieldVisitor visitField(int access, String name, String desc, String sign
this.methods = createSet(methods);
this.fields = createSet(fields);
this.signaturePolymorphicMethods = createSet(signaturePolymorphicMethods);
this.isNonPortableRuntime = this.determineNonPortableRuntime();
}

/** Alternative ctor that can be used to build the information via reflection from an already loaded class. Useful for Java 9 Jigsaw. */
Expand Down Expand Up @@ -112,19 +113,28 @@ public ClassSignature(final Class<?> clazz, boolean isRuntimeClass) {
this.methods = createSet(methods);
this.fields = createSet(fields);
this.signaturePolymorphicMethods = createSet(signaturePolymorphicMethods);
this.isNonPortableRuntime = this.determineNonPortableRuntime();
}

private static <T> Set<T> createSet(Set<? extends T> s) {
return s.isEmpty() ? Collections.<T>emptySet() : Collections.<T>unmodifiableSet(s);
}

private boolean determineNonPortableRuntime() {
return isRuntimeClass && !AsmUtils.isPortableRuntimeClass(getBinaryClassName());
}

public ClassReader getReader() {
if (reader == null)
throw new IllegalStateException("'" + Type.getObjectType(className).getClassName() + "' has no ClassReader, because it was already checked or is only loaded as related class.");
throw new IllegalStateException("'" + getBinaryClassName() + "' has no ClassReader, because it was already checked or is only loaded as related class.");
try {
return reader;
} finally {
reader = null;
}
}

public String getBinaryClassName() {
return Type.getObjectType(className).getClassName();
}
}

0 comments on commit b131231

Please sign in to comment.