本文整理汇总了Java中com.sun.source.tree.ModifiersTree.getFlags方法的典型用法代码示例。如果您正苦于以下问题:Java ModifiersTree.getFlags方法的具体用法?Java ModifiersTree.getFlags怎么用?Java ModifiersTree.getFlags使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.source.tree.ModifiersTree
的用法示例。
在下文中一共展示了ModifiersTree.getFlags方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: performRewrite
import com.sun.source.tree.ModifiersTree; //导入方法依赖的package包/类
@Override
protected void performRewrite(TransformationContext ctx) throws Exception {
WorkingCopy wc = ctx.getWorkingCopy();
Tree.Kind k = ctx.getPath().getLeaf().getKind();
if (!TreeUtilities.CLASS_TREE_KINDS.contains(k)) {
// TODO: report
return;
}
ClassTree ct = (ClassTree)ctx.getPath().getLeaf();
ModifiersTree mt = ct.getModifiers();
Set<Modifier> mods = new HashSet<>(mt.getFlags());
mods.remove(Modifier.FINAL);
mods.add(Modifier.ABSTRACT);
ModifiersTree newMt = wc.getTreeMaker().Modifiers(mods, mt.getAnnotations());
wc.rewrite(mt, newMt);
}
示例2: testAddClassAbstract
import com.sun.source.tree.ModifiersTree; //导入方法依赖的package包/类
/**
* Update top-level class modifiers.
*/
public void testAddClassAbstract() 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 abstract void taragui();\n" +
"}\n"
);
String golden =
"package hierbas.del.litoral;\n\n" +
"import java.io.*;\n\n" +
"public abstract class Test {\n" +
" public abstract void taragui();\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);
ModifiersTree mods = clazz.getModifiers();
Set<Modifier> s = new HashSet<Modifier>(mods.getFlags());
s.add(Modifier.ABSTRACT);
workingCopy.rewrite(mods, make.Modifiers(s));
}
};
testSource.runModificationTask(task).commit();
String res = TestUtilities.copyFileToString(testFile);
System.err.println(res);
assertEquals(golden, res);
}
示例3: testChangeInterfaceModifier
import com.sun.source.tree.ModifiersTree; //导入方法依赖的package包/类
public void testChangeInterfaceModifier() throws Exception {
testFile = new File(getWorkDir(), "Test.java");
TestUtilities.copyStringToFile(testFile,
"package flaska;\n" +
"\n" +
"public interface Test {\n" +
"}\n"
);
String golden =
"package flaska;\n" +
"\n" +
"interface Test {\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);
ModifiersTree mods = clazz.getModifiers();
Set<Modifier> flags = new HashSet<Modifier>(mods.getFlags());
flags.remove(Modifier.PUBLIC);
ModifiersTree modified = make.Modifiers(flags);
ClassTree copy = make.Interface(
modified,
clazz.getSimpleName(),
Collections.<TypeParameterTree>emptyList(),
Collections.<Tree>emptyList(),
clazz.getMembers()
);
workingCopy.rewrite(clazz, copy);
}
};
testSource.runModificationTask(task).commit();
String res = TestUtilities.copyFileToString(testFile);
System.err.println(res);
assertEquals(golden, res);
}
示例4: testMakeClassAbstract
import com.sun.source.tree.ModifiersTree; //导入方法依赖的package包/类
public void testMakeClassAbstract() throws Exception {
testFile = new File(getWorkDir(), "Test.java");
TestUtilities.copyStringToFile(testFile,
"package org.netbeans.test.java.hints;\n" +
"\n" +
"@Test1 @Test2(test=\"uuu\") class MakeClassAbstract3 {\n" +
"\n" +
" public MakeClassAbstract3() {\n" +
" }\n" +
"\n" +
" public abstract void test();\n" +
"\n" +
"}\n" +
"\n" +
"@interface Test1 {}\n" +
"\n" +
"@interface Test2 {\n" +
" public String test();\n" +
"}\n"
);
String golden =
"package org.netbeans.test.java.hints;\n" +
"\n" +
"@Test1 @Test2(test=\"uuu\") abstract class MakeClassAbstract3 {\n" +
"\n" +
" public MakeClassAbstract3() {\n" +
" }\n" +
"\n" +
" public abstract void test();\n" +
"\n" +
"}\n" +
"\n" +
"@interface Test1 {}\n" +
"\n" +
"@interface Test2 {\n" +
" public String test();\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);
ModifiersTree mods = clazz.getModifiers();
Set<Modifier> flags = new HashSet<Modifier>(mods.getFlags());
flags.add(Modifier.ABSTRACT);
workingCopy.rewrite(mods, make.Modifiers(flags, mods.getAnnotations()));
}
};
testSource.runModificationTask(task).commit();
String res = TestUtilities.copyFileToString(testFile);
System.err.println(res);
assertEquals(golden, res);
}
示例5: ensureNoArgConstructor
import com.sun.source.tree.ModifiersTree; //导入方法依赖的package包/类
/**
* Ensures the given class has a public no-arg constructor.
*
* @param classTree the class to ensure the constructor for; cannot be null.
* @return a modified class if a no-arg constructor was added, the original
* class otherwise; never null.
*/
public ClassTree ensureNoArgConstructor(ClassTree classTree) {
TypeElement typeElement = SourceUtils.classTree2TypeElement(copy, classTree);
if (typeElement == null) {
throw new IllegalArgumentException("No TypeElement for ClassTree " + classTree.getSimpleName());
}
ExecutableElement constructor = SourceUtils.getNoArgConstructor(copy, typeElement);
MethodTree constructorTree = constructor != null ? copy.getTrees().getTree(constructor) : null;
MethodTree newConstructorTree = null;
TreeMaker make = getTreeMaker();
if (constructor != null) {
if (!constructor.getModifiers().contains(Modifier.PUBLIC)) {
ModifiersTree oldModifiersTree = constructorTree.getModifiers();
Set<Modifier> newModifiers = EnumSet.of(Modifier.PUBLIC);
for (Modifier modifier : oldModifiersTree.getFlags()) {
if (!Modifier.PROTECTED.equals(modifier) && !Modifier.PRIVATE.equals(modifier)) {
newModifiers.add(modifier);
}
}
newConstructorTree = make.Constructor(
make.Modifiers(newModifiers),
constructorTree.getTypeParameters(),
constructorTree.getParameters(),
constructorTree.getThrows(),
constructorTree.getBody());
}
} else {
newConstructorTree = make.Constructor(
createModifiers(Modifier.PUBLIC),
Collections.<TypeParameterTree>emptyList(),
Collections.<VariableTree>emptyList(),
Collections.<ExpressionTree>emptyList(),
"{ }"); // NOI18N
}
ClassTree newClassTree = classTree;
if (newConstructorTree != null) {
if (constructorTree != null) {
newClassTree = make.removeClassMember(newClassTree, constructorTree);
}
newClassTree = make.addClassMember(newClassTree, newConstructorTree);
}
return newClassTree;
}