本文整理汇总了Java中org.netbeans.api.java.source.CompilationInfo.getCompilationUnit方法的典型用法代码示例。如果您正苦于以下问题:Java CompilationInfo.getCompilationUnit方法的具体用法?Java CompilationInfo.getCompilationUnit怎么用?Java CompilationInfo.getCompilationUnit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.netbeans.api.java.source.CompilationInfo
的用法示例。
在下文中一共展示了CompilationInfo.getCompilationUnit方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: findMainClass
import org.netbeans.api.java.source.CompilationInfo; //导入方法依赖的package包/类
/**
* Finds a main class.
*
* @param compInfo defines scope in which the class is to be found
* @param className name of the class to be found
* @return the found class; or <code>null</code> if the class was not
* found (e.g. because of a broken source file)
*/
public static ClassTree findMainClass(final CompilationInfo compInfo) {
final String className = compInfo.getFileObject().getName();
CompilationUnitTree compUnitTree = compInfo.getCompilationUnit();
String shortClassName = getSimpleName(className);
for (Tree typeDecl : compUnitTree.getTypeDecls()) {
if (TreeUtilities.CLASS_TREE_KINDS.contains(typeDecl.getKind())) {
ClassTree clazz = (ClassTree) typeDecl;
if (clazz.getSimpleName().toString().equals(shortClassName)) {
return clazz;
}
}
}
return null;
}
示例2: DiffContext
import org.netbeans.api.java.source.CompilationInfo; //导入方法依赖的package包/类
public DiffContext(CompilationInfo copy, Set<Tree> syntheticTrees) {
this.tokenSequence = copy.getTokenHierarchy().tokenSequence(JavaTokenId.language());
this.mainCode = this.origText = copy.getText();
this.style = getCodeStyle(copy);
this.context = JavaSourceAccessor.getINSTANCE().getJavacTask(copy).getContext();
this.mainUnit = this.origUnit = (JCCompilationUnit) copy.getCompilationUnit();
this.trees = copy.getTrees();
this.doc = copy.getSnapshot().getSource().getDocument(false); //TODO: true or false?
this.positionConverter = copy.getPositionConverter();
this.file = copy.getFileObject();
this.syntheticTrees = syntheticTrees;
this.textLength = copy.getSnapshot() == null ? Integer.MAX_VALUE : copy.getSnapshot().getOriginalOffset(copy.getSnapshot().getText().length());
this.blockSequences = new BlockSequences(this.tokenSequence, doc, textLength);
this.forceInitialComment = false;
}
示例3: testSynteticDefaultConstructor
import org.netbeans.api.java.source.CompilationInfo; //导入方法依赖的package包/类
public void testSynteticDefaultConstructor() throws Exception {
performTest("SynteticDefaultConstructor");
source.runModificationTask(new Task<WorkingCopy>() {
public void run(WorkingCopy copy) throws IOException {
copy.toPhase(Phase.RESOLVED);
ClassTree topLevel = findTopLevelClass(copy);
SourceUtilsTestUtil2.run(copy, new AddSimpleField(), topLevel);
}
}).commit();
JavaSourceAccessor.getINSTANCE().revalidate(source);
CompilationInfo check = SourceUtilsTestUtil.getCompilationInfo(source, Phase.RESOLVED);
CompilationUnitTree cu = check.getCompilationUnit();
assertEquals(check.getDiagnostics().toString(), 0, check.getDiagnostics().size());
ClassTree newTopLevel = findTopLevelClass(check);
Element clazz = check.getTrees().getElement(TreePath.getPath(cu, newTopLevel));
Element pack = clazz.getEnclosingElement();
assertEquals(ElementKind.PACKAGE, pack.getKind());
assertEquals("test", ((PackageElement) pack).getQualifiedName().toString());
assertEquals(clazz.getEnclosedElements().toString(), 2 + 1/*syntetic default constructor*/, clazz.getEnclosedElements().size());
}
示例4: testEmptyClass
import org.netbeans.api.java.source.CompilationInfo; //导入方法依赖的package包/类
public void testEmptyClass() throws Exception {
performTest("EmptyClass");
source.runModificationTask(new Task<WorkingCopy>() {
public void run(WorkingCopy copy) throws IOException {
copy.toPhase(Phase.RESOLVED);
ClassTree topLevel = findTopLevelClass(copy);
SourceUtilsTestUtil2.run(copy, new AddSimpleField(), topLevel);
}
}).commit();
JavaSourceAccessor.getINSTANCE().revalidate(source);
CompilationInfo check = SourceUtilsTestUtil.getCompilationInfo(source, Phase.RESOLVED);
CompilationUnitTree cu = check.getCompilationUnit();
assertEquals(check.getDiagnostics().toString(), 0, check.getDiagnostics().size());
ClassTree newTopLevel = findTopLevelClass(check);
Element clazz = check.getTrees().getElement(TreePath.getPath(cu, newTopLevel));
Element pack = clazz.getEnclosingElement();
assertEquals(ElementKind.PACKAGE, pack.getKind());
assertEquals("test", ((PackageElement) pack).getQualifiedName().toString());
assertEquals(clazz.getEnclosedElements().toString(), 1 + 1/*syntetic default constructor*/, clazz.getEnclosedElements().size());
}
示例5: match
import org.netbeans.api.java.source.CompilationInfo; //导入方法依赖的package包/类
@Override
public Map<String, Collection<TreePath>> match(CompilationInfo info, AtomicBoolean cancel, TreePath toSearch, BulkPattern pattern, Map<String, Long> timeLog) {
Parameters.notNull("info", info);
Map<String, Collection<TreePath>> result = new HashMap<String, Collection<TreePath>>();
TreePath topLevel = new TreePath(info.getCompilationUnit());
for (Entry<Tree, String> e : ((BulkPatternImpl) pattern).pattern2Code.entrySet()) {
for (Occurrence od : Matcher.create(info).setCancel(new AtomicBoolean()).setUntypedMatching().setCancel(cancel).match(Pattern.createPatternWithFreeVariables(new TreePath(topLevel, e.getKey()), Collections.<String, TypeMirror>emptyMap()))) {
Collection<TreePath> c = result.get(e.getValue());
if (c == null) {
result.put(e.getValue(), c = new LinkedList<TreePath>());
}
c.add(od.getOccurrenceRoot());
}
}
return result;
}
示例6: run
import org.netbeans.api.java.source.CompilationInfo; //导入方法依赖的package包/类
public List<Fix> run(CompilationInfo compilationInfo, String diagnosticKey, int offset, TreePath treePath, Data<Void> data) {
Tree leaf = treePath.getLeaf();
if (leaf.getKind() == Kind.IDENTIFIER) {
Element el = compilationInfo.getTrees().getElement(treePath);
if (el == null) {
return null;
}
TreePath declaration = compilationInfo.getTrees().getPath(el);
// do not offer any modifications for members in other CUs
if (declaration != null && declaration.getCompilationUnit() == compilationInfo.getCompilationUnit()) {
return Collections.singletonList((Fix) new FixImpl(compilationInfo.getFileObject(), el.getSimpleName().toString(), TreePathHandle.create(declaration, compilationInfo), fixDescription, type));
}
}
return Collections.<Fix>emptyList();
}
示例7: findExactStatement
import org.netbeans.api.java.source.CompilationInfo; //导入方法依赖的package包/类
private StatementTree findExactStatement(CompilationInfo info, BlockTree block, int offset, boolean start) {
if (offset == (-1)) return null;
SourcePositions sp = info.getTrees().getSourcePositions();
CompilationUnitTree cut = info.getCompilationUnit();
for (StatementTree t : block.getStatements()) {
long pos = start ? sp.getStartPosition(info.getCompilationUnit(), t) : sp.getEndPosition( cut, t);
if (offset == pos) {
return t;
}
}
return null;
}
示例8: testClassImplementingList
import org.netbeans.api.java.source.CompilationInfo; //导入方法依赖的package包/类
public void testClassImplementingList() throws Exception {
performTest("ClassImplementingList");
source.runModificationTask(new Task<WorkingCopy>() {
public void run(WorkingCopy copy) throws IOException {
copy.toPhase(Phase.RESOLVED);
ClassTree topLevel = findTopLevelClass(copy);
SourceUtilsTestUtil2.run(copy, new AddSimpleField(), topLevel);
}
}).commit();
JavaSourceAccessor.getINSTANCE().revalidate(source);
CompilationInfo check = SourceUtilsTestUtil.getCompilationInfo(source, Phase.RESOLVED);
CompilationUnitTree cu = check.getCompilationUnit();
assertEquals(check.getDiagnostics().toString(), 1, check.getDiagnostics().size());
assertEquals("compiler.err.does.not.override.abstract", check.getDiagnostics().get(0).getCode());
ClassTree newTopLevel = findTopLevelClass(check);
Element clazz = check.getTrees().getElement(TreePath.getPath(cu, newTopLevel));
Element pack = clazz.getEnclosingElement();
assertEquals(ElementKind.PACKAGE, pack.getKind());
assertEquals("test", ((PackageElement) pack).getQualifiedName().toString());
assertEquals(clazz.getEnclosedElements().toString(), 1 + 1/*syntetic default constructor*/, clazz.getEnclosedElements().size());
}
示例9: createHighlightImpl
import org.netbeans.api.java.source.CompilationInfo; //导入方法依赖的package包/类
private static Token<JavaTokenId> createHighlightImpl(CompilationInfo info, Document doc, TreePath tree) {
Tree leaf = tree.getLeaf();
SourcePositions positions = info.getTrees().getSourcePositions();
CompilationUnitTree cu = info.getCompilationUnit();
//XXX: do not use instanceof:
if (leaf instanceof MethodTree || leaf instanceof VariableTree || leaf instanceof ClassTree
|| leaf instanceof MemberSelectTree || leaf instanceof AnnotatedTypeTree || leaf instanceof MemberReferenceTree) {
return findIdentifierSpan(info, doc, tree);
}
int start = (int) positions.getStartPosition(cu, leaf);
int end = (int) positions.getEndPosition(cu, leaf);
if (start == Diagnostic.NOPOS || end == Diagnostic.NOPOS) {
return null;
}
TokenHierarchy<?> th = info.getTokenHierarchy();
TokenSequence<JavaTokenId> ts = th.tokenSequence(JavaTokenId.language());
if (ts.move(start) == Integer.MAX_VALUE) {
return null;
}
if (ts.moveNext()) {
Token<JavaTokenId> token = ts.token();
if (ts.offset() == start && token != null) {
final JavaTokenId id = token.id();
if (id == JavaTokenId.IDENTIFIER) {
return token;
}
if (id == JavaTokenId.THIS || id == JavaTokenId.SUPER) {
return ts.offsetToken();
}
}
}
return null;
}
示例10: PreconditionsChecker
import org.netbeans.api.java.source.CompilationInfo; //导入方法依赖的package包/类
public PreconditionsChecker(Tree forLoop, CompilationInfo workingCopy) {
if (forLoop.getKind() == Tree.Kind.ENHANCED_FOR_LOOP) {
this.isForLoop = true;
this.workingCopy = workingCopy;
this.hasUncaughtException = workingCopy.getTreeUtilities()
.getUncaughtExceptions(TreePath.getPath(workingCopy.getCompilationUnit(), forLoop)).stream().anyMatch(this::filterCheckedExceptions);
this.innerVariables = this.getInnerVariables(forLoop, workingCopy.getTrees());
this.visitor = new ForLoopTreeVisitor(this.innerVariables, workingCopy, new TreePath(workingCopy.getCompilationUnit()), (EnhancedForLoopTree) forLoop);
this.isIterable = this.isIterbale(((EnhancedForLoopTree) forLoop).getExpression());
visitor.scan(TreePath.getPath(workingCopy.getCompilationUnit(), forLoop), workingCopy.getTrees());
} else {
this.isForLoop = false;
}
}
示例11: getAllImportsOfKind
import org.netbeans.api.java.source.CompilationInfo; //导入方法依赖的package包/类
private static List<TreePathHandle> getAllImportsOfKind(CompilationInfo ci, ImportHintKind kind) {
//allow only default and samepackage
assert (kind == ImportHintKind.DEFAULT_PACKAGE || kind == ImportHintKind.SAME_PACKAGE);
CompilationUnitTree cut = ci.getCompilationUnit();
TreePath topLevel = new TreePath(cut);
List<TreePathHandle> result = new ArrayList<TreePathHandle>(3);
List<? extends ImportTree> imports = cut.getImports();
for (ImportTree it : imports) {
if (it.isStatic()) {
continue; // XXX
}
if (it.getQualifiedIdentifier() instanceof MemberSelectTree) {
MemberSelectTree ms = (MemberSelectTree) it.getQualifiedIdentifier();
if (kind == ImportHintKind.DEFAULT_PACKAGE) {
if (ms.getExpression().toString().equals(DEFAULT_PACKAGE)) {
result.add(TreePathHandle.create(new TreePath(topLevel, it), ci));
}
}
if (kind == ImportHintKind.SAME_PACKAGE) {
ExpressionTree packageName = cut.getPackageName();
if (packageName != null &&
ms.getExpression().toString().equals(packageName.toString())) {
result.add(TreePathHandle.create(new TreePath(topLevel, it), ci));
}
}
}
}
return result;
}
示例12: process
import org.netbeans.api.java.source.CompilationInfo; //导入方法依赖的package包/类
public Map<ElementHandle<? extends Element>, List<ElementDescription>> process(CompilationInfo info) {
IsOverriddenVisitor v = new IsOverriddenVisitor(info, cancel);
CompilationUnitTree unit = info.getCompilationUnit();
long startTime1 = System.currentTimeMillis();
v.scan(unit, null);
long endTime1 = System.currentTimeMillis();
Logger.getLogger("TIMER").log(Level.FINE, "Overridden Scanner", //NOI18N
new Object[] {info.getFileObject(), endTime1 - startTime1});
Map<ElementHandle<? extends Element>, List<ElementDescription>> result = new HashMap<ElementHandle<? extends Element>, List<ElementDescription>>();
for (ElementHandle<TypeElement> td : v.type2Declaration.keySet()) {
if (isCanceled())
return null;
Map<ElementHandle<ExecutableElement>, List<ElementDescription>> overrides = compute(info, td, cancel);
if (overrides != null) {
result.putAll(overrides);
}
}
if (isCanceled())
return null;
else
return result;
}
示例13: isInHeader
import org.netbeans.api.java.source.CompilationInfo; //导入方法依赖的package包/类
private static boolean isInHeader(CompilationInfo info, ClassTree tree, int offset) {
CompilationUnitTree cut = info.getCompilationUnit();
SourcePositions sp = info.getTrees().getSourcePositions();
long lastKnownOffsetInHeader = sp.getStartPosition(cut, tree);
List<? extends Tree> impls = tree.getImplementsClause();
List<? extends TypeParameterTree> typeparams;
if (impls != null && !impls.isEmpty()) {
lastKnownOffsetInHeader= sp.getEndPosition(cut, impls.get(impls.size() - 1));
} else if ((typeparams = tree.getTypeParameters()) != null && !typeparams.isEmpty()) {
lastKnownOffsetInHeader= sp.getEndPosition(cut, typeparams.get(typeparams.size() - 1));
} else if (tree.getExtendsClause() != null) {
lastKnownOffsetInHeader = sp.getEndPosition(cut, tree.getExtendsClause());
} 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) {
return offset < ts.offset();
}
}
return false;
}
示例14: Matcher
import org.netbeans.api.java.source.CompilationInfo; //导入方法依赖的package包/类
private Matcher(CompilationInfo info) {
this.info = info;
this.root = new TreePath(info.getCompilationUnit());
this.options.add(Options.ALLOW_GO_DEEPER);
}
示例15: process
import org.netbeans.api.java.source.CompilationInfo; //导入方法依赖的package包/类
public static Collection<TreePath> process(CompilationInfo info, AtomicBoolean cancel) {
Collection<TreePath> result = (Collection<TreePath>) info.getCachedValue(KEY_CACHE);
if (result != null) return result;
DetectorVisitor v = new DetectorVisitor(info, cancel);
CompilationUnitTree cu = info.getCompilationUnit();
v.scan(cu, null);
if (cancel.get())
return null;
List<TreePath> allUnusedImports = new ArrayList<TreePath>();
for (TreePath tree : v.getUnusedImports().values()) {
if (cancel.get()) {
return null;
}
allUnusedImports.add(tree);
}
allUnusedImports = Collections.unmodifiableList(allUnusedImports);
info.putCachedValue(KEY_CACHE, allUnusedImports, CacheClearPolicy.ON_CHANGE);
return allUnusedImports;
}