當前位置: 首頁>>代碼示例>>Java>>正文


Java MethodTree.getModifiers方法代碼示例

本文整理匯總了Java中com.sun.source.tree.MethodTree.getModifiers方法的典型用法代碼示例。如果您正苦於以下問題:Java MethodTree.getModifiers方法的具體用法?Java MethodTree.getModifiers怎麽用?Java MethodTree.getModifiers使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.sun.source.tree.MethodTree的用法示例。


在下文中一共展示了MethodTree.getModifiers方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testMethodMods1

import com.sun.source.tree.MethodTree; //導入方法依賴的package包/類
/**
 * Original:
 * 
 * void method() {
 * }
 * 
 * Result:
 * 
 * public static void method() {
 * }
 */
public void testMethodMods1() 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" +
            "    void method() {\n" +
            "    }\n" +
            "}\n"
            );
    String golden =
            "package hierbas.del.litoral;\n\n" +
            "import java.io.*;\n\n" +
            "public class Test {\n" +
            "    public static void method() {\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);
            ModifiersTree mods = method.getModifiers();
            workingCopy.rewrite(mods, make.Modifiers(EnumSet.of(Modifier.PUBLIC, Modifier.STATIC)));
        }
        
    };
    testSource.runModificationTask(task).commit();
    String res = TestUtilities.copyFileToString(testFile);
    System.err.println(res);
    assertEquals(golden, res);
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:49,代碼來源:ModifiersTest.java

示例2: testMethodMods2

import com.sun.source.tree.MethodTree; //導入方法依賴的package包/類
/**
 * Original:
 * 
 * public static void method() {
 * }
 * 
 * Result:
 * 
 * void method() {
 * }
 */
public void testMethodMods2() 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 static void method() {\n" +
            "    }\n" +
            "}\n"
            );
    String golden =
            "package hierbas.del.litoral;\n\n" +
            "import java.io.*;\n\n" +
            "public class Test {\n" +
            "    void method() {\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);
            ModifiersTree mods = method.getModifiers();
            workingCopy.rewrite(mods, make.Modifiers(Collections.<Modifier>emptySet()));
        }
        
    };
    testSource.runModificationTask(task).commit();
    String res = TestUtilities.copyFileToString(testFile);
    System.err.println(res);
    assertEquals(golden, res);
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:49,代碼來源:ModifiersTest.java

示例3: testMethodMods3

import com.sun.source.tree.MethodTree; //導入方法依賴的package包/類
/**
 * Original:
 * 
 * Test() {
 * }
 * 
 * Result:
 * 
 * public Test() {
 * }
 */
public void testMethodMods3() 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" +
            "    Test() {\n" +
            "    }\n" +
            "}\n"
            );
    String golden =
            "package hierbas.del.litoral;\n\n" +
            "import java.io.*;\n\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);
            MethodTree method = (MethodTree) clazz.getMembers().get(0);
            ModifiersTree mods = method.getModifiers();
            workingCopy.rewrite(mods, make.Modifiers(Collections.<Modifier>singleton(Modifier.PUBLIC)));
        }
        
    };
    testSource.runModificationTask(task).commit();
    String res = TestUtilities.copyFileToString(testFile);
    System.err.println(res);
    assertEquals(golden, res);
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:49,代碼來源:ModifiersTest.java

示例4: testMethodMods4

import com.sun.source.tree.MethodTree; //導入方法依賴的package包/類
/**
 * Original:
 * 
 * public Test() {
 * }
 * 
 * Result:
 * 
 * Test() {
 * }
 */
public void testMethodMods4() 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" +
            "public class Test {\n" +
            "    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);
            MethodTree method = (MethodTree) clazz.getMembers().get(0);
            ModifiersTree mods = method.getModifiers();
            workingCopy.rewrite(mods, make.Modifiers(Collections.<Modifier>emptySet()));
        }
        
    };
    testSource.runModificationTask(task).commit();
    String res = TestUtilities.copyFileToString(testFile);
    System.err.println(res);
    assertEquals(golden, res);
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:49,代碼來源:ModifiersTest.java

示例5: testMethodMods5

import com.sun.source.tree.MethodTree; //導入方法依賴的package包/類
/**
 * Original:
 * 
 * public static void method() {
 * }
 * 
 * Result:
 * 
 * static void method() {
 * }
 */
public void testMethodMods5() 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 static void method() {\n" +
            "    }\n" +
            "}\n"
            );
    String golden =
            "package hierbas.del.litoral;\n\n" +
            "import java.io.*;\n\n" +
            "public class Test {\n" +
            "    static void method() {\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);
            ModifiersTree mods = method.getModifiers();
            workingCopy.rewrite(mods, make.Modifiers(Collections.<Modifier>singleton(Modifier.STATIC)));
        }
        
    };
    testSource.runModificationTask(task).commit();
    String res = TestUtilities.copyFileToString(testFile);
    System.err.println(res);
    assertEquals(golden, res);
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:49,代碼來源:ModifiersTest.java

示例6: testMethodMods6

import com.sun.source.tree.MethodTree; //導入方法依賴的package包/類
/**
 * Original:
 * 
 * public Test() {
 * }
 * 
 * Result:
 * 
 * protected Test() {
 * }
 */
public void testMethodMods6() 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" +
            "public class Test {\n" +
            "    protected 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);
            MethodTree method = (MethodTree) clazz.getMembers().get(0);
            ModifiersTree mods = method.getModifiers();
            workingCopy.rewrite(mods, make.Modifiers(Collections.<Modifier>singleton(Modifier.PROTECTED)));
        }
        
    };
    testSource.runModificationTask(task).commit();
    String res = TestUtilities.copyFileToString(testFile);
    System.err.println(res);
    assertEquals(golden, res);
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:49,代碼來源:ModifiersTest.java

示例7: test124701

import com.sun.source.tree.MethodTree; //導入方法依賴的package包/類
public void test124701() throws Exception {
    testFile = new File(getWorkDir(), "Test.java");
    TestUtilities.copyStringToFile(testFile,
            "package flaska;\n" +
            "\n" +
            "public class Test {\n" +
            "    @SuppressWarnings(\"x\")\n" +
            "    private void alois() {\n" +
            "    }\n" +
            "    \n" +
            "}\n"
            );
    String golden =
            "package flaska;\n" +
            "\n" +
            "public class Test {\n" +
            "    @SuppressWarnings(\"x\")\n" +
            "    public 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);
            MethodTree method = (MethodTree) clazz.getMembers().get(1);
            ModifiersTree mods = method.getModifiers();
            ModifiersTree copy = make.Modifiers(EnumSet.of(Modifier.PUBLIC), mods.getAnnotations());
            workingCopy.rewrite(mods, copy);
        }
    };
    testSource.runModificationTask(task).commit();
    String res = TestUtilities.copyFileToString(testFile);
    System.err.println(res);
    assertEquals(golden, res);
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:40,代碼來源:ModifiersTest.java

示例8: isInHeader

import com.sun.source.tree.MethodTree; //導入方法依賴的package包/類
private static boolean isInHeader(CompilationInfo info, MethodTree tree, int offset) {
    CompilationUnitTree cut = info.getCompilationUnit();
    SourcePositions sp = info.getTrees().getSourcePositions();
    long lastKnownOffsetInHeader = sp.getStartPosition(cut, tree);
    
    List<? extends ExpressionTree> throwz;
    List<? extends VariableTree> params;
    List<? extends TypeParameterTree> typeparams;
    
    if ((throwz = tree.getThrows()) != null && !throwz.isEmpty()) {
        lastKnownOffsetInHeader = sp.getEndPosition(cut, throwz.get(throwz.size() - 1));
    } else if ((params = tree.getParameters()) != null && !params.isEmpty()) {
        lastKnownOffsetInHeader = sp.getEndPosition(cut, params.get(params.size() - 1));
    } else if ((typeparams = tree.getTypeParameters()) != null && !typeparams.isEmpty()) {
        lastKnownOffsetInHeader = sp.getEndPosition(cut, typeparams.get(typeparams.size() - 1));
    } else if (tree.getReturnType() != null) {
        lastKnownOffsetInHeader = sp.getEndPosition(cut, tree.getReturnType());
    } 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 || ts.token().id() == JavaTokenId.SEMICOLON) {
            return offset < ts.offset();
        }
    }
    
    return false;
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:34,代碼來源:JavadocUtilities.java

示例9: ensureNoArgConstructor

import com.sun.source.tree.MethodTree; //導入方法依賴的package包/類
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 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;
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:43,代碼來源:GenerationUtils.java

示例10: ensureNoArgConstructor

import com.sun.source.tree.MethodTree; //導入方法依賴的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;
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:50,代碼來源:GenerationUtils.java

示例11: getTargetModifiers

import com.sun.source.tree.MethodTree; //導入方法依賴的package包/類
protected ModifiersTree getTargetModifiers(VariableTree fieldTree, MethodTree methodTree) {
    return methodTree.getModifiers();
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:4,代碼來源:UnifyAccessType.java

示例12: getSourceModifiers

import com.sun.source.tree.MethodTree; //導入方法依賴的package包/類
protected ModifiersTree getSourceModifiers(VariableTree fieldTree, MethodTree methodTree) {
    return methodTree.getModifiers();
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:4,代碼來源:UnifyAccessType.java


注:本文中的com.sun.source.tree.MethodTree.getModifiers方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。