本文整理汇总了Java中org.netbeans.api.java.source.TreeUtilities类的典型用法代码示例。如果您正苦于以下问题:Java TreeUtilities类的具体用法?Java TreeUtilities怎么用?Java TreeUtilities使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
TreeUtilities类属于org.netbeans.api.java.source包,在下文中一共展示了TreeUtilities类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: create
import org.netbeans.api.java.source.TreeUtilities; //导入依赖的package包/类
@Override
public List<? extends CodeGenerator> create(Lookup context) {
ArrayList<CodeGenerator> ret = new ArrayList<>();
JTextComponent component = context.lookup(JTextComponent.class);
CompilationController controller = context.lookup(CompilationController.class);
if (component == null || controller == null) {
return ret;
}
TreePath path = context.lookup(TreePath.class);
path = controller.getTreeUtilities().getPathElementOfKind(TreeUtilities.CLASS_TREE_KINDS, path);
if (path == null) {
return ret;
}
try {
controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
Element elem = controller.getTrees().getElement(path);
if (elem != null) {
EqualsHashCodeGenerator gen = createEqualsHashCodeGenerator(component, controller, elem);
if (gen != null) {
ret.add(gen);
}
}
} catch (IOException ioe) {
}
return ret;
}
示例2: run
import org.netbeans.api.java.source.TreeUtilities; //导入依赖的package包/类
public void run(WorkingCopy workingCopy) throws Exception {
workingCopy.toPhase(Phase.RESOLVED);
CompilationUnitTree cut = workingCopy.getCompilationUnit();
TreeMaker make = workingCopy.getTreeMaker();
for (Tree typeDeclaration : cut.getTypeDecls()){
if (TreeUtilities.CLASS_TREE_KINDS.contains(typeDeclaration.getKind())){
ClassTree clazz = (ClassTree) typeDeclaration;
EntityManagerGenerationStrategySupport strategy =
(EntityManagerGenerationStrategySupport) getStrategy(workingCopy, make, clazz, new GenerationOptions());
doAsserts(strategy);
} else {
fail("No class found"); // should not happen
}
}
}
示例3: findTopClasses
import org.netbeans.api.java.source.TreeUtilities; //导入依赖的package包/类
/**
*
* @return list of top classes, or an empty list of none were found
*/
static List<ClassTree> findTopClasses(
CompilationUnitTree compilationUnit,
TreeUtilities treeUtils) {
List<? extends Tree> typeDecls = compilationUnit.getTypeDecls();
if ((typeDecls == null) || typeDecls.isEmpty()) {
return Collections.<ClassTree>emptyList();
}
List<ClassTree> result = new ArrayList<ClassTree>(typeDecls.size());
for (Tree typeDecl : typeDecls) {
if (TreeUtilities.CLASS_TREE_KINDS.contains(typeDecl.getKind())) {
ClassTree clsTree = (ClassTree) typeDecl;
if (isTestable(clsTree, treeUtils)) {
result.add(clsTree);
}
}
}
return result;
}
示例4: findTopClassElems
import org.netbeans.api.java.source.TreeUtilities; //导入依赖的package包/类
/**
*
* @return list of {@code Element}s representing top classes,
* or an empty list of none were found
*/
private static List<TypeElement> findTopClassElems(
CompilationInfo compInfo,
CompilationUnitTree compilationUnit,
Filter filter) {
List<? extends Tree> typeDecls = compilationUnit.getTypeDecls();
if ((typeDecls == null) || typeDecls.isEmpty()) {
return Collections.<TypeElement>emptyList();
}
List<TypeElement> result = new ArrayList<TypeElement>(typeDecls.size());
Trees trees = compInfo.getTrees();
for (Tree typeDecl : typeDecls) {
if (TreeUtilities.CLASS_TREE_KINDS.contains(typeDecl.getKind())) {
Element element = trees.getElement(
new TreePath(new TreePath(compilationUnit), typeDecl));
TypeElement typeElement = (TypeElement) element;
if (filter.passes(typeElement, compInfo)) {
result.add(typeElement);
}
}
}
return result;
}
示例5: findMainClass
import org.netbeans.api.java.source.TreeUtilities; //导入依赖的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;
}
示例6: enclosingClasses
import org.netbeans.api.java.source.TreeUtilities; //导入依赖的package包/类
/**Returns canonical names of classes that enclose the {@link Variable}.
* If the given {@link Variable} represents a class, its canonical name is also listed.
* The names are given from the innermost class to the outermost class.
*
* @return the canonical names of the enclosing classes
*/
public @NonNull Iterable<? extends String> enclosingClasses(Variable forVariable) {
List<String> result = new ArrayList<String>();
TreePath path = getSingleVariable(forVariable);
while (path != null) {
TreePath current = path;
path = path.getParentPath();
if (!TreeUtilities.CLASS_TREE_KINDS.contains(current.getLeaf().getKind())) continue;
Element e = ctx.getInfo().getTrees().getElement(current);
if (e == null) continue;
if (e.getKind().isClass() || e.getKind().isInterface()) {
result.add(((TypeElement) e).getQualifiedName().toString());
}
}
return result;
}
示例7: CasualDiff
import org.netbeans.api.java.source.TreeUtilities; //导入依赖的package包/类
protected CasualDiff(Context context, DiffContext diffContext, TreeUtilities treeUtilities, Map<Tree, ?> tree2Tag, Map<Tree, DocCommentTree> tree2Doc, Map<?, int[]> tag2Span, Set<Tree> oldTrees) {
diffs = new LinkedHashSet<Diff>();
comments = CommentHandlerService.instance(context);
this.treeUtilities = treeUtilities;
this.diffContext = diffContext;
this.tokenSequence = diffContext.tokenSequence;
this.origText = diffContext.origText;
this.context = context;
this.names = Names.instance(context);
this.tree2Tag = tree2Tag;
this.tree2Doc = tree2Doc;
this.tag2Span = (Map<Object, int[]>) tag2Span;//XXX
printer = new VeryPretty(diffContext, diffContext.style, tree2Tag, tree2Doc, tag2Span, origText);
printer.oldTrees = oldTrees;
this.oldTrees = oldTrees;
}
示例8: getBounds
import org.netbeans.api.java.source.TreeUtilities; //导入依赖的package包/类
private int[] getBounds(TreeUtilities tu, SourcePositions sp, CompilationUnitTree cut, Tree tree) {
int[] bounds = {-1, -1};
if (tree != null) {
if (tree.getKind() == Tree.Kind.BLOCK) {
List<? extends StatementTree> stats = ((BlockTree) tree).getStatements();
if (stats != null && !stats.isEmpty()) {
bounds[0] = getStart(tu, sp, cut, stats.get(0));
bounds[1] = getEnd(tu, sp, cut, stats.get(stats.size() - 1));
}
} else {
bounds[0] = getStart(tu, sp, cut, tree);
bounds[1] = getEnd(tu, sp, cut, tree);
}
}
return bounds;
}
示例9: findMethod
import org.netbeans.api.java.source.TreeUtilities; //导入依赖的package包/类
/**
* Returns a path to the immediate enclosing method, lambda body or initializer block
* @param path start of the search
* @return path to the nearest enclosing executable or {@code null} in case of error.
*/
static TreePath findMethod(TreePath path) {
while (path != null) {
Tree leaf = path.getLeaf();
switch (leaf.getKind()) {
case BLOCK:
if (path.getParentPath() != null && TreeUtilities.CLASS_TREE_KINDS.contains(path.getParentPath().getLeaf().getKind())) {
return path.getParentPath();
}
break;
case METHOD:
case LAMBDA_EXPRESSION:
return path;
default:
break;
}
path = path.getParentPath();
}
return null;
}
示例10: deleteDeclIfMatch
import org.netbeans.api.java.source.TreeUtilities; //导入依赖的package包/类
private void deleteDeclIfMatch(Tree tree, Element elementToFind) {
if (JavaPluginUtils.isSyntheticPath(workingCopy, getCurrentPath())) {
return ;
}
Element el = workingCopy.getTrees().getElement(getCurrentPath());
if (isMatch(el, elementToFind)) {
Tree parent = getCurrentPath().getParentPath().getLeaf();
Tree newOne = null;
if (TreeUtilities.CLASS_TREE_KINDS.contains(parent.getKind())) {
newOne = make.removeClassMember((ClassTree) parent, tree);
} else if (parent.getKind() == Tree.Kind.COMPILATION_UNIT) {
newOne = make.removeCompUnitTypeDecl((CompilationUnitTree) parent, tree);
} else if (tree.getKind() == Tree.Kind.VARIABLE) {
if (parent.getKind() == Tree.Kind.METHOD) {
newOne = make.removeMethodParameter((MethodTree)parent, (VariableTree) tree);
} else {
newOne = make.removeBlockStatement((BlockTree)parent, (VariableTree) tree);
}
}
if (newOne!=null) {
rewrite(parent,newOne);
}
}
}
示例11: run
import org.netbeans.api.java.source.TreeUtilities; //导入依赖的package包/类
@Override
public List<Fix> run(CompilationInfo info, String diagnosticKey, int offset, TreePath treePath, Data<Void> data) {
if (treePath == null || !TreeUtilities.CLASS_TREE_KINDS.contains(treePath.getLeaf().getKind())) {
return null;
}
if (treePath.getLeaf().getKind() == Tree.Kind.INTERFACE) {
return null;
}
TypeElement type = (TypeElement) info.getTrees().getElement(treePath);
if (type == null) {
return null;
}
List<Fix> fixes = new ArrayList<Fix>();
fixes.add(new FixImpl(TreePathHandle.create(treePath, info), false).toEditorFix());
// fixes.add(new FixImpl(TreePathHandle.create(treePath, info), true));
if (!type.getNestingKind().equals(NestingKind.ANONYMOUS)) {
// add SuppressWarning only to non-anonymous class
fixes.addAll(FixFactory.createSuppressWarnings(info, treePath, SERIAL));
}
return fixes;
}
示例12: implement
import org.netbeans.api.java.source.TreeUtilities; //导入依赖的package包/类
@Override
public ChangeInfo implement() throws Exception {
final FileObject file = handle.getFileObject();
final JTextComponent component = EditorRegistry.lastFocusedComponent();
if (file != null && file == getFileObject(component)) {
final int[] position = new int[] {-1};
JavaSource.forFileObject(file).runUserActionTask(new Task<CompilationController>() {
@Override
public void run(CompilationController controller) throws Exception {
controller.toPhase(JavaSource.Phase.PARSED);
final TreePath tp = handle.resolve(controller);
if (tp != null && TreeUtilities.CLASS_TREE_KINDS.contains(tp.getLeaf().getKind())) {
position[0] = (int) controller.getTrees().getSourcePositions().getStartPosition(
tp.getCompilationUnit(),
(ClassTree)tp.getLeaf())+1;
}
}
}, true);
invokeRefactoring(component, position[0]);
}
return null;
}
示例13: findPublicTopLevelClass
import org.netbeans.api.java.source.TreeUtilities; //导入依赖的package包/类
/**
* Finds the first public top-level type in the compilation unit given by the
* given <code>CompilationController</code>.
*
* This method assumes the restriction that there is at most a public
* top-level type declaration in a compilation unit, as described in the
* section 7.6 of the JLS.
*/
public static ClassTree findPublicTopLevelClass(CompilationController controller) throws IOException {
controller.toPhase(Phase.ELEMENTS_RESOLVED);
final String mainElementName = controller.getFileObject().getName();
for (Tree tree : controller.getCompilationUnit().getTypeDecls()) {
if (!TreeUtilities.CLASS_TREE_KINDS.contains(tree.getKind())) {
continue;
}
ClassTree classTree = (ClassTree) tree;
if (!classTree.getSimpleName().contentEquals(mainElementName)) {
continue;
}
if (!classTree.getModifiers().getFlags().contains(Modifier.PUBLIC)) {
continue;
}
return classTree;
}
return null;
}
示例14: addInnerClasses
import org.netbeans.api.java.source.TreeUtilities; //导入依赖的package包/类
private void addInnerClasses(TypeElement te, EnumSet<ElementKind> kinds, DeclaredType baseType, Set<? extends Element> toExclude, String prefix, int substitutionOffset, JavadocContext jdctx) {
CompilationInfo controller = jdctx.javac;
Element srcEl = jdctx.handle.resolve(controller);
Elements elements = controller.getElements();
Types types = controller.getTypes();
Trees trees = controller.getTrees();
TreeUtilities tu = controller.getTreeUtilities();
TreePath docpath = srcEl != null ? trees.getPath(srcEl) : null;
Scope scope = docpath != null ? trees.getScope(docpath) : tu.scopeFor(caretOffset);
for (Element e : controller.getElementUtilities().getMembers(te.asType(), null)) {
if ((e.getKind().isClass() || e.getKind().isInterface()) && (toExclude == null || !toExclude.contains(e))) {
String name = e.getSimpleName().toString();
if (Utilities.startsWith(name, prefix) && (Utilities.isShowDeprecatedMembers() || !elements.isDeprecated(e)) && trees.isAccessible(scope, (TypeElement)e) && isOfKindAndType(e.asType(), e, kinds, baseType, scope, trees, types)) {
items.add(JavadocCompletionItem.createTypeItem(jdctx.javac, (TypeElement) e, substitutionOffset, null, elements.isDeprecated(e)/*, isOfSmartType(env, e.asType(), smartTypes)*/));
}
}
}
}
示例15: enclosingTry
import org.netbeans.api.java.source.TreeUtilities; //导入依赖的package包/类
static TreePath enclosingTry(TreePath from) {
TreePath tryPath = from;
while (tryPath != null
&& tryPath.getLeaf().getKind() != Kind.TRY
&& !TreeUtilities.CLASS_TREE_KINDS.contains(tryPath.getLeaf().getKind())
&& tryPath.getLeaf().getKind() != Kind.CATCH
&& tryPath.getLeaf().getKind() != Kind.LAMBDA_EXPRESSION)
tryPath = tryPath.getParentPath();
if (tryPath.getLeaf().getKind() == Kind.TRY) {
TryTree tt = (TryTree) tryPath.getLeaf();
//#104085: if the statement to be wrapped is inside a finally block of the try-catch,
//do not attempt to extend existing catches...:
for (Tree t : from) {
if (tt.getFinallyBlock() == t) {
return null;
}
}
return tryPath;
}
return null;
}