Skip to content

Commit

Permalink
Don't enforce annotations to be available, not even runtime-visible o…
Browse files Browse the repository at this point in the history
…nes (#194)
  • Loading branch information
uschindler authored Mar 24, 2022
1 parent 109e582 commit d9203a9
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/main/java/de/thetaphi/forbiddenapis/ClassScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public String getSourceFile() {
return source;
}

String checkClassUse(Type type, String what, boolean deep, String origInternalName) {
String checkClassUse(Type type, String what, boolean isAnnotation, String origInternalName) {
while (type.getSort() == Type.ARRAY) {
type = type.getElementType(); // unwrap array
}
Expand All @@ -98,7 +98,8 @@ String checkClassUse(Type type, String what, boolean deep, String origInternalNa
if (violation != null) {
return violation;
}
if (deep && forbidNonPortableRuntime) {
// try best to check for non portable runtime
if (forbidNonPortableRuntime) try {
final String binaryClassName = type.getClassName();
final ClassMetadata c = lookup.lookupRelatedClass(type.getInternalName(), origInternalName);
if (c != null && c.isNonPortableRuntime) {
Expand All @@ -107,12 +108,15 @@ String checkClassUse(Type type, String what, boolean deep, String origInternalNa
what, binaryClassName
);
}
} catch (RelatedClassLoadingException e) {
// only throw exception if it is not an annotation
if (false == isAnnotation) throw e;
}
return null;
}

String checkClassUse(String internalName, String what, String origInternalName) {
return checkClassUse(Type.getObjectType(internalName), what, true, origInternalName);
return checkClassUse(Type.getObjectType(internalName), what, false, origInternalName);
}

// TODO: @FunctionalInterface from Java 8 on
Expand Down Expand Up @@ -209,7 +213,7 @@ String checkType(Type type) {
switch (type.getSort()) {
case Type.OBJECT:
final String internalName = type.getInternalName();
violation = checkClassUse(type, "class/interface", true, internalName);
violation = checkClassUse(type, "class/interface", false, internalName);
if (violation != null) {
return violation;
}
Expand Down Expand Up @@ -257,8 +261,7 @@ String checkDescriptor(String desc) {

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

void maybeSuppressCurrentGroup(Type annotation) {
Expand Down

0 comments on commit d9203a9

Please sign in to comment.