Skip to content

Commit b131231

Browse files
committed
Refactor ClassSignature to calculate isNonPortableRuntime upfront on instantiation
1 parent b76d8c9 commit b131231

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/main/java/de/thetaphi/forbiddenapis/ClassScanner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ String checkClassUse(Type type, String what, boolean deep, String origInternalNa
9999
if (deep && forbidNonPortableRuntime) {
100100
final String binaryClassName = type.getClassName();
101101
final ClassSignature c = lookup.lookupRelatedClass(type.getInternalName(), origInternalName);
102-
if (c != null && c.isRuntimeClass && !AsmUtils.isPortableRuntimeClass(binaryClassName)) {
102+
if (c != null && c.isNonPortableRuntime) {
103103
return String.format(Locale.ENGLISH,
104104
"Forbidden %s use: %s [non-portable or internal runtime class]",
105105
what, binaryClassName

src/main/java/de/thetaphi/forbiddenapis/ClassSignature.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
final class ClassSignature implements Constants {
3737
private ClassReader reader;
3838

39-
public final boolean isRuntimeClass;
39+
public final boolean isRuntimeClass, isNonPortableRuntime;
4040
public final Set<Method> methods;
4141
public final Set<String> fields, signaturePolymorphicMethods;
4242
public final String className, superName;
@@ -76,6 +76,7 @@ public FieldVisitor visitField(int access, String name, String desc, String sign
7676
this.methods = createSet(methods);
7777
this.fields = createSet(fields);
7878
this.signaturePolymorphicMethods = createSet(signaturePolymorphicMethods);
79+
this.isNonPortableRuntime = this.determineNonPortableRuntime();
7980
}
8081

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

117119
private static <T> Set<T> createSet(Set<? extends T> s) {
118120
return s.isEmpty() ? Collections.<T>emptySet() : Collections.<T>unmodifiableSet(s);
119121
}
122+
123+
private boolean determineNonPortableRuntime() {
124+
return isRuntimeClass && !AsmUtils.isPortableRuntimeClass(getBinaryClassName());
125+
}
120126

121127
public ClassReader getReader() {
122128
if (reader == null)
123-
throw new IllegalStateException("'" + Type.getObjectType(className).getClassName() + "' has no ClassReader, because it was already checked or is only loaded as related class.");
129+
throw new IllegalStateException("'" + getBinaryClassName() + "' has no ClassReader, because it was already checked or is only loaded as related class.");
124130
try {
125131
return reader;
126132
} finally {
127133
reader = null;
128134
}
129135
}
136+
137+
public String getBinaryClassName() {
138+
return Type.getObjectType(className).getClassName();
139+
}
130140
}

0 commit comments

Comments
 (0)