Java Code Examples for com.sun.source.tree.ClassTree#getModifiers()
The following examples show how to use
com.sun.source.tree.ClassTree#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: JavacParserTest.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
@Test void testPositionForEnumModifiers() throws IOException { final String theString = "public"; String code = "package test; " + theString + " enum Test {A;}"; JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, 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)); }
Example 2
Source File: JavacParserTest.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
@Test void testPositionForEnumModifiers() throws IOException { final String theString = "public"; String code = "package test; " + theString + " enum Test {A;}"; JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, 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)); }
Example 3
Source File: JavacParserTest.java From hottub with GNU General Public License v2.0 | 6 votes |
@Test void testPositionForEnumModifiers() throws IOException { final String theString = "public"; String code = "package test; " + theString + " enum Test {A;}"; JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, 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)); }
Example 4
Source File: JavacParserTest.java From openjdk-8-source with GNU General Public License v2.0 | 6 votes |
@Test void testPositionForEnumModifiers() throws IOException { final String theString = "public"; String code = "package test; " + theString + " enum Test {A;}"; JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, 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)); }
Example 5
Source File: ImplementAllAbstractMethods.java From netbeans with Apache License 2.0 | 6 votes |
@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); }
Example 6
Source File: ClassStructure.java From netbeans with Apache License 2.0 | 6 votes |
@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); }
Example 7
Source File: NopenChecker.java From nopen with Apache License 2.0 | 5 votes |
@Override public Description matchClass(ClassTree tree, VisitorState state) { if (tree.getKind() != CLASS) { return NO_MATCH; } ModifiersTree modifiers = tree.getModifiers(); Set<Modifier> modifierFlags = modifiers.getFlags(); if (modifierFlags.contains(FINAL) || modifierFlags.contains(ABSTRACT)) { return NO_MATCH; } switch (ASTHelpers.getSymbol(tree).getNestingKind()) { case LOCAL: case ANONYMOUS: return NO_MATCH; case MEMBER: if (modifierFlags.contains(PRIVATE)) { return NO_MATCH; } break; case TOP_LEVEL: break; } for (AnnotationTree annotation : modifiers.getAnnotations()) { AnnotationMirror annotationMirror = ASTHelpers.getAnnotationMirror(annotation); if (annotationMirror.getAnnotationType().toString().equals(OPEN_FQCN)) { return NO_MATCH; } } return describeMatch(tree); }
Example 8
Source File: JUnit4TestGenerator.java From netbeans with Apache License 2.0 | 5 votes |
/** */ @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); }
Example 9
Source File: AnnotationProcessors.java From netbeans with Apache License 2.0 | 5 votes |
public TreePath findSupportedAnnotation() { ClassTree ct = (ClassTree) processorPath.getLeaf(); TreePath modPath = new TreePath(processorPath, ct.getModifiers()); for (AnnotationTree at : ct.getModifiers().getAnnotations()) { TreePath tp = new TreePath(modPath, at); TypeMirror am = info.getTrees().getTypeMirror(tp); if (info.getTypes().isSameType(am, supportedSourceType)) { return tp; } } return null; }
Example 10
Source File: ModifiersTest.java From netbeans with Apache License 2.0 | 5 votes |
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); }
Example 11
Source File: JavaSourceHelper.java From netbeans with Apache License 2.0 | 5 votes |
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); }
Example 12
Source File: JavaSourceHelper.java From netbeans with Apache License 2.0 | 5 votes |
public static void addClassAnnotation(WorkingCopy copy, String[] annotations, Object[] annotationAttrs) { TreeMaker maker = copy.getTreeMaker(); ClassTree tree = getTopLevelClassTree(copy); if (tree == null) { return; } 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); }
Example 13
Source File: ModifiersTest.java From netbeans with Apache License 2.0 | 5 votes |
/** * 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); }
Example 14
Source File: EntityResourcesGenerator.java From netbeans with Apache License 2.0 | 5 votes |
protected ModifiersTree addResourceAnnotation( final String entityFQN, ClassTree classTree, GenerationUtils genUtils, TreeMaker maker ) { // Add @Path annotation to REST resource class ExpressionTree resourcePath = maker.Literal(entityFQN.toLowerCase()); ModifiersTree modifiersTree = classTree.getModifiers(); modifiersTree = maker.addModifiersAnnotation(modifiersTree, genUtils.createAnnotation(RestConstants.PATH, Collections.<ExpressionTree>singletonList( resourcePath))); return modifiersTree; }
Example 15
Source File: ModifiersTest.java From netbeans with Apache License 2.0 | 4 votes |
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); }
Example 16
Source File: ModifiersTest.java From netbeans with Apache License 2.0 | 4 votes |
public void testRemoveClassAnnotation() throws Exception { testFile = new File(getWorkDir(), "Test.java"); TestUtilities.copyStringToFile(testFile, "package flaska;\n" + "\n" + "import java.io.*;\n" + "\n" + "@Annotation\n" + "public class Test {\n" + " void alois() {\n" + " }\n" + " \n" + "}\n" ); String golden = "package flaska;\n" + "\n" + "import java.io.*;\n" + "\n" + "public class Test {\n" + " void alois() {\n" + " }\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(); ModifiersTree modified = make.removeModifiersAnnotation(mods, 0); workingCopy.rewrite(mods, modified); } }; testSource.runModificationTask(task).commit(); String res = TestUtilities.copyFileToString(testFile); //System.err.println(res); assertEquals(golden, res); }
Example 17
Source File: ModifiersTest.java From netbeans with Apache License 2.0 | 4 votes |
public void testRemoveClassAnnotationAttribute1() throws Exception { testFile = new File(getWorkDir(), "Test.java"); TestUtilities.copyStringToFile(testFile, "package flaska;\n" + "\n" + "import java.io.*;\n" + "\n" + "@Annotation(attr1 = \"aa\", attr2 = \"bb\")\n" + "public class Test {\n" + " void alois() {\n" + " }\n" + " \n" + "}\n" ); String golden = "package flaska;\n" + "\n" + "import java.io.*;\n" + "\n" + "@Annotation(attr1 = \"aa\")\n" + "public class Test {\n" + " void alois() {\n" + " }\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(); AnnotationTree ann = mods.getAnnotations().get(0); AnnotationTree modified = make.removeAnnotationAttrValue(ann, 1); workingCopy.rewrite(ann, modified); } }; testSource.runModificationTask(task).commit(); String res = TestUtilities.copyFileToString(testFile); //System.err.println(res); assertEquals(golden, res); }
Example 18
Source File: ModifiersTest.java From netbeans with Apache License 2.0 | 4 votes |
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); }
Example 19
Source File: ModifiersTest.java From netbeans with Apache License 2.0 | 4 votes |
/** * 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); }
Example 20
Source File: ModifiersTest.java From netbeans with Apache License 2.0 | 4 votes |
public void testRemoveClassAnnotationAttribute5() throws Exception { testFile = new File(getWorkDir(), "Test.java"); TestUtilities.copyStringToFile(testFile, "package flaska;\n" + "\n" + "import java.io.*;\n" + "\n" + "@Annotation(attr1 = \"aa\", attr2 = \"bb\", attr3 = \"cc\")\n" + "public class Test {\n" + " void alois() {\n" + " }\n" + " \n" + "}\n" ); String golden = "package flaska;\n" + "\n" + "import java.io.*;\n" + "\n" + "@Annotation()\n" + "public class Test {\n" + " void alois() {\n" + " }\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(); AnnotationTree ann = mods.getAnnotations().get(0); AnnotationTree modified = make.removeAnnotationAttrValue(ann, 2); modified = make.removeAnnotationAttrValue(modified, 1); modified = make.removeAnnotationAttrValue(modified, 0); workingCopy.rewrite(ann, modified); } }; testSource.runModificationTask(task).commit(); String res = TestUtilities.copyFileToString(testFile); //System.err.println(res); assertEquals(golden, res); }