예제 #1
0
 @Override
 public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState state) {
   if (tree.getTypeDecls().size() <= 1) {
     // package-info.java files have zero top-level declarations, everything
     // else should have exactly one.
     return Description.NO_MATCH;
   }
   if (tree.getPackageName() == null) {
     // Real code doesn't use the default package.
     return Description.NO_MATCH;
   }
   List<String> names = new ArrayList<>();
   for (Tree member : tree.getTypeDecls()) {
     if (member instanceof ClassTree) {
       ClassTree classMember = (ClassTree) member;
       switch (classMember.getKind()) {
         case CLASS:
         case INTERFACE:
         case ANNOTATION_TYPE:
         case ENUM:
           SuppressWarnings suppression =
               ASTHelpers.getAnnotation(classMember, SuppressWarnings.class);
           if (suppression != null
               && !Collections.disjoint(Arrays.asList(suppression.value()), allNames())) {
             // If any top-level classes have @SuppressWarnings("TopLevel"), ignore
             // this compilation unit. We can't rely on the normal suppression
             // mechanism because the only enclosing element is the package declaration,
             // and @SuppressWarnings can't be applied to packages.
             return Description.NO_MATCH;
           }
           names.add(classMember.getSimpleName().toString());
           break;
         default:
           break;
       }
     }
   }
   if (names.size() <= 1) {
     // this can happen with multiple type declarations if some of them are
     // empty (e.g. ";" at the top level counts as an empty type decl)
     return Description.NO_MATCH;
   }
   String message =
       String.format(
           "Expected at most one top-level class declaration, instead found: %s",
           Joiner.on(", ").join(names));
   return buildDescription(tree.getPackageName()).setMessage(message).build();
 }
예제 #2
0
 private boolean suppressWarning(Declaration declaration, String warning) {
   SuppressWarnings suppressionInfo = declaration.getAnnotation(SuppressWarnings.class);
   return suppressionInfo != null && Arrays.asList(suppressionInfo.value()).contains(warning);
 }