下面列出了com.sun.source.tree.VariableTree#getModifiers ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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);
}
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);
}
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;
}
/**
* 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);
}
protected ModifiersTree getSourceModifiers(VariableTree fieldTree, MethodTree methodTree) {
return fieldTree.getModifiers();
}
protected ModifiersTree getTargetModifiers(VariableTree fieldTree, MethodTree methodTree) {
return fieldTree.getModifiers();
}