本文整理匯總了Java中com.sun.source.tree.ClassTree.getModifiers方法的典型用法代碼示例。如果您正苦於以下問題:Java ClassTree.getModifiers方法的具體用法?Java ClassTree.getModifiers怎麽用?Java ClassTree.getModifiers使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.sun.source.tree.ClassTree
的用法示例。
在下文中一共展示了ClassTree.getModifiers方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testPositionForEnumModifiers
import com.sun.source.tree.ClassTree; //導入方法依賴的package包/類
@Test
void testPositionForEnumModifiers() throws IOException {
final String theString = "public";
String code = "package test; " + theString + " enum Test {A;}";
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null,
null, Arrays.asList(new MyFileObject(code)));
CompilationUnitTree cut = ct.parse().iterator().next();
SourcePositions pos = Trees.instance(ct).getSourcePositions();
ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
ModifiersTree mt = clazz.getModifiers();
int spos = code.indexOf(theString);
int epos = spos + theString.length();
assertEquals("testPositionForEnumModifiers",
spos, pos.getStartPosition(cut, mt));
assertEquals("testPositionForEnumModifiers",
epos, pos.getEndPosition(cut, mt));
}
示例2: performRewrite
import com.sun.source.tree.ClassTree; //導入方法依賴的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);
}
示例3: performRewrite
import com.sun.source.tree.ClassTree; //導入方法依賴的package包/類
@Override
protected void performRewrite(TransformationContext ctx) {
WorkingCopy wc = ctx.getWorkingCopy();
GeneratorUtilities gu = GeneratorUtilities.get(wc);
TreePath path = ctx.getPath();
final ClassTree cls = (ClassTree) path.getLeaf();
gu.importComments(cls, wc.getCompilationUnit());
final TreeMaker treeMaker = wc.getTreeMaker();
ModifiersTree mods = cls.getModifiers();
if (mods.getFlags().contains(Modifier.ABSTRACT)) {
Set<Modifier> modifiers = EnumSet.copyOf(mods.getFlags());
modifiers.remove(Modifier.ABSTRACT);
ModifiersTree nmods = treeMaker.Modifiers(modifiers, mods.getAnnotations());
gu.copyComments(mods, nmods, true);
gu.copyComments(mods, nmods, false);
mods = nmods;
}
Tree nue = treeMaker.Interface(mods, cls.getSimpleName(), cls.getTypeParameters(), cls.getImplementsClause(), cls.getMembers());
gu.copyComments(cls, nue, true);
gu.copyComments(cls, nue, false);
wc.rewrite(path.getLeaf(), nue);
}
示例4: testAddClassAbstract
import com.sun.source.tree.ClassTree; //導入方法依賴的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);
}
示例5: testChangeInterfaceModifier
import com.sun.source.tree.ClassTree; //導入方法依賴的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);
}
示例6: finishSuiteClass
import com.sun.source.tree.ClassTree; //導入方法依賴的package包/類
/**
*/
@Override
protected ClassTree finishSuiteClass(ClassTree tstClass,
TreePath tstClassTreePath,
List<Tree> tstMembers,
List<String> suiteMembers,
boolean membersChanged,
ClassMap classMap,
WorkingCopy workingCopy) {
ModifiersTree currModifiers = tstClass.getModifiers();
ModifiersTree modifiers = fixSuiteClassModifiers(tstClass,
tstClassTreePath,
currModifiers,
suiteMembers,
workingCopy);
if (!membersChanged) {
if (modifiers != currModifiers) {
workingCopy.rewrite(currModifiers, modifiers);
}
return tstClass;
}
return workingCopy.getTreeMaker().Class(
modifiers,
tstClass.getSimpleName(),
tstClass.getTypeParameters(),
tstClass.getExtendsClause(),
(List<? extends ExpressionTree>) tstClass.getImplementsClause(),
tstMembers);
}
示例7: isInHeader
import com.sun.source.tree.ClassTree; //導入方法依賴的package包/類
private static boolean isInHeader(CompilationInfo info, ClassTree tree, int offset) {
CompilationUnitTree cut = info.getCompilationUnit();
SourcePositions sp = info.getTrees().getSourcePositions();
long lastKnownOffsetInHeader = sp.getStartPosition(cut, tree);
List<? extends Tree> impls = tree.getImplementsClause();
List<? extends TypeParameterTree> typeparams;
if (impls != null && !impls.isEmpty()) {
lastKnownOffsetInHeader= sp.getEndPosition(cut, impls.get(impls.size() - 1));
} else if ((typeparams = tree.getTypeParameters()) != null && !typeparams.isEmpty()) {
lastKnownOffsetInHeader= sp.getEndPosition(cut, typeparams.get(typeparams.size() - 1));
} else if (tree.getExtendsClause() != null) {
lastKnownOffsetInHeader = sp.getEndPosition(cut, tree.getExtendsClause());
} else if (tree.getModifiers() != null) {
lastKnownOffsetInHeader = sp.getEndPosition(cut, tree.getModifiers());
}
TokenSequence<JavaTokenId> ts = info.getTreeUtilities().tokensFor(tree);
ts.move((int) lastKnownOffsetInHeader);
while (ts.moveNext()) {
if (ts.token().id() == JavaTokenId.LBRACE) {
return offset < ts.offset();
}
}
return false;
}
示例8: addClassAnnotation
import com.sun.source.tree.ClassTree; //導入方法依賴的package包/類
public static void addClassAnnotation(WorkingCopy copy, String[] annotations, Object[] annotationAttrs) {
TreeMaker maker = copy.getTreeMaker();
ClassTree tree = getTopLevelClassTree(copy);
ModifiersTree modifiers = tree.getModifiers();
for (int i = 0; i < annotations.length; i++) {
List<ExpressionTree> attrTrees = null;
Object attr = annotationAttrs[i];
if (attr != null) {
attrTrees = new ArrayList<ExpressionTree>();
if (attr instanceof ExpressionTree) {
attrTrees.add((ExpressionTree) attr);
} else {
attrTrees.add(maker.Literal(attr));
}
} else {
attrTrees = Collections.<ExpressionTree>emptyList();
}
AnnotationTree newAnnotation = maker.Annotation(maker.Identifier(annotations[i]), attrTrees);
if (modifiers != null) {
modifiers = maker.addModifiersAnnotation(modifiers, newAnnotation);
}
}
copy.rewrite(tree.getModifiers(), modifiers);
}
示例9: testAnnRename
import com.sun.source.tree.ClassTree; //導入方法依賴的package包/類
/**
* Original:
*
* @Anotace()
* public Test() {
* }
*
* Result:
*
* @Annotaition()
* protected Test() {
* }
*/
public void testAnnRename() throws Exception {
testFile = new File(getWorkDir(), "Test.java");
TestUtilities.copyStringToFile(testFile,
"package hierbas.del.litoral;\n" +
"\n" +
"import java.io.*;\n" +
"\n" +
"@Annotace()\n" +
"public class Test {\n" +
" public Test() {\n" +
" }\n" +
"}\n"
);
String golden =
"package hierbas.del.litoral;\n" +
"\n" +
"import java.io.*;\n" +
"\n" +
"@Annotation()\n" +
"public class Test {\n" +
" public Test() {\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);
ModifiersTree mods = clazz.getModifiers();
AnnotationTree ann = mods.getAnnotations().get(0);
IdentifierTree ident = (IdentifierTree) ann.getAnnotationType();
workingCopy.rewrite(ident, make.Identifier("Annotation"));
}
};
testSource.runModificationTask(task).commit();
String res = TestUtilities.copyFileToString(testFile);
System.err.println(res);
assertEquals(golden, res);
}
示例10: testAddArrayValue
import com.sun.source.tree.ClassTree; //導入方法依賴的package包/類
/**
* Original:
*
* public class Test {
* ...
*
* Result:
*
* @Annotation(value = { "Lojza", "Karel" })
* public class Test {
* ...
*
*/
public void testAddArrayValue() 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 Test() {\n" +
" }\n" +
"}\n"
);
String golden =
"package hierbas.del.litoral;\n" +
"\n" +
"import java.io.*;\n" +
"\n" +
"@Annotation(value = {\"Lojza\", \"Karel\"})\n" +
"public class Test {\n" +
" public Test() {\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();
ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
ModifiersTree mods = clazz.getModifiers();
List<LiteralTree> l = new ArrayList<LiteralTree>();
l.add(make.Literal("Lojza"));
l.add(make.Literal("Karel"));
NewArrayTree nat = make.NewArray(null, Collections.<ExpressionTree>emptyList(), l);
AssignmentTree at = make.Assignment(make.Identifier("value"), nat);
AnnotationTree ann = make.Annotation(make.Identifier("Annotation"), Collections.<ExpressionTree>singletonList(at));
workingCopy.rewrite(mods, make.Modifiers(mods.getFlags(), Collections.<AnnotationTree>singletonList(ann)));
}
};
testSource.runModificationTask(task).commit();
String res = TestUtilities.copyFileToString(testFile);
System.err.println(res);
assertEquals(golden, res);
}
示例11: testRenameAnnotationAttribute
import com.sun.source.tree.ClassTree; //導入方法依賴的package包/類
public void testRenameAnnotationAttribute() throws Exception {
testFile = new File(getWorkDir(), "Test.java");
TestUtilities.copyStringToFile(testFile,
"package flaska;\n" +
"\n" +
"import java.io.*;\n" +
"\n" +
"/**\n" +
" *aa\n" +
" */\n" +
"@Annotation(val = 2)\n" +
"public class Test {\n" +
"}\n"
);
String golden =
"package flaska;\n" +
"\n" +
"import java.io.*;\n" +
"\n" +
"/**\n" +
" *aa\n" +
" */\n" +
"@Annotation(value = 2)\n" +
"public class 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();
AnnotationTree annotationTree = mods.getAnnotations().get(0);
AssignmentTree assignementTree = (AssignmentTree) annotationTree.getArguments().get(0);
workingCopy.rewrite(assignementTree.getVariable(), make.Identifier("value"));
}
};
testSource.runModificationTask(task).commit();
String res = TestUtilities.copyFileToString(testFile);
System.err.println(res);
assertEquals(golden, res);
}
示例12: testMakeClassAbstract
import com.sun.source.tree.ClassTree; //導入方法依賴的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);
}
示例13: test106543
import com.sun.source.tree.ClassTree; //導入方法依賴的package包/類
public void test106543() throws Exception {
testFile = new File(getWorkDir(), "Test.java");
TestUtilities.copyStringToFile(testFile,
"package flaska;\n" +
"\n" +
"import java.io.*;\n" +
"\n" +
"/**\n" +
" *aa\n" +
" */\n" +
"@Annotation(val = 2)\n" +
"public class Test {\n" +
"}\n"
);
String golden =
"package flaska;\n" +
"\n" +
"import java.io.*;\n" +
"\n" +
"/**\n" +
" *aa\n" +
" */\n" +
"@Annotation()\n" +
"public class 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();
AnnotationTree annotationTree = mods.getAnnotations().get(0);
AnnotationTree copy = make.removeAnnotationAttrValue(annotationTree, annotationTree.getArguments().get(0));
workingCopy.rewrite(annotationTree, copy);
}
};
testSource.runModificationTask(task).commit();
String res = TestUtilities.copyFileToString(testFile);
System.err.println(res);
assertEquals(golden, res);
}
示例14: test106403
import com.sun.source.tree.ClassTree; //導入方法依賴的package包/類
public void test106403() throws Exception {
testFile = new File(getWorkDir(), "Test.java");
TestUtilities.copyStringToFile(testFile,
"package flaska;\n" +
"\n" +
"import java.io.*;\n" +
"\n" +
"/**\n" +
" *aa\n" +
" */\n" +
"@Annotation\n" +
"public class Test {\n" +
"}\n"
);
String golden =
"package flaska;\n" +
"\n" +
"import java.io.*;\n" +
"\n" +
"/**\n" +
" *aa\n" +
" */\n" +
"@Annotation(val = 2)\n" +
"public class 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();
AnnotationTree annotationTree = mods.getAnnotations().get(0);
AnnotationTree modified = make.addAnnotationAttrValue(
annotationTree,
make.Assignment(make.Identifier("val"), make.Literal(2))
);
workingCopy.rewrite(annotationTree, modified);
}
};
testSource.runModificationTask(task).commit();
String res = TestUtilities.copyFileToString(testFile);
System.err.println(res);
assertEquals(golden, res);
}
示例15: test106403_2
import com.sun.source.tree.ClassTree; //導入方法依賴的package包/類
public void test106403_2() throws Exception {
testFile = new File(getWorkDir(), "Test.java");
TestUtilities.copyStringToFile(testFile,
"package flaska;\n" +
"\n" +
"import java.io.*;\n" +
"\n" +
"/**\n" +
" *aa\n" +
" */\n" +
"@Annotation()\n" +
"public class Test {\n" +
"}\n"
);
String golden =
"package flaska;\n" +
"\n" +
"import java.io.*;\n" +
"\n" +
"/**\n" +
" *aa\n" +
" */\n" +
"@Annotation(val = 2)\n" +
"public class 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();
AnnotationTree annotationTree = mods.getAnnotations().get(0);
AnnotationTree modified = make.addAnnotationAttrValue(
annotationTree,
make.Assignment(make.Identifier("val"), make.Literal(2))
);
workingCopy.rewrite(annotationTree, modified);
}
};
testSource.runModificationTask(task).commit();
String res = TestUtilities.copyFileToString(testFile);
System.err.println(res);
assertEquals(golden, res);
}