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


Java JavaSource类代码示例

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


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

示例1: performUseSuperType

import org.netbeans.api.java.source.JavaSource; //导入依赖的package包/类
private void performUseSuperType(FileObject source, final int position, Problem... expectedProblems) throws Exception {
    final UseSuperTypeRefactoring[] r = new UseSuperTypeRefactoring[1];

    JavaSource.forFileObject(source).runUserActionTask(new Task<CompilationController>() {

        @Override
        public void run(CompilationController parameter) throws Exception {
            parameter.toPhase(JavaSource.Phase.RESOLVED);
            CompilationUnitTree cut = parameter.getCompilationUnit();

            ClassTree classTree = (ClassTree) cut.getTypeDecls().get(0);
            TreePath tp = TreePath.getPath(cut, classTree);
            r[0] = new UseSuperTypeRefactoring(TreePathHandle.create(tp, parameter));
            r[0].setTargetSuperType(r[0].getCandidateSuperTypes()[position]);
        }
    }, true);

    RefactoringSession rs = RefactoringSession.create("Session");
    List<Problem> problems = new LinkedList<Problem>();

    addAllProblems(problems, r[0].preCheck());
    addAllProblems(problems, r[0].prepare(rs));
    addAllProblems(problems, rs.doRefactoring(true));

    assertProblems(Arrays.asList(expectedProblems), problems);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:27,代码来源:UseSuperTypeTest.java

示例2: computeMethodArguments

import org.netbeans.api.java.source.JavaSource; //导入依赖的package包/类
public static MethodArgument[] computeMethodArguments(CompilationController ci,
                                                      EditorContext.Operation operation,
                                                      ASTOperationCreationDelegate opCreationDelegate)
                                                        throws IOException {
    EditorContext.MethodArgument args[];
    if (!PreferredCCParser.toPhase(ci, JavaSource.Phase.RESOLVED, LOG)) {
        return null;
    }
    int offset = operation.getMethodEndPosition().getOffset();
    Scope scope = ci.getTreeUtilities().scopeFor(offset);
    Element method = scope.getEnclosingMethod();
    if (method == null) {
        return null;
    }
    Tree methodTree = ci.getTrees().getTree(method);
    CompilationUnitTree cu = ci.getCompilationUnit();
    MethodArgumentsScanner scanner =
            new MethodArgumentsScanner(offset, cu, ci.getTrees().getSourcePositions(), true,
                                       opCreationDelegate);
    args = methodTree.accept(scanner, null);
    args = scanner.getArguments();
    return args;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:24,代码来源:EditorContextSupport.java

示例3: runWhenScanFinishedReallyLazy

import org.netbeans.api.java.source.JavaSource; //导入依赖的package包/类
private static Future<Void> runWhenScanFinishedReallyLazy(final FileObject fo,
                                                          final Task<CompilationController> task,
                                                          final boolean shared) throws IOException {
    return scanReallyLazy(new ScanRunnable<IOException>(IOException.class) {
        @Override
        public void run(Future<Void>[] resultPtr, IOException[] excPtr) {
            JavaSource js = JavaSource.forFileObject(fo);
            if (js == null) {
                return ;
            }
            try {
                js.runUserActionTask(task, shared);
            } catch (IOException ex) {
                synchronized (resultPtr) {
                    excPtr[0] = ex;
                }
            }
        }
    });
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:21,代码来源:EditorContextSupport.java

示例4: get

import org.netbeans.api.java.source.JavaSource; //导入依赖的package包/类
static synchronized JavaSemanticTokenList get(FileObject file) {
    if (!JavaTokenListProvider.ENABLE_SEMANTIC_TOKEN_LIST) {
        return null;
    }
    
    JavaSource js = JavaSource.forFileObject(file);
    
    if (js == null) {
        return null;
    }
    
    JavaSemanticTokenList l = js2TokenList.get(file);
    
    if (l == null) {
        js2TokenList.put(file, l = new JavaSemanticTokenList());
    }
    
    return l;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:20,代码来源:JavaSemanticTokenList.java

示例5: test130479

import org.netbeans.api.java.source.JavaSource; //导入依赖的package包/类
public void test130479() throws IOException {
    testFile = getFile(getSourceDir(), getSourcePckg() + "ImportsTest130479.java");
    JavaSource src = getJavaSource(testFile);
    Task<WorkingCopy> task = new Task<WorkingCopy>() {

        public void run(WorkingCopy workingCopy) throws IOException {
            workingCopy.toPhase(Phase.RESOLVED);
            CompilationUnitTree cut = workingCopy.getCompilationUnit();
            TreeMaker make = workingCopy.getTreeMaker();
            ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
            MethodTree method = (MethodTree) clazz.getMembers().get(2);
            BlockTree block = method.getBody();
            final TypeElement foo = workingCopy.getElements().getTypeElement("org.netbeans.test.codegen.ImportsTest130479.Foo");
            final TypeElement clazzType = workingCopy.getElements().getTypeElement("java.lang.Class");
            TypeMirror tm = workingCopy.getTypes().getDeclaredType(clazzType, workingCopy.getTypes().erasure(foo.asType()));
            assertNotNull(foo);
            Tree type = make.Type(tm);
            VariableTree vt = make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), "test", type, null);
            workingCopy.rewrite(block, make.addBlockStatement(block, vt));
        }
    };
    src.runModificationTask(task).commit();
    assertFiles("testImports130479.pass");
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:25,代码来源:ImportAnalysisTest.java

示例6: testCreateOuterClass

import org.netbeans.api.java.source.JavaSource; //导入依赖的package包/类
/**
 * Tests parsing of outer class.
 */
public void testCreateOuterClass() throws IOException {
    EditorOperator editor = getDefaultEditor();
    try {
        editor.insert("class Outer { }\n", 53, 1);
        JavaSource js = JavaSource.forFileObject(testFileObject);
        FileWriter fw = new FileWriter(getRefFile(1));
        FileOutputStream fos = new FileOutputStream(getRefFile(1));
        PrintStream ps = new PrintStream(fos);
        dumpStructure(js, ps, editor.getText());
        compareGoldenFile(1);
        editor.deleteLine(53);
        fos = new FileOutputStream(getRefFile(2));
        ps = new PrintStream(fos);
        dumpStructure(js, ps, editor.getText());
        compareGoldenFile(2);
    } finally {
        editor.closeDiscard();
    }

}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:24,代码来源:ParserTest.java

示例7: testAddImport17

import org.netbeans.api.java.source.JavaSource; //导入依赖的package包/类
public void testAddImport17() throws IOException {
    testFile = getFile(getSourceDir(), getSourcePckg() + "ImportsTest6.java");
    JavaSource src = getJavaSource(testFile);
    Task<WorkingCopy> task = new Task<WorkingCopy>() {

        public void run(WorkingCopy workingCopy) throws IOException {
            workingCopy.toPhase(Phase.RESOLVED);
            CompilationUnitTree cut = workingCopy.getCompilationUnit();
            TreeMaker make = workingCopy.getTreeMaker();
            ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
            MethodTree node = (MethodTree) clazz.getMembers().get(0);
            BlockTree body = node.getBody();
            List<StatementTree> stats = new ArrayList<StatementTree>();
            for (StatementTree st : body.getStatements()) {
                stats.add(st);
            }
            TypeElement list = workingCopy.getElements().getTypeElement("java.lang.String");
            Types types = workingCopy.getTypes();
            TypeMirror tm = types.getArrayType(types.erasure(list.asType()));
            stats.add(make.Variable(make.Modifiers(Collections.<Modifier>emptySet()), "s", make.Type(tm), null));
            workingCopy.rewrite(body, make.Block(stats, false));
        }
    };
    src.runModificationTask(task).commit();
    assertFiles("testAddImport17.pass");
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:27,代码来源:ImportAnalysisTest.java

示例8: getClasses

import org.netbeans.api.java.source.JavaSource; //导入依赖的package包/类
@Override
public Collection<SourceClassInfo> getClasses() {
    final List<SourceClassInfo> clzs = new ArrayList<SourceClassInfo>();
    
    ParsingUtils.invokeScanSensitiveTask(cpInfo, new ScanSensitiveTask<CompilationController> () {
        @Override
        public void run(CompilationController cc)
                throws Exception {
            cc.toPhase(JavaSource.Phase.PARSED);

            PackageElement pelem = cc.getElements().getPackageElement(getSimpleName());

            if (pelem != null) {
                for (TypeElement type : ElementFilter.typesIn(pelem.getEnclosedElements())) {
                    if ((type.getKind() == ElementKind.CLASS) || (type.getKind() == ElementKind.ENUM)) {
                        clzs.add(new JavacClassInfo(ElementHandle.create(type), indexInfo));
                    }
                }
            } else {
                LOGGER.log(Level.FINEST, "Package name {0} resulted into a NULL element", getBinaryName()); // NOI18N
            }
        }
    });

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

示例9: implement

import org.netbeans.api.java.source.JavaSource; //导入依赖的package包/类
@Override
public ChangeInfo implement() throws Exception {
    final FileObject file = handle.getFileObject();
    final JTextComponent comp = EditorRegistry.lastFocusedComponent();
    if (file != null && file == getFileObject(comp)) {
        final int[] pos = new int[]{-1};
        JavaSource.forFileObject(file).runUserActionTask(new Task<CompilationController>(){
            @Override
            public void run(CompilationController info) throws Exception {
                info.toPhase(JavaSource.Phase.PARSED);
                final TreePath tp = handle.resolve(info);
                if (tp != null && tp.getLeaf().getKind() == Tree.Kind.VARIABLE) {
                    pos[0] = (int) info.getTrees().getSourcePositions().getEndPosition(
                            tp.getCompilationUnit(),
                            ((VariableTree)tp.getLeaf()).getType()) + 1;
                }
            }
        }, true);
        invokeRefactoring (comp, pos[0]);
    }
    return null;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:23,代码来源:FieldEncapsulation.java

示例10: testCreateField

import org.netbeans.api.java.source.JavaSource; //导入依赖的package包/类
/**
 * Tests parsing of a field.
 */
public void testCreateField() throws IOException {
    EditorOperator editor = getDefaultEditor();
    try {
        editor.insert("String field;\n", 48, 1);
        JavaSource js = JavaSource.forFileObject(testFileObject);
        FileWriter fw = new FileWriter(getRefFile(1));
        FileOutputStream fos = new FileOutputStream(getRefFile(1));
        PrintStream ps = new PrintStream(fos);
        dumpStructure(js, ps, editor.getText());
        compareGoldenFile(1);
        editor.deleteLine(48);
        fos = new FileOutputStream(getRefFile(2));
        ps = new PrintStream(fos);
        dumpStructure(js, ps, editor.getText());
        compareGoldenFile(2);
    } finally {
        editor.closeDiscard();
    }

}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:24,代码来源:ParserTest.java

示例11: findAndOpenJavaClass

import org.netbeans.api.java.source.JavaSource; //导入依赖的package包/类
public static void findAndOpenJavaClass(final String classBinaryName, Document doc) {
    final JavaSource js = getJavaSource(doc);
    if (js != null) {
        try {
            js.runUserActionTask(new Task<CompilationController>() {

                public void run(CompilationController cc) throws Exception {
                    boolean opened = false;
                    TypeElement element = findClassElementByBinaryName(classBinaryName, cc);
                    if (element != null) {
                        opened = ElementOpen.open(js.getClasspathInfo(), element);
                    }
                    if (!opened) {
                        String msg = NbBundle.getMessage(HibernateEditorUtil.class, "LBL_SourceNotFound", classBinaryName);
                        StatusDisplayer.getDefault().setStatusText(msg);
                    }
                }
                }, false);
        } catch (IOException ex) {
            Logger.getLogger("global").log(Level.SEVERE, ex.getMessage(), ex);
        }
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:24,代码来源:HibernateEditorUtil.java

示例12: testAnnotation187551c

import org.netbeans.api.java.source.JavaSource; //导入依赖的package包/类
public void testAnnotation187551c() throws Exception {
    testFile = new File(getWorkDir(), "Test.java");
    TestUtilities.copyStringToFile(testFile,
        "\n" +
        "class Test {\n" +
        "}\n"
        );
    String golden =
        "\n" +
        "@interface Foo {\n" +
        "}\n";

    JavaSource src = getJavaSource(testFile);
    Task task = new Task<WorkingCopy>() {

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

            for (Tree typeDecl : cut.getTypeDecls()) {
                // ensure that it is correct type declaration, i.e. class
                if (TreeUtilities.CLASS_TREE_KINDS.contains(typeDecl.getKind())) {
                    ClassTree clazz = (ClassTree) typeDecl;
                    ClassTree copy = make.AnnotationType(clazz.getModifiers(), "Foo", clazz.getMembers());
                    workingCopy.rewrite(typeDecl, copy);
                }
            }
        }

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

示例13: createRestConnectionFile

import org.netbeans.api.java.source.JavaSource; //导入依赖的package包/类
protected void createRestConnectionFile(Project project) throws IOException {
    SourceGroup[] srcGrps = SourceGroupSupport.getJavaSourceGroups(project);
    String pkg = REST_CONNECTION_PACKAGE;
    FileObject targetFolder = SourceGroupSupport.getFolderForPackage(srcGrps[0],pkg , true);
    JavaSourceHelper.createJavaSource(REST_CONNECTION_TEMPLATE, targetFolder, pkg, REST_CONNECTION);
    String restResponseTemplate = REST_RESPONSE_TEMPLATE;
    JavaSource restResponseJS = JavaSourceHelper.createJavaSource(restResponseTemplate, targetFolder, pkg, REST_RESPONSE);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:9,代码来源:CustomClientPojoCodeGenerator.java

示例14: shouldHandle

import org.netbeans.api.java.source.JavaSource; //导入依赖的package包/类
/**
 * Checks whether the given <code>refactoringSource</code> should be handled by
 * this refactoring. Override in subclasses as needed, the
 * default implementation returns true if the given <code>refactoringSource</code>
 * is a class.
 * @param refactoringSource the object being refactored.
 * 
 * @return true if the <code>refactoringSource<code> represents a class that
 * should be handled by persistence.xml refactorings.
 */
protected boolean shouldHandle(FileObject refactoringSource) {
    final boolean[] result = new boolean[]{false};

    if (RefactoringUtil.isJavaFile(refactoringSource)) {
        JavaSource source = JavaSource.forFileObject(refactoringSource);
        try {
            source.runUserActionTask(new CancellableTask<CompilationController>() {

                public void cancel() {
                }

                public void run(CompilationController info) throws Exception {
                    info.toPhase(JavaSource.Phase.RESOLVED);
                    TreePathHandle treePathHandle = null;
                    CompilationUnitTree cut = info.getCompilationUnit();
                    if (!cut.getTypeDecls().isEmpty()) {
                        treePathHandle = TreePathHandle.create(TreePath.getPath(cut, cut.getTypeDecls().get(0)), info);
                    }
                    if (treePathHandle == null) {
                        result[0] = false;
                    } else {
                        Element element = treePathHandle.resolveElement(info);
                        if (element != null) {
                            result[0] = element.getKind() == ElementKind.CLASS;
                        }
                    }
                }
            }, true);
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }
    }

    return result[0];
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:46,代码来源:PersistenceXmlRefactoring.java

示例15: testGetTreePathHandle

import org.netbeans.api.java.source.JavaSource; //导入依赖的package包/类
/**
     * TODO, resolve fail
     * @throws Exception
     */
//    public void testResolveReferences() throws Exception {
//        EntityAssociationResolver resolver = new EntityAssociationResolver(getTreePathHandle("customer", ORDER), createModel());
//        List<EntityAnnotationReference> result = resolver.resolveReferences();
//        assertEquals(1, result.size());
//        EntityAnnotationReference reference = result.get(0);
//        assertEquals(EntityAssociationResolver.ONE_TO_MANY, reference.getAnnotation());
//        assertEquals("entities.Customer", reference.getEntity());
//        assertEquals(EntityAssociationResolver.MAPPED_BY, reference.getAttribute());
//        assertEquals("customer", reference.getAttributeValue());
//
//    }
    
    public void testGetTreePathHandle() throws Exception{
        final TreePathHandle handle  = RefactoringUtil.getTreePathHandle("orders", CUSTOMER, getJavaFile(CUSTOMER));
        JavaSource source = JavaSource.forFileObject(handle.getFileObject());
        source.runUserActionTask(new CancellableTask<CompilationController>(){
            
            public void cancel() {
            }
            
            public void run(CompilationController parameter) throws Exception {
                parameter.toPhase(JavaSource.Phase.RESOLVED);
                Element element = handle.resolveElement(parameter);
                assertEquals("orders", element.getSimpleName().toString());
                for (AnnotationMirror annotation : element.getAnnotationMirrors()){
                    assertEquals(EntityAssociationResolver.ONE_TO_MANY, annotation.getAnnotationType().toString());
                }
            }
        }, true);
        
    }
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:36,代码来源:EntityAssociationResolverTest.java


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