From 2ae93bdc0adc3259cd1bfcb03f9d2016c15ec084 Mon Sep 17 00:00:00 2001 From: Uwe Schindler Date: Thu, 24 Mar 2022 12:21:49 +0100 Subject: [PATCH 1/2] Don't enforce annotations to be available, not even runtime-visible ones --- src/main/java/de/thetaphi/forbiddenapis/ClassScanner.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/de/thetaphi/forbiddenapis/ClassScanner.java b/src/main/java/de/thetaphi/forbiddenapis/ClassScanner.java index 065c432..cfa4463 100644 --- a/src/main/java/de/thetaphi/forbiddenapis/ClassScanner.java +++ b/src/main/java/de/thetaphi/forbiddenapis/ClassScanner.java @@ -257,8 +257,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", false, type.getInternalName()); } void maybeSuppressCurrentGroup(Type annotation) { From fab7177ca5ce8be074d2daaf83b14248636fd0af Mon Sep 17 00:00:00 2001 From: Uwe Schindler Date: Thu, 24 Mar 2022 12:41:41 +0100 Subject: [PATCH 2/2] Still try to check portable runtime also with shallow (annotation) checks --- .../de/thetaphi/forbiddenapis/ClassScanner.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/thetaphi/forbiddenapis/ClassScanner.java b/src/main/java/de/thetaphi/forbiddenapis/ClassScanner.java index cfa4463..33b55f0 100644 --- a/src/main/java/de/thetaphi/forbiddenapis/ClassScanner.java +++ b/src/main/java/de/thetaphi/forbiddenapis/ClassScanner.java @@ -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 } @@ -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) { @@ -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 @@ -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; } @@ -257,7 +261,7 @@ String checkDescriptor(String desc) { String checkAnnotationDescriptor(Type type, boolean visible) { // for annotations, we don't need to look into super-classes, interfaces,... - return checkClassUse(type, "annotation", false, type.getInternalName()); + return checkClassUse(type, "annotation", true, type.getInternalName()); } void maybeSuppressCurrentGroup(Type annotation) {