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


Java JavaSource.create方法代码示例

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


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

示例1: inferModuleNames

import org.netbeans.api.java.source.JavaSource; //导入方法依赖的package包/类
private Set<String> inferModuleNames(FileObject t, List<FileObject> jsources) throws IOException {
    ClasspathInfo cpi = ClasspathInfo.create(t);
    JavaSource src = JavaSource.create(cpi, jsources);
    Set<String> unresolved = new HashSet<>();
    src.runUserActionTask(new Task<CompilationController>() {
        @Override
        public void run(CompilationController parameter) throws Exception {
            parameter.toPhase(JavaSource.Phase.RESOLVED);
            Scanner s = new Scanner(parameter, unresolved);
            s.scan(parameter.getCompilationUnit(), null);
        }
    }, true);
    ClassPath searchPath = ClassPathSupport.createProxyClassPath(
            cpi.getClassPath(PathKind.MODULE_BOOT),
            cpi.getClassPath(PathKind.MODULE_COMPILE));
    Set<String> moduleNames = new HashSet<>();
    for (String fqn : unresolved) {
        findModuleNames(fqn, searchPath, moduleNames);
    }
    return moduleNames;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:22,代码来源:TemplateModuleDeclarator.java

示例2: testIssue247469

import org.netbeans.api.java.source.JavaSource; //导入方法依赖的package包/类
public void testIssue247469() throws IOException {
    final JavaPlatform jp = JavaPlatformManager.getDefault().getDefaultPlatform();
    assertNotNull(jp);
    final ClasspathInfo cpInfo = ClasspathInfo.create(jp.getBootstrapLibraries(), ClassPath.EMPTY, ClassPath.EMPTY);
    assertNotNull(cpInfo);
    final JavaSource js = JavaSource.create(cpInfo);
    js.runUserActionTask(new Task<CompilationController>() {
        @Override
        public void run(final CompilationController cc) throws Exception {
            final PackageElement packageElement = cc.getElements().getPackageElement("java.lang"); // NOI18N
            for (Element elem : packageElement.getEnclosedElements()) {
                if ("ProcessBuilder$1".equals(elem.getSimpleName().toString())) { // NOI18N
                    TypeElement te = (TypeElement) elem;
                    assertEquals(NestingKind.ANONYMOUS, te.getNestingKind());
                    break;
                }
            }
        }
    }, true);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:21,代码来源:CtSymArchiveTest.java

示例3: getJavaSource

import org.netbeans.api.java.source.JavaSource; //导入方法依赖的package包/类
public static JavaSource getJavaSource(FileObject fileObject) {
    if (fileObject == null) {
        return null;
    }
    Project project = FileOwnerQuery.getOwner(fileObject);
    if (project == null) {
        return null;
    }
    // XXX this only works correctly with projects with a single sourcepath,
    // but we don't plan to support another kind of projects anyway (what about Maven?).
    SourceGroup[] sourceGroups = ProjectUtils.getSources(project).getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
    for (SourceGroup sourceGroup : sourceGroups) {
        return JavaSource.create(ClasspathInfo.create(sourceGroup.getRootFolder()));
    }
    return null;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:17,代码来源:JavaUtils.java

示例4: getJavaSource

import org.netbeans.api.java.source.JavaSource; //导入方法依赖的package包/类
public static JavaSource getJavaSource(Document doc) {
    FileObject fileObject = NbEditorUtilities.getFileObject(doc);
    if (fileObject == null) {
        return null;
    }
    Project project = FileOwnerQuery.getOwner(fileObject);
    if (project == null) {
        return null;
    }
    // XXX this only works correctly with projects with a single sourcepath,
    // but we don't plan to support another kind of projects anyway (what about Maven?).
    // mkleint: Maven has just one sourceroot for java sources, the config files are placed under
    // different source root though. JavaProjectConstants.SOURCES_TYPE_RESOURCES
    SourceGroup[] sourceGroups = ProjectUtils.getSources(project).getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
    for (SourceGroup sourceGroup : sourceGroups) {
        return JavaSource.create(ClasspathInfo.create(sourceGroup.getRootFolder()));
    }
    return null;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:20,代码来源:HibernateEditorUtil.java

示例5: verifySourceRoot

import org.netbeans.api.java.source.JavaSource; //导入方法依赖的package包/类
private static void verifySourceRoot(
        @NonNull final FileObject root,
        @NullAllowed final Filter filter,
        @NonNull final Collection<? extends ProfileSupport.Violation> violations,
        @NonNull final Result result) {
    try {
        final ClasspathInfo cpInfo = ClasspathInfo.create(root);
        final Map<FileObject,Collection<ProfileSupport.Violation>> violationsByFiles =
            new HashMap<>();
        final JavaSource js = JavaSource.create(
            cpInfo,
            violationsToFileObjects(violations, filter, violationsByFiles));
        if (js != null) {
            js.runUserActionTask(
                new Task<CompilationController>(){
                    @Override
                    public void run(@NonNull final CompilationController cc) throws Exception {
                        cc.toPhase(JavaSource.Phase.RESOLVED);
                        final FileObject currentFile = cc.getFileObject();
                        if (currentFile != null) {
                            final FindPosScanner fps = new FindPosScanner(
                                    currentFile,
                                    cc.getTrees(),
                                    cc.getElements(),
                                    cc.getTreeUtilities(),
                                    violationsByFiles.get(currentFile),
                                    result);
                            fps.scan(cc.getCompilationUnit(), null);
                        }
                    }
                },
                true);
        }
    } catch (IOException ioe) {
        Exceptions.printStackTrace(ioe);
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:38,代码来源:ProfilesAnalyzer.java

示例6: implement

import org.netbeans.api.java.source.JavaSource; //导入方法依赖的package包/类
public ChangeInfo implement() throws Exception {
    //use the original cp-info so it is "sure" that the target can be resolved:
    JavaSource js = JavaSource.create(cpInfo, targetFile);
    
    ModificationResult diff = js.runModificationTask(new Task<WorkingCopy>() {

        public void run(final WorkingCopy working) throws IOException {
            working.toPhase(Phase.RESOLVED);
            TypeElement targetType = target.resolve(working);
            
            if (targetType == null) {
                ErrorHintsProvider.LOG.log(Level.INFO, "Cannot resolve target."); // NOI18N
                return;
            }
            
            TreePath targetTree = working.getTrees().getPath(targetType);
            
            if (targetTree == null) {
                ErrorHintsProvider.LOG.log(Level.INFO, "Cannot resolve target tree: " + targetType.getQualifiedName() + "."); // NOI18N
                return;
            }
            
            TreeMaker make = working.getTreeMaker();
            MethodTree constr = make.Method(make.Modifiers(EnumSet.of(Modifier.PUBLIC)), "<init>", null, Collections.<TypeParameterTree>emptyList(), Collections.<VariableTree>emptyList(), Collections.<ExpressionTree>emptyList(), "{}" /*XXX*/, null); // NOI18N
            ClassTree innerClass = make.Class(make.Modifiers(modifiers), name, Collections.<TypeParameterTree>emptyList(), null, Collections.<Tree>emptyList(), Collections.<Tree>singletonList(constr));
            
            innerClass = createConstructor(working, new TreePath(targetTree, innerClass));
            
            working.rewrite(targetTree.getLeaf(), GeneratorUtilities.get(working).insertClassMember((ClassTree)targetTree.getLeaf(), innerClass));
        }
    });
    
    return Utilities.commitAndComputeChangeInfo(targetFile, diff, null);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:35,代码来源:CreateClassFix.java

示例7: invokeByJavaSource

import org.netbeans.api.java.source.JavaSource; //导入方法依赖的package包/类
private static void invokeByJavaSource (
        @NonNull final Runnable runnable) throws IOException {
    Parameters.notNull("runnable", runnable);   //NOI18N
    final ClasspathInfo info = ClasspathInfo.create(JavaPlatform.getDefault().getBootstrapLibraries(),
        ClassPathSupport.createClassPath(new URL[0]),
        ClassPathSupport.createClassPath(new URL[0]));
    final JavaSource js = JavaSource.create(info);
    js.runWhenScanFinished((final CompilationController controller) -> {
        runnable.run();
    }, true);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:12,代码来源:ActionProviderSupport.java

示例8: getJavaSource

import org.netbeans.api.java.source.JavaSource; //导入方法依赖的package包/类
private static JavaSource getJavaSource(SourcePathProvider sp) {
    String[] roots = sp.getOriginalSourceRoots();
    List<FileObject> sourcePathFiles = new ArrayList<FileObject>();
    for (String root : roots) {
        FileObject fo = FileUtil.toFileObject (new java.io.File(root));
        if (fo != null && FileUtil.isArchiveFile (fo)) {
            fo = FileUtil.getArchiveRoot (fo);
        }
        sourcePathFiles.add(fo);
    }
    ClassPath bootPath = ClassPathSupport.createClassPath(new FileObject[] {});
    ClassPath classPath = ClassPathSupport.createClassPath(new FileObject[] {});
    ClassPath sourcePath = ClassPathSupport.createClassPath(sourcePathFiles.toArray(new FileObject[] {}));
    return JavaSource.create(ClasspathInfo.create(bootPath, classPath, sourcePath), new FileObject[] {});
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:16,代码来源:PreferredCCParser.java

示例9: createSource

import org.netbeans.api.java.source.JavaSource; //导入方法依赖的package包/类
@NonNull
private static JavaSource createSource() {
    final ClasspathInfo cpInfo = ClasspathInfo.create(
            ClassPath.EMPTY,
            ClassPath.EMPTY,
            ClassPath.EMPTY);
    final JavaSource js = JavaSource.create(cpInfo);
    return js;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:10,代码来源:J2SEActionProvider.java

示例10: test157760b

import org.netbeans.api.java.source.JavaSource; //导入方法依赖的package包/类
public void test157760b() throws Exception {
    testFile = new File(getWorkDir(), "package-info.java");
    TestUtilities.copyStringToFile(testFile, "@AA\[email protected]\[email protected]\npackage foo;");

    FileObject testSourceFO = FileUtil.toFileObject(testFile);
    assertNotNull(testSourceFO);
    ClassPath sourcePath = ClassPath.getClassPath(testSourceFO, ClassPath.SOURCE);
    assertNotNull(sourcePath);
    FileObject[] roots = sourcePath.getRoots();
    assertEquals(1, roots.length);
    final FileObject sourceRoot = roots[0];
    assertNotNull(sourceRoot);
    ClassPath compilePath = ClassPath.getClassPath(testSourceFO, ClassPath.COMPILE);
    assertNotNull(compilePath);
    ClassPath bootPath = ClassPath.getClassPath(testSourceFO, ClassPath.BOOT);
    assertNotNull(bootPath);
    ClasspathInfo cpInfo = ClasspathInfo.create(bootPath, compilePath, sourcePath);

    String golden = "@EE\[email protected]\[email protected]\npackage foo;";

    JavaSource javaSource = JavaSource.create(cpInfo, FileUtil.toFileObject(testFile));

    Task<WorkingCopy> task = new Task<WorkingCopy>() {
        public void run(WorkingCopy workingCopy) throws Exception {
            workingCopy.toPhase(JavaSource.Phase.RESOLVED);
            TreeMaker make = workingCopy.getTreeMaker();
            CompilationUnitTree nue = make.addPackageAnnotation(workingCopy.getCompilationUnit(), make.Annotation(make.Identifier("DD"), Collections.<ExpressionTree>emptyList()));
            nue = make.insertPackageAnnotation(nue, 0, make.Annotation(make.Identifier("EE"), Collections.<ExpressionTree>emptyList()));
            nue = make.removePackageAnnotation(nue, 1);
            nue = make.removePackageAnnotation(nue, workingCopy.getCompilationUnit().getPackageAnnotations().get(1));
            workingCopy.rewrite(workingCopy.getCompilationUnit(), nue);
        }
    };
    ModificationResult result = javaSource.runModificationTask(task);
    result.commit();
    String res = TestUtilities.copyFileToString(testFile);
    System.err.println(res);
    assertEquals(golden, res);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:40,代码来源:CompilationUnitTest.java

示例11: waitScanningFinished

import org.netbeans.api.java.source.JavaSource; //导入方法依赖的package包/类
/**
 * Wait until scanning is finished.
 *
 * @param projectDir File pointing to project root or root of several
 * projects
 * @throws java.lang.Exception
 */
public static void waitScanningFinished(File projectDir) throws Exception {
    JavaSource src = JavaSource.create(ClasspathInfo.create(projectDir));
    src.runWhenScanFinished(new Task<CompilationController>() {

        @Override()
        public void run(CompilationController controller) throws Exception {
            controller.toPhase(JavaSource.Phase.RESOLVED);
        }
    }, false).get();
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:18,代码来源:Utilities.java

示例12: computeCandidatesEx

import org.netbeans.api.java.source.JavaSource; //导入方法依赖的package包/类
private ComputeImports computeCandidatesEx(Set<String> forcedUnresolved) {
    ComputeImports cache = (ComputeImports)info.getCachedValue(IMPORT_CANDIDATES_KEY);
    if (cache != null) {
        return cache;
    }
    boolean modules = false;
    
    if (info.getSourceVersion().compareTo(SourceVersion.RELEASE_9) <= 0) {
        if (info.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.SOURCE).findResource("module-info.java") != null) {
            modules = true;
        }
    }
    
    if (modules) {
        ClasspathInfo cpInfo = info.getClasspathInfo();
        ClasspathInfo extraInfo = ClasspathInfo.create(
                ClassPathSupport.createProxyClassPath(
                        cpInfo.getClassPath(ClasspathInfo.PathKind.BOOT),
                        cpInfo.getClassPath(ClasspathInfo.PathKind.MODULE_BOOT)),
                ClassPathSupport.createProxyClassPath(
                        cpInfo.getClassPath(ClasspathInfo.PathKind.COMPILE),
                        cpInfo.getClassPath(ClasspathInfo.PathKind.MODULE_COMPILE),
                        cpInfo.getClassPath(ClasspathInfo.PathKind.MODULE_CLASS)),
                cpInfo.getClassPath(ClasspathInfo.PathKind.SOURCE));
        JavaSource src = JavaSource.create(extraInfo, info.getSnapshot().getSource().getFileObject());
        try {
            src.runUserActionTask(new Task<CompilationController>() {
                @Override
                public void run(CompilationController parameter) throws Exception {
                    allInfo = parameter;
                    parameter.toPhase(JavaSource.Phase.RESOLVED);
                    doComputeCandidates(forcedUnresolved);
                }
            }, true);
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }
    } else {
        allInfo = info;
        doComputeCandidates(forcedUnresolved);
    }
    info.putCachedValue(IMPORT_CANDIDATES_KEY, this, CacheClearPolicy.ON_CHANGE);
    return this;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:45,代码来源:ComputeImports.java

示例13: parse

import org.netbeans.api.java.source.JavaSource; //导入方法依赖的package包/类
private CompilationInfo parse(FileObject file) throws DataObjectNotFoundException, IllegalArgumentException, IOException {
    DataObject od = DataObject.find(file);
    EditorCookie ec = od.getLookup().lookup(EditorCookie.class);

    assertNotNull(ec);

    Document doc = ec.openDocument();

    doc.putProperty(Language.class, JavaTokenId.language());
    doc.putProperty("mimeType", "text/x-java");

    JavaSource js = JavaSource.create(ClasspathInfo.create(file), file);

    assertNotNull("found JavaSource for " + file, js);

    final DeadlockTask bt = new DeadlockTask(Phase.RESOLVED);

    js.runUserActionTask(bt, true);
    
    return bt.info;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:22,代码来源:HintTest.java

示例14: query

import org.netbeans.api.java.source.JavaSource; //导入方法依赖的package包/类
@Override
protected void query(CompletionResultSet resultSet, Document doc, int caretOffset) {
    {
        try {
            this.caretOffset = caretOffset;
            {
                results = null;
                anchorOffset = -1;
                Source source = Source.create(doc);
                PUDataObject puObject;
                JavaSource js;
                if (source != null) {
                    JPQLEditorTopComponent tc = (JPQLEditorTopComponent) NbEditorUtilities.getTopComponent(component);
                    puObject = tc.getDataObject();
                    final FileObject pXml = puObject.getPrimaryFile();
                    final Project project = FileOwnerQuery.getOwner(pXml);
                    if (project == null) {
                        return;
                    }
                    // XXX this only works correctly with projects with a single sourcepath,
                    // but we don't plan to support another kind of projects anyway (what about Maven?).
                    // mkleint: Maven has just one sourceroot for java sources, the config files are placed under
                    // different source root though. JavaProjectConstants.SOURCES_TYPE_RESOURCES
                    SourceGroup[] sourceGroups = ProjectUtils.getSources(project).getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
                    js = JavaSource.create(ClasspathInfo.create(sourceGroups[0].getRootFolder()));

                    js.runUserActionTask(new org.netbeans.api.java.source.Task<CompilationController>() {
                        @Override
                        public void run(CompilationController parameter) throws Exception {
                            JPACodeCompletionQuery.this.run(parameter, project, pXml);
                        }
                    }, false);
                    if ((queryType & COMPLETION_QUERY_TYPE) != 0) {
                        if (results != null) {
                            resultSet.addAllItems(results);
                        }
                        resultSet.setHasAdditionalItems(hasAdditionalItems > 0);
                        if (hasAdditionalItems == 1) {
                            resultSet.setHasAdditionalItemsText(NbBundle.getMessage(JPQLEditorCodeCompletionProvider.class, "JCP-imported-items")); //NOI18N
                        }
                        if (hasAdditionalItems == 2) {
                            resultSet.setHasAdditionalItemsText(NbBundle.getMessage(JPQLEditorCodeCompletionProvider.class, "JCP-instance-members")); //NOI18N
                        }
                    }
                    if (anchorOffset > -1) {
                        resultSet.setAnchorOffset(anchorOffset);
                    }
                }
            }
        } catch (Exception e) {
            Exceptions.printStackTrace(e);
        } finally {
            resultSet.finish();
        }
    }

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

示例15: implement

import org.netbeans.api.java.source.JavaSource; //导入方法依赖的package包/类
public ChangeInfo implement() throws Exception {
        JavaSource js = JavaSource.create(cpInfo, targetFile);

        ModificationResult diff = js.runModificationTask(new Task<WorkingCopy>() {
            public void run(final WorkingCopy working) throws IOException {
                working.toPhase(Phase.RESOLVED);
                TypeElement targetType = target.resolve(working);

                if (targetType == null) {
                    ErrorHintsProvider.LOG.log(Level.INFO, "Cannot resolve target."); // NOI18N
                    return;
                }

                ClassTree targetTree = working.getTrees().getTree(targetType);

                if (targetTree == null) {
                    ErrorHintsProvider.LOG.log(Level.INFO, "Cannot resolve target tree: " + targetType.getQualifiedName() + "."); // NOI18N
                    return;
                }
                
                TypeMirror proposedType = CreateEnumConstant.this.proposedType.resolve(working);
                TreeMaker make = working.getTreeMaker();

                int mods = 1<<14; //XXX enum flag. Creation of enum constant should be part of TreeMaker
                ModifiersTree modds = make.Modifiers(mods, Collections.<AnnotationTree>emptyList());
                VariableTree var = make.Variable(modds, name, make.Type(proposedType), null);

                List<? extends Tree> members = targetTree.getMembers();
                ArrayList<Tree> newMembers = new ArrayList<Tree>(members);
                int pos = -1;
                for (Iterator<? extends Tree> it = members.iterator(); it.hasNext();) {
                    Tree t = it.next();
                    if (t.getKind() == Kind.VARIABLE && working.getTreeUtilities().isEnumConstant((VariableTree)t) ) {
                        pos = members.indexOf(t);
                    }
                }

                newMembers.add(pos+1, var);
                ClassTree enumm = make.Enum(targetTree.getModifiers(), targetTree.getSimpleName(), targetTree.getImplementsClause(), newMembers);
//                ClassTree decl = GeneratorUtilities.get(working).insertClassMember(targetTree, var);
                working.rewrite(targetTree, enumm);
            }
        });

        return Utilities.commitAndComputeChangeInfo(targetFile, diff, null);
    }
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:47,代码来源:CreateEnumConstant.java


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