Skip to content

Commit

Permalink
Fix issue #27: forbidden-apis 1.5 fails on non-runtime annotations (e…
Browse files Browse the repository at this point in the history
….g. java.lang.Synthetic) which are not in classpath
  • Loading branch information
uschindler committed Apr 17, 2014
1 parent a244e04 commit 7f18979
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 38 deletions.
68 changes: 34 additions & 34 deletions src/main/java/de/thetaphi/forbiddenapis/Checker.java
Original file line number Diff line number Diff line change
Expand Up @@ -469,22 +469,22 @@ boolean checkDescriptor(String desc) {
return checkType(Type.getType(desc));
}

private void reportClassViolation(boolean violation) {
private void reportClassViolation(boolean violation, String where) {
if (violation) {
violations[0]++;
final StringBuilder sb = new StringBuilder(" in ").append(className);
if (source != null) {
new Formatter(sb, Locale.ENGLISH).format(" (%s, class declaration)", source).flush();
new Formatter(sb, Locale.ENGLISH).format(" (%s, %s)", source, where).flush();
} else {
sb.append(" (class declaration)");
new Formatter(sb, Locale.ENGLISH).format(" (%s)", where).flush();
}
logError(sb.toString());
}
}

@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
reportClassViolation(checkClassDefinition(superName, interfaces));
reportClassViolation(checkClassDefinition(superName, interfaces), "class declaration");
}

@Override
Expand All @@ -494,13 +494,13 @@ public void visitSource(String source, String debug) {

@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
reportClassViolation(checkDescriptor(desc));
if (visible) reportClassViolation(checkDescriptor(desc), "annotation on class declaration");
return null;
}

@Override
public AnnotationVisitor visitTypeAnnotation(int typeRef, TypePath typePath, String desc, boolean visible) {
reportClassViolation(checkDescriptor(desc));
if (visible) reportClassViolation(checkDescriptor(desc), "type annotation on class declaration");
return null;
}

Expand All @@ -510,30 +510,30 @@ public FieldVisitor visitField(final int access, final String name, final String
{
// only check signature, if field is not synthetic
if ((access & Opcodes.ACC_SYNTHETIC) == 0) {
reportFieldViolation(checkDescriptor(desc));
reportFieldViolation(checkDescriptor(desc), "field declaration");
}
}

@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
reportFieldViolation(checkDescriptor(desc));
if (visible) reportFieldViolation(checkDescriptor(desc), "annotation on field declaration");
return null;
}

@Override
public AnnotationVisitor visitTypeAnnotation(int typeRef, TypePath typePath, String desc, boolean visible) {
reportFieldViolation(checkDescriptor(desc));
if (visible) reportFieldViolation(checkDescriptor(desc), "type annotation on field declaration");
return null;
}

private void reportFieldViolation(boolean violation) {
private void reportFieldViolation(boolean violation, String where) {
if (violation) {
violations[0]++;
final StringBuilder sb = new StringBuilder(" in ").append(className);
if (source != null) {
new Formatter(sb, Locale.ENGLISH).format(" (%s, field declaration of '%s')", source, name).flush();
new Formatter(sb, Locale.ENGLISH).format(" (%s, %s of '%s')", source, where, name).flush();
} else {
new Formatter(sb, Locale.ENGLISH).format(" (field declaration of '%s')", name).flush();
new Formatter(sb, Locale.ENGLISH).format(" (%s of '%s')", where, name).flush();
}
logError(sb.toString());
}
Expand All @@ -549,7 +549,7 @@ public MethodVisitor visitMethod(final int access, final String name, final Stri
{
// only check signature, if method is not synthetic
if ((access & Opcodes.ACC_SYNTHETIC) == 0) {
reportMethodViolation(checkDescriptor(desc));
reportMethodViolation(checkDescriptor(desc), "method declaration");
}
}

Expand Down Expand Up @@ -637,72 +637,72 @@ private boolean checkConstant(Object cst) {

@Override
public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
reportMethodViolation(checkMethodAccess(owner, new Method(name, desc)));
reportMethodViolation(checkMethodAccess(owner, new Method(name, desc)), "method body");
}

@Override
public void visitFieldInsn(int opcode, String owner, String name, String desc) {
reportMethodViolation(checkFieldAccess(owner, name));
reportMethodViolation(checkFieldAccess(owner, name), "method body");
}

@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
reportMethodViolation(checkDescriptor(desc));
if (visible) reportMethodViolation(checkDescriptor(desc), "annotation on method declaration");
return null;
}

@Override
public AnnotationVisitor visitParameterAnnotation(int parameter, String desc, boolean visible) {
if (visible) reportMethodViolation(checkDescriptor(desc), "parameter annotation on method declaration");
return null;
}

@Override
public AnnotationVisitor visitTypeAnnotation(int typeRef, TypePath typePath, String desc, boolean visible) {
reportMethodViolation(checkDescriptor(desc));
if (visible) reportMethodViolation(checkDescriptor(desc), "type annotation on method declaration");
return null;
}

@Override
public AnnotationVisitor visitInsnAnnotation(int typeRef, TypePath typePath, String desc, boolean visible) {
reportMethodViolation(checkDescriptor(desc));
if (visible) reportMethodViolation(checkDescriptor(desc), "annotation in method body");
return null;
}

@Override
public AnnotationVisitor visitLocalVariableAnnotation(int typeRef, TypePath typePath, Label[] start, Label[] end, int[] index, String desc, boolean visible) {
reportMethodViolation(checkDescriptor(desc));
if (visible) reportMethodViolation(checkDescriptor(desc), "annotation in method body");
return null;
}

@Override
public AnnotationVisitor visitTryCatchAnnotation(int typeRef, TypePath typePath, String desc, boolean visible) {
reportMethodViolation(checkDescriptor(desc));
if (visible) reportMethodViolation(checkDescriptor(desc), "annotation in method body");
return null;
}

@Override
public AnnotationVisitor visitParameterAnnotation(int parameter, String desc, boolean visible) {
reportMethodViolation(checkDescriptor(desc));
return null;
}

@Override
public void visitTypeInsn(int opcode, String type) {
if (opcode == Opcodes.ANEWARRAY) {
reportMethodViolation(checkType(Type.getObjectType(type)));
reportMethodViolation(checkType(Type.getObjectType(type)), "method body");
}
}

@Override
public void visitMultiANewArrayInsn(String desc, int dims) {
reportMethodViolation(checkDescriptor(desc));
reportMethodViolation(checkDescriptor(desc), "method body");
}

@Override
public void visitLdcInsn(Object cst) {
reportMethodViolation(checkConstant(cst));
reportMethodViolation(checkConstant(cst), "method body");
}

@Override
public void visitInvokeDynamicInsn(String name, String desc, Handle bsm, Object... bsmArgs) {
reportMethodViolation(checkHandle(bsm));
reportMethodViolation(checkHandle(bsm), "method body");
for (final Object cst : bsmArgs) {
reportMethodViolation(checkConstant(cst));
reportMethodViolation(checkConstant(cst), "method body");
}
}

Expand All @@ -719,18 +719,18 @@ private String getHumanReadableMethodSignature() {
return sb.toString();
}

private void reportMethodViolation(boolean violation) {
private void reportMethodViolation(boolean violation, String where) {
if (violation) {
violations[0]++;
final StringBuilder sb = new StringBuilder(" in ").append(className);
if (source != null) {
if (lineNo >= 0) {
new Formatter(sb, Locale.ENGLISH).format(" (%s:%d)", source, lineNo).flush();
} else {
new Formatter(sb, Locale.ENGLISH).format(" (%s, method declaration of '%s')", source, getHumanReadableMethodSignature()).flush();
new Formatter(sb, Locale.ENGLISH).format(" (%s, %s of '%s')", source, where, getHumanReadableMethodSignature()).flush();
}
} else {
new Formatter(sb, Locale.ENGLISH).format(" (method declaration of '%s')", getHumanReadableMethodSignature()).flush();
new Formatter(sb, Locale.ENGLISH).format(" (%s of '%s')", where, getHumanReadableMethodSignature()).flush();
}
logError(sb.toString());
}
Expand Down
Binary file modified src/test/antunit/Java8Annotations$FooBar.class
Binary file not shown.
Binary file not shown.
Binary file modified src/test/antunit/Java8Annotations.class
Binary file not shown.
11 changes: 9 additions & 2 deletions src/test/antunit/Java8Annotations.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,20 @@ static void test(@FooBar int param1, @FooBar long param2) {
}
}

final class InnerClassWithCtorParam {
public InnerClassWithCtorParam(@FooBar X param) {
System.out.println(Java8Annotations.this);
System.out.println(param);
}
}

@Deprecated
public int testField1;

@FooBar
public int testField2;

@Retention(value=RetentionPolicy.CLASS)
@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER, ElementType.METHOD, ElementType.TYPE})
@Retention(value=RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE_USE, ElementType.PARAMETER, ElementType.METHOD, ElementType.TYPE})
static @interface FooBar {}
}
4 changes: 2 additions & 2 deletions src/test/antunit/TestAnnotations.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
<target name="testAnnotations">
<au:expectfailure expectedMessage="Check for forbidden API calls failed, see log">
<forbiddenapis failOnMissingClasses="true" classpath="${basedir}">
<fileset file="Java8Annotations.class"/>
<fileset file="Java8Annotations*.class"/>
Java8Annotations$FooBar @ Forbidden annotation
java.lang.Deprecated @ Deprecated annotation
</forbiddenapis>
</au:expectfailure>
<au:assertLogContains level="error" text="Java8Annotations$FooBar [Forbidden annotation]"/>
<au:assertLogContains level="error" text="java.lang.Deprecated [Deprecated annotation]"/>
<au:assertLogContains level="error" text=" 11 error(s)"/>
<au:assertLogContains level="error" text=" 15 error(s)"/>
</target>

</project>

0 comments on commit 7f18979

Please sign in to comment.