public void testAddExtends() throws Exception {
    testFile = new File(getWorkDir(), "Test.java");
    TestUtilities.copyStringToFile(
        testFile,
        "package hierbas.del.litoral;\n\n"
            + "import java.util.*;\n\n"
            + "public interface Test {\n"
            + "    public void taragui();\n"
            + "}\n");
    String golden =
        "package hierbas.del.litoral;\n\n"
            + "import java.util.*;\n\n"
            + "public interface Test extends Serializable {\n"
            + "    public void taragui();\n"
            + "}\n";
    JavaSource src = getJavaSource(testFile);

    Task task =
        new Task<WorkingCopy>() {

          public void run(WorkingCopy workingCopy) throws IOException {
            workingCopy.toPhase(Phase.RESOLVED);
            CompilationUnitTree cut = workingCopy.getCompilationUnit();
            TreeMaker make = workingCopy.getTreeMaker();

            for (Tree typeDecl : cut.getTypeDecls()) {
              // ensure that it is correct type declaration, i.e. class
              if (TreeUtilities.CLASS_TREE_KINDS.contains(typeDecl.getKind())) {
                ClassTree classTree = (ClassTree) typeDecl;
                List<ExpressionTree> implementz =
                    Collections.<ExpressionTree>singletonList(make.Identifier("Serializable"));
                ClassTree copy =
                    make.Class(
                        classTree.getModifiers(),
                        classTree.getSimpleName(),
                        classTree.getTypeParameters(),
                        classTree.getExtendsClause(),
                        implementz,
                        classTree.getMembers());
                workingCopy.rewrite(classTree, copy);
              }
            }
          }
        };
    src.runModificationTask(task).commit();
    String res = TestUtilities.copyFileToString(testFile);
    assertEquals(golden, res);
  }
Esempio n. 2
0
    @Override
    public ChangeInfo implement() throws Exception {
      JavaSource js = JavaSource.forFileObject(sourceCode);

      js.runModificationTask(
              new Task<WorkingCopy>() {
                @Override
                public void run(WorkingCopy parameter) throws Exception {
                  parameter.toPhase(Phase.RESOLVED);
                  TypeElement suppressWarningsAnnotation = null;

                  for (String ann : SUPPRESS_WARNINGS_ANNOTATIONS) {
                    if ((suppressWarningsAnnotation = parameter.getElements().getTypeElement(ann))
                        != null) break;
                  }

                  if (suppressWarningsAnnotation == null) {
                    NotifyDescriptor nd =
                        new NotifyDescriptor.Message(
                            "Cannot find SuppressWarnings annotation with Retention.CLASS, please add some on the classpath",
                            NotifyDescriptor.WARNING_MESSAGE);
                    DialogDisplayer.getDefault().notifyLater(nd);
                    return;
                  }

                  int realPos;

                  if (pos != (-1)) {
                    realPos = pos;
                  } else {
                    realPos =
                        (int) parameter.getCompilationUnit().getLineMap().getPosition(line, 0);
                  }

                  TreePath tp = parameter.getTreeUtilities().pathFor(realPos);

                  while (tp != null && !ANNOTATABLE.contains(tp.getLeaf().getKind())) {
                    tp = tp.getParentPath();
                  }

                  if (tp == null) return;

                  ModifiersTree mods;
                  Tree leaf = tp.getLeaf();

                  switch (leaf.getKind()) {
                    case ANNOTATION_TYPE:
                    case CLASS:
                    case ENUM:
                    case INTERFACE:
                      mods = ((ClassTree) leaf).getModifiers();
                      break;
                    case METHOD:
                      mods = ((MethodTree) leaf).getModifiers();
                      break;
                    case VARIABLE:
                      mods = ((MethodTree) leaf).getModifiers();
                      break;
                    default:
                      throw new IllegalStateException(leaf.getKind().name());
                  }

                  parameter.rewrite(
                      mods,
                      GeneratorUtilities.get(parameter)
                          .appendToAnnotationValue(
                              mods,
                              suppressWarningsAnnotation,
                              "value",
                              parameter.getTreeMaker().Literal(bugId)));
                }
              })
          .commit();

      return null;
    }