Skip to content

Commit d9203a9

Browse files
authored
Don't enforce annotations to be available, not even runtime-visible ones (#194)
1 parent 109e582 commit d9203a9

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

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

+9-6
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public String getSourceFile() {
8787
return source;
8888
}
8989

90-
String checkClassUse(Type type, String what, boolean deep, String origInternalName) {
90+
String checkClassUse(Type type, String what, boolean isAnnotation, String origInternalName) {
9191
while (type.getSort() == Type.ARRAY) {
9292
type = type.getElementType(); // unwrap array
9393
}
@@ -98,7 +98,8 @@ String checkClassUse(Type type, String what, boolean deep, String origInternalNa
9898
if (violation != null) {
9999
return violation;
100100
}
101-
if (deep && forbidNonPortableRuntime) {
101+
// try best to check for non portable runtime
102+
if (forbidNonPortableRuntime) try {
102103
final String binaryClassName = type.getClassName();
103104
final ClassMetadata c = lookup.lookupRelatedClass(type.getInternalName(), origInternalName);
104105
if (c != null && c.isNonPortableRuntime) {
@@ -107,12 +108,15 @@ String checkClassUse(Type type, String what, boolean deep, String origInternalNa
107108
what, binaryClassName
108109
);
109110
}
111+
} catch (RelatedClassLoadingException e) {
112+
// only throw exception if it is not an annotation
113+
if (false == isAnnotation) throw e;
110114
}
111115
return null;
112116
}
113117

114118
String checkClassUse(String internalName, String what, String origInternalName) {
115-
return checkClassUse(Type.getObjectType(internalName), what, true, origInternalName);
119+
return checkClassUse(Type.getObjectType(internalName), what, false, origInternalName);
116120
}
117121

118122
// TODO: @FunctionalInterface from Java 8 on
@@ -209,7 +213,7 @@ String checkType(Type type) {
209213
switch (type.getSort()) {
210214
case Type.OBJECT:
211215
final String internalName = type.getInternalName();
212-
violation = checkClassUse(type, "class/interface", true, internalName);
216+
violation = checkClassUse(type, "class/interface", false, internalName);
213217
if (violation != null) {
214218
return violation;
215219
}
@@ -257,8 +261,7 @@ String checkDescriptor(String desc) {
257261

258262
String checkAnnotationDescriptor(Type type, boolean visible) {
259263
// for annotations, we don't need to look into super-classes, interfaces,...
260-
// -> we just check if its disallowed or internal runtime (only if visible)!
261-
return checkClassUse(type, "annotation", visible, type.getInternalName());
264+
return checkClassUse(type, "annotation", true, type.getInternalName());
262265
}
263266

264267
void maybeSuppressCurrentGroup(Type annotation) {

0 commit comments

Comments
 (0)