当前位置: 首页>>代码示例>>Java>>正文


Java JavaSource.forFileObject方法代码示例

本文整理汇总了Java中org.netbeans.api.java.source.JavaSource.forFileObject方法的典型用法代码示例。如果您正苦于以下问题:Java JavaSource.forFileObject方法的具体用法?Java JavaSource.forFileObject怎么用?Java JavaSource.forFileObject使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.netbeans.api.java.source.JavaSource的用法示例。


在下文中一共展示了JavaSource.forFileObject方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getJavaSources

import org.netbeans.api.java.source.JavaSource; //导入方法依赖的package包/类
public static List<JavaSource> getJavaSources(Project project) {
    List<JavaSource> result = new ArrayList<JavaSource>();
    SourceGroup[] groups = SourceGroupSupport.getJavaSourceGroups(project);

    for (SourceGroup group : groups) {
        FileObject root = group.getRootFolder();
        Enumeration<? extends FileObject> files = root.getData(true);

        while (files.hasMoreElements()) {
            FileObject fobj = files.nextElement();

            if (fobj.getExt().equals(JAVA_EXT)) {
                JavaSource source = JavaSource.forFileObject(fobj);
                result.add(source);
            }
        }
    }

    return result;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:21,代码来源:JavaSourceHelper.java

示例2: performErrorsCorrectTest

import org.netbeans.api.java.source.JavaSource; //导入方法依赖的package包/类
private void performErrorsCorrectTest(String code, String sourceLevel, String... errors) throws Exception {
    prepareTest();

    FileObject src = FileUtil.createData(sourceRoot, "test/Test.java");

    TestUtilities.copyStringToFile(src, code);
    SourceUtilsTestUtil.setSourceLevel(src, sourceLevel);
    JavaSource javaSource = JavaSource.forFileObject(src);
    final Set<String> actual = new HashSet<String>();
    javaSource.runUserActionTask(new Task<CompilationController>() {
        public void run(CompilationController controller) throws IOException {
            controller.toPhase(Phase.RESOLVED);
            for (Diagnostic<?> d : controller.getDiagnostics()) {
                actual.add(d.getPosition() + ":" + d.getCode());
            }
        }
    }, true);
    
    assertEquals(new HashSet<String>(Arrays.asList(errors)), actual);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:21,代码来源:PostFlowAnalysisTest.java

示例3: testAssignLeft

import org.netbeans.api.java.source.JavaSource; //导入方法依赖的package包/类
/**
 * #92187: Test for left right side of assignment
 */
public void testAssignLeft() throws Exception {
    testFile = new File(getWorkDir(), "Test.java");
    TestUtilities.copyStringToFile(testFile, 
        "package personal;\n" +
        "\n" +
        "public class Test {\n" +
        "    public Object method() {\n" +
        "        this.key = key;\n" +
        "    }\n" +
        "}\n");

     String golden = 
        "package personal;\n" +
        "\n" +
        "public class Test {\n" +
        "    public Object method() {\n" +
        "        this.key2 = key;\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);
            ExpressionStatementTree est = (ExpressionStatementTree) method.getBody().getStatements().get(0);
            AssignmentTree assignment = (AssignmentTree) est.getExpression();
            MemberSelectTree mstCopy = make.setLabel((MemberSelectTree) assignment.getVariable(), "key2");
            workingCopy.rewrite(assignment.getVariable(), mstCopy);
        }
        
    };
    testSource.runModificationTask(task).commit();
    String res = TestUtilities.copyFileToString(testFile);
    System.err.println(res);
    assertEquals(golden, res);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:43,代码来源:BodyStatementTest.java

示例4: resolveTreePathHandle

import org.netbeans.api.java.source.JavaSource; //导入方法依赖的package包/类
/**
 * Resolves the TreePathHandle for the given refactoring.
 * @param refactoring the refactoring.
 * @return the TreePathHandle or null if no handle could be resolved.
 */
public static TreePathHandle resolveTreePathHandle(AbstractRefactoring refactoring) throws IOException {
    Parameters.notNull("refactoring", refactoring); //NO18N
    
    TreePathHandle tph = refactoring.getRefactoringSource().lookup(TreePathHandle.class);
    if (tph != null) {
        return tph;
    }
    
    final TreePathHandle[] result = new TreePathHandle[1];
    JavaSource source = JavaSource.forFileObject(refactoring.getRefactoringSource().lookup(FileObject.class));
    
    source.runUserActionTask(new CancellableTask<CompilationController>() {
        @Override
        public void cancel() {
        }
        @Override
        public void run(CompilationController co) throws Exception {
            co.toPhase(JavaSource.Phase.RESOLVED);
            CompilationUnitTree cut = co.getCompilationUnit();
            result[0] = TreePathHandle.create(TreePath.getPath(cut, cut.getTypeDecls().get(0)), co);
        }
        
    }, true);
    
    return result[0];
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:32,代码来源:RefactoringUtil.java

示例5: testObjectEditing2

import org.netbeans.api.java.source.JavaSource; //导入方法依赖的package包/类
public void testObjectEditing2() throws Exception {
    FileObject java = FileUtil.createData(fo, "X.java");
    final String what1 = "package java.lang; public class Object {\n" +
                         "  public int hashCode() {return 0;}" +
                         "  public int hashCode() {return 0;}" +
                         "  public boolean equals(Object o) {return false;}" +
                         "  public boolean equals(Object o) {return false;}" +
                         "  \n";

    String what2 =
        "}\n";
    String what = what1 + what2;
    GeneratorUtilsTest.writeIntoFile(java, what);

    JavaSource js = JavaSource.forFileObject(java);
    assertNotNull("Created", js);

    class TaskImpl implements Task<WorkingCopy> {
        public void run(WorkingCopy copy) throws Exception {
            copy.toPhase(JavaSource.Phase.RESOLVED);
            ClassTree clazzTree = (ClassTree) copy.getCompilationUnit().getTypeDecls().get(0);
            TreePath clazz = new TreePath(new TreePath(copy.getCompilationUnit()), clazzTree);
            List<VariableElement> vars = new LinkedList<VariableElement>();

            for (Tree m : clazzTree.getMembers()) {
                if (m.getKind() == Kind.VARIABLE) {
                    vars.add((VariableElement) copy.getTrees().getElement(new TreePath(clazz, m)));
                }
            }

            EqualsHashCodeGenerator.overridesHashCodeAndEquals(copy, copy.getTrees().getElement(clazz), null);
        }
    }

    TaskImpl t = new TaskImpl();

    js.runModificationTask(t);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:39,代码来源:EqualsHashCodeGeneratorTest.java

示例6: testAddClassAbstract

import org.netbeans.api.java.source.JavaSource; //导入方法依赖的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);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:40,代码来源:ModifiersTest.java

示例7: renameJavaClass

import org.netbeans.api.java.source.JavaSource; //导入方法依赖的package包/类
private void renameJavaClass(RefactoringElementsBag refactoringElements, TreePathHandle treePathHandle,
        FileObject fo) throws IOException {

    RenamedClassName clazz = null;

    // Figure out the old name and new name
    JavaSource js = JavaSource.forFileObject(fo);
    if (js != null) {
        clazz = HibernateRefactoringUtil.getRenamedClassName(treePathHandle, js, refactoring.getNewName());
    }

    if (clazz != null) {
        String oldBinaryName = clazz.getOldBinaryName();
        String newBinaryName = clazz.getNewBinaryName();
        if (oldBinaryName != null && newBinaryName != null) {

            Map<FileObject, List<OccurrenceItem>> occurrences =
                    HibernateRefactoringUtil.getJavaClassOccurrences(mFileObjs, oldBinaryName);

            for (FileObject mFileObj : occurrences.keySet()) {
                List<OccurrenceItem> foundPlaces = occurrences.get(mFileObj);
                for( OccurrenceItem foundPlace : foundPlaces) {
                    HibernateRenameRefactoringElement elem = new HibernateRenameRefactoringElement(mFileObj,
                            oldBinaryName,
                            foundPlace.getMatching(),
                            newBinaryName,
                            foundPlace.getLocation(),
                            foundPlace.getText());
                    refactoringElements.add(refactoring, elem);
                }
            }

            refactoringElements.registerTransaction(new JavaClassRenameTransaction(occurrences.keySet(), oldBinaryName, newBinaryName));
        }
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:37,代码来源:HibernateRenamePlugin.java

示例8: testObjectEditing1

import org.netbeans.api.java.source.JavaSource; //导入方法依赖的package包/类
public void testObjectEditing1() throws Exception {
    FileObject java = FileUtil.createData(fo, "X.java");
    final String what1 = "package java.lang; public class Object {\n" +
                         "  public int hashCode() {return 0;}" +
                         "  public static int hashCode(Object o) {return 0;}" +
                         "  public boolean equals(Object o) {return false;}" +
                         "  public boolean equals(Object o1, Object o2) {return false;}" +
                         "  \n";

    String what2 =
        "}\n";
    String what = what1 + what2;
    GeneratorUtilsTest.writeIntoFile(java, what);

    JavaSource js = JavaSource.forFileObject(java);
    assertNotNull("Created", js);

    class TaskImpl implements Task<WorkingCopy> {
        public void run(WorkingCopy copy) throws Exception {
            copy.toPhase(JavaSource.Phase.RESOLVED);
            ClassTree clazzTree = (ClassTree) copy.getCompilationUnit().getTypeDecls().get(0);
            TreePath clazz = new TreePath(new TreePath(copy.getCompilationUnit()), clazzTree);
            List<VariableElement> vars = new LinkedList<VariableElement>();

            for (Tree m : clazzTree.getMembers()) {
                if (m.getKind() == Kind.VARIABLE) {
                    vars.add((VariableElement) copy.getTrees().getElement(new TreePath(clazz, m)));
                }
            }

            EqualsHashCodeGenerator.overridesHashCodeAndEquals(copy, copy.getTrees().getElement(clazz), null);
        }
    }

    TaskImpl t = new TaskImpl();

    js.runModificationTask(t);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:39,代码来源:EqualsHashCodeGeneratorTest.java

示例9: testRemovingReturnType134403

import org.netbeans.api.java.source.JavaSource; //导入方法依赖的package包/类
public void testRemovingReturnType134403() throws Exception {
    testFile = new File(getWorkDir(), "Test.java");
    TestUtilities.copyStringToFile(testFile,
        "package hierbas.del.litoral;\n" +
        "\n" +
        "public class Test {\n" +
        "    public void    Test() {\n" +
        "    }\n" +
        "}\n"
        );
    String golden =
        "package hierbas.del.litoral;\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();

            ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
            MethodTree method = (MethodTree) clazz.getMembers().get(1);
            MethodTree nueMethod = make.Method(method.getModifiers(), method.getName(), null, method.getTypeParameters(), method.getParameters(), method.getThrows(), method.getBody(), (ExpressionTree) method.getDefaultValue());
            workingCopy.rewrite(method, nueMethod);
        }

    };
    testSource.runModificationTask(task).commit();
    String res = TestUtilities.copyFileToString(testFile);
    System.err.println(res);
    assertEquals(golden, res);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:37,代码来源:ConstructorTest.java

示例10: test111983

import org.netbeans.api.java.source.JavaSource; //导入方法依赖的package包/类
public void test111983() throws Exception {
    testFile = new File(getWorkDir(), "Test.java");
    TestUtilities.copyStringToFile(testFile, 
        "package javaapplication1;\n" +
        "\n" +
        "public class NewClass {\n" +
        "    public static <T> T method() {\n" +
        "        return null;\n" +
        "    }\n" +
        "    \n" +
        "    static void m() {\n" +
        "        NewClass.<Class>method();\n" +
        "    }\n" +
        "}\n");
    String golden = 
        "package javaapplication1;\n" +
        "\n" +
        "public class NewClass {\n" +
        "    public static <T> T metoda() {\n" +
        "        return null;\n" +
        "    }\n" +
        "    \n" +
        "    static void m() {\n" +
        "        NewClass.<Class>metoda();\n" +
        "    }\n" +
        "}\n";
    JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
    Task<WorkingCopy> task = new Task<WorkingCopy>() {

        public void run(WorkingCopy workingCopy) throws IOException {
            workingCopy.toPhase(Phase.RESOLVED);
            TreeMaker make = workingCopy.getTreeMaker();
            ClassTree clazz = (ClassTree)workingCopy.getCompilationUnit().getTypeDecls().get(0);
            MethodTree method = (MethodTree)clazz.getMembers().get(1);
            workingCopy.rewrite(method, make.setLabel(method, "metoda"));
            
            method = (MethodTree)clazz.getMembers().get(2);
            BlockTree block = method.getBody();
            ExpressionStatementTree est = (ExpressionStatementTree) block.getStatements().get(0);
            MethodInvocationTree mit = (MethodInvocationTree) est.getExpression();
            MemberSelectTree mst = (MemberSelectTree) mit.getMethodSelect();
            workingCopy.rewrite(mst, make.setLabel(mst, "metoda"));
        }
        
        public void cancel() {
        }
    };
    testSource.runModificationTask(task).commit();
    String res = TestUtilities.copyFileToString(testFile);
    System.err.println(res);
    assertEquals(golden, res);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:53,代码来源:BodyStatementTest.java

示例11: testAddFirstToMultiCatch

import org.netbeans.api.java.source.JavaSource; //导入方法依赖的package包/类
public void testAddFirstToMultiCatch() throws Exception {
    testFile = new File(getWorkDir(), "Test.java");
    TestUtilities.copyStringToFile(testFile,
        "package hierbas.del.litoral;\n" +
        "import java.io.*;\n" +
        "import java.net.*;\n" +
        "public class Test {\n" +
        "    public void taragui() {\n" +
        "        try {\n" +
        "        } catch (MalformedURLException | FileNotFoundException ex) {\n" +
        "        }\n" +
        "    }\n" +
        "}\n"
        );
    String golden =
        "package hierbas.del.litoral;\n" +
        "import java.io.*;\n" +
        "import java.net.*;\n" +
        "public class Test {\n" +
        "    public void taragui() {\n" +
        "        try {\n" +
        "        } catch (IOException | MalformedURLException | FileNotFoundException ex) {\n" +
        "        }\n" +
        "    }\n" +
        "}\n";
    JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
    Task task = new Task<WorkingCopy>() {

        public void run(final WorkingCopy workingCopy) throws IOException {
            workingCopy.toPhase(Phase.RESOLVED);
            final TreeMaker make = workingCopy.getTreeMaker();

            new ErrorAwareTreeScanner<Void, Void>() {
                @Override public Void visitUnionType(UnionTypeTree node, Void p) {
                    List<Tree> alternatives = new ArrayList<Tree>(node.getTypeAlternatives());
                    alternatives.add(0, make.Identifier("IOException"));
                    workingCopy.rewrite(node, make.UnionType(alternatives));
                    return null;
                }
            }.scan(workingCopy.getCompilationUnit(), null);
        }

    };
    testSource.runModificationTask(task).commit();
    String res = TestUtilities.copyFileToString(testFile);
    System.err.println(res);
    assertEquals(golden, res);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:49,代码来源:MultiCatchTest.java

示例12: instantiate

import org.netbeans.api.java.source.JavaSource; //导入方法依赖的package包/类
@Override
public Set instantiate() throws IOException, IllegalArgumentException {
    Set set = delegateIterator.instantiate();
    FileObject template = (FileObject) set.iterator().next();
    if (wiz instanceof TemplateWizard) {
        Logger logger = Logger.getLogger("org.netbeans.ui.metrics.form"); // NOI18N
        LogRecord rec = new LogRecord(Level.INFO, "USG_FORM_CREATED"); // NOI18N
        rec.setLoggerName(logger.getName());
        rec.setParameters(new Object[] { ((TemplateWizard)wiz).getTemplate().getName() });
        logger.log(rec);
    }

    if (specifySuperclass) {
        final String className = template.getName();
        final String superclassName = 
                ((SuperclassWizardPanel) superclassPanel).getSuperclassName();           
        JavaSource js = JavaSource.forFileObject(template);
        js.runModificationTask(new CancellableTask<WorkingCopy>() {
            @Override
            public void cancel() {
            }
            @Override
            public void run(WorkingCopy wcopy) throws Exception {
                wcopy.toPhase(JavaSource.Phase.RESOLVED);
   
                for (Tree t: wcopy.getCompilationUnit().getTypeDecls()) {
                    if (TreeUtilities.CLASS_TREE_KINDS.contains(t.getKind()) && className.equals(((ClassTree) t).getSimpleName().toString())) {
                        ClassTree orig = (ClassTree) t;
                        TreeMaker maker = wcopy.getTreeMaker();
                        TypeElement superclassElm = wcopy.getElements().getTypeElement(superclassName);
                        ExpressionTree extendsTree = superclassElm != null
                            ? maker.QualIdent(superclassElm)
                            : maker.Identifier(superclassName);
                        ClassTree copy = maker.Class(
                            orig.getModifiers(),
                            orig.getSimpleName(),
                            orig.getTypeParameters(),
                            extendsTree,
                            orig.getImplementsClause(),
                            orig.getMembers());
                        wcopy.rewrite(orig, copy);
                        break;
                    }
                }
            }
        }).commit();
    }

    template.setAttribute("justCreatedByNewWizard", Boolean.TRUE); // NOI18N
    
    return set;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:53,代码来源:TemplateWizardIterator.java

示例13: testMakeClassAbstract

import org.netbeans.api.java.source.JavaSource; //导入方法依赖的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);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:57,代码来源:ModifiersTest.java

示例14: implement

import org.netbeans.api.java.source.JavaSource; //导入方法依赖的package包/类
public ChangeInfo implement(){
    CancellableTask<WorkingCopy> task = new CancellableTask<WorkingCopy>(){
        public void cancel() {}
        
        public void run(WorkingCopy workingCopy) throws Exception {
            workingCopy.toPhase(JavaSource.Phase.RESOLVED);
            TypeElement clazz = classHandle.resolve(workingCopy);
            
            if (clazz != null){
                
                for (ExecutableElement methodElem : ElementFilter.methodsIn(clazz.getEnclosedElements())){
                    if (methodElem.getSimpleName().toString().startsWith("get")){ //NOI18N
                        VariableElement fieldElem = ModelUtils.getField(clazz, ModelUtils.getFieldNameFromAccessor(methodElem.getSimpleName().toString()));
                        
                        if (fieldElem != null){
                            MethodTree methodTree = workingCopy.getTrees().getTree((methodElem));
                            VariableTree fieldTree = (VariableTree) workingCopy.getTrees().getTree(fieldElem);
                            
                            ModifiersTree srcModifiersTree = getSourceModifiers(fieldTree, methodTree);
                            
                            List <AnnotationTree> remainingAnnotations = new LinkedList<AnnotationTree>();
                            List <AnnotationTree> newTargetAnnots = new LinkedList<AnnotationTree>();
                            
                            for (AnnotationTree annTree : srcModifiersTree.getAnnotations()){
                                if (isJPAAttrAnnotation(workingCopy, annTree)){
                                    newTargetAnnots.add(annTree);
                                } else {
                                    remainingAnnotations.add(annTree);
                                }
                            }
                            
                            if (newTargetAnnots.size() > 0){
                                TreeMaker make = workingCopy.getTreeMaker();
                                ModifiersTree targetModifiers = getTargetModifiers(fieldTree, methodTree);
                                
                                workingCopy.rewrite(srcModifiersTree, make.Modifiers(srcModifiersTree, remainingAnnotations));
                                newTargetAnnots.addAll(targetModifiers.getAnnotations());
                                workingCopy.rewrite(targetModifiers,make.Modifiers(targetModifiers,newTargetAnnots));
                            }
                        }
                    }
                }
            }
        }
    };
    
    JavaSource javaSource = JavaSource.forFileObject(fileObject);
    
    try{
        javaSource.runModificationTask(task).commit();
    } catch (IOException e){
        JPAProblemFinder.LOG.log(Level.SEVERE, e.getMessage(), e);
    }
    return null;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:56,代码来源:UnifyAccessType.java

示例15: testRenameInIfStatement

import org.netbeans.api.java.source.JavaSource; //导入方法依赖的package包/类
/**
 * Renames el to element in method parameter and if statement
 */
public void testRenameInIfStatement() throws Exception {
    testFile = new File(getWorkDir(), "Test.java");
    TestUtilities.copyStringToFile(testFile, 
        "package personal;\n" +
        "\n" +
        "import javax.swing.text.Element;\n" +
        "\n" +
        "public class Test {\n" +
        "    public void action666(Element el) {\n" +
        "        if (el.getName().equalsIgnoreCase(\"flaskuvElement\")) {\n" +
        "            System.err.println(\"Win!\");\n" +
        "        }\n" +
        "    }\n" +
        "}\n");
    String golden = 
        "package personal;\n" +
        "\n" +
        "import javax.swing.text.Element;\n" +
        "\n" +
        "public class Test {\n" +
        "    public void action666(Element element) {\n" +
        "        if (element.getName().equalsIgnoreCase(\"flaskuvElement\")) {\n" +
        "            System.err.println(\"Win!\");\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);
            // rename in parameter
            VariableTree vt = method.getParameters().get(0);
            VariableTree parCopy = make.setLabel(vt, "element");
            workingCopy.rewrite(vt, parCopy);
            // no need to check kind
            // rename in if
            IfTree statementTree = (IfTree) method.getBody().getStatements().get(0);
            ParenthesizedTree condition = (ParenthesizedTree) statementTree.getCondition();
            MethodInvocationTree invocation = (MethodInvocationTree) condition.getExpression();
            MemberSelectTree select = (MemberSelectTree) invocation.getMethodSelect();
            invocation = (MethodInvocationTree) select.getExpression();
            select = (MemberSelectTree) invocation.getMethodSelect();
            IdentifierTree identToRename = (IdentifierTree) select.getExpression();
            IdentifierTree copy = make.setLabel(identToRename, "element");
            workingCopy.rewrite(identToRename, copy);
        }
        
    };
    testSource.runModificationTask(task).commit();
    String res = TestUtilities.copyFileToString(testFile);
    System.err.println(res);
    assertEquals(golden, res);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:61,代码来源:BodyStatementTest.java


注:本文中的org.netbeans.api.java.source.JavaSource.forFileObject方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。