Java Code Examples for com.sun.source.tree.VariableTree#getModifiers()

The following examples show how to use com.sun.source.tree.VariableTree#getModifiers() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LoggerNotStaticFinal.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
protected void performRewrite(TransformationContext ctx) {
    WorkingCopy wc = ctx.getWorkingCopy();
    TreePath tp = ctx.getPath();
    VariableTree vt = (VariableTree) tp.getLeaf();
    ModifiersTree mt = vt.getModifiers();
    Set<Modifier> modifiers = EnumSet.noneOf(Modifier.class);

    modifiers.addAll(mt.getFlags());
    modifiers.add(Modifier.FINAL);
    modifiers.add(Modifier.STATIC);

    ModifiersTree newMod = wc.getTreeMaker().Modifiers(modifiers, mt.getAnnotations());

    wc.rewrite(mt, newMod);
}
 
Example 2
Source File: ModifiersTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testRemoveVariableModifier() throws Exception {
    testFile = new File(getWorkDir(), "Test.java");
    TestUtilities.copyStringToFile(testFile,
            "package flaska;\n" +
            "\n" +
            "public class Test {\n" +
            "    private int a;\n" +
            "}\n"
            );
    String golden =
            "package flaska;\n" +
            "\n" +
            "public class Test {\n" +
            "    int a;\n" +
            "}\n";
    JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
    Task<WorkingCopy> task = new Task<WorkingCopy>() {

        public void run(WorkingCopy workingCopy) throws java.io.IOException {
            workingCopy.toPhase(Phase.RESOLVED);
            TreeMaker make = workingCopy.getTreeMaker();
            ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
            VariableTree var = (VariableTree) clazz.getMembers().get(1);
            ModifiersTree mods = var.getModifiers();
            ModifiersTree copy = make.Modifiers(EnumSet.noneOf(Modifier.class), mods.getAnnotations());
            workingCopy.rewrite(mods, copy);
        }
    };
    testSource.runModificationTask(task).commit();
    String res = TestUtilities.copyFileToString(testFile);
    //System.err.println(res);
    assertEquals(golden, res);
}
 
Example 3
Source File: ConvertToARM.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static VariableTree removeFinal(
        final WorkingCopy wc,
        final VariableTree varTree) {
    final ModifiersTree oldMods = varTree.getModifiers();
    if (oldMods != null && oldMods.getFlags().contains(Modifier.FINAL)) {
        final ModifiersTree newMods = wc.getTreeMaker().removeModifiersModifier(oldMods, Modifier.FINAL);
        rewriteCopyComments(wc, oldMods, newMods);
    }
    return varTree;
}
 
Example 4
Source File: ModifiersTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Tests the change of modifier in local variable
 */
public void testChangeToFinalLocVar() throws Exception {
    testFile = new File(getWorkDir(), "Test.java");
    TestUtilities.copyStringToFile(testFile,
            "package hierbas.del.litoral;\n\n" +
            "import java.io.*;\n\n" +
            "public class Test {\n" +
            "    public void taragui() {\n" +
            "        int i = 10;\n" +
            "    }\n" +
            "}\n"
            );
    String golden =
            "package hierbas.del.litoral;\n\n" +
            "import java.io.*;\n\n" +
            "public class Test {\n" +
            "    public void taragui() {\n" +
            "        final int i = 10;\n" +
            "    }\n" +
            "}\n";
    JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
    Task<WorkingCopy> task = new Task<WorkingCopy>() {
        
        public void run(WorkingCopy workingCopy) throws java.io.IOException {
            workingCopy.toPhase(Phase.RESOLVED);
            TreeMaker make = workingCopy.getTreeMaker();
            
            // finally, find the correct body and rewrite it.
            ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
            MethodTree method = (MethodTree) clazz.getMembers().get(1);
            BlockTree block = method.getBody();
            VariableTree vt = (VariableTree) block.getStatements().get(0);
            ModifiersTree mods = vt.getModifiers();
            workingCopy.rewrite(mods, make.Modifiers(Collections.<Modifier>singleton(Modifier.FINAL)));
        }
        
    };
    testSource.runModificationTask(task).commit();
    String res = TestUtilities.copyFileToString(testFile);
    //System.err.println(res);
    assertEquals(golden, res);
}
 
Example 5
Source File: UnifyAccessType.java    From netbeans with Apache License 2.0 4 votes vote down vote up
protected ModifiersTree getSourceModifiers(VariableTree fieldTree, MethodTree methodTree) {
    return fieldTree.getModifiers();
}
 
Example 6
Source File: UnifyAccessType.java    From netbeans with Apache License 2.0 4 votes vote down vote up
protected ModifiersTree getTargetModifiers(VariableTree fieldTree, MethodTree methodTree) {
    return fieldTree.getModifiers();
}