Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't enforce annotations to be available, not even runtime-visible ones #194

Merged
merged 2 commits into from
Mar 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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