@Override public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState state) { if (tree.getSourceFile() == null) { return NO_MATCH; } String name = tree.getSourceFile().getName(); int idx = name.lastIndexOf('/'); if (idx != -1) { name = name.substring(idx + 1); } if (!name.equals("package-info.java")) { return NO_MATCH; } if (tree.getTypeDecls().isEmpty()) { return NO_MATCH; } return describeMatch(tree.getTypeDecls().get(0)); } }
private Boolean reportError(String msg, Tree node) { SourcePositions srcPos = verifier.getSourcePositions(); CompilationUnitTree compUnit = verifier.getCompilationUnit(); if (compUnit != null) { long pos = srcPos.getStartPosition(compUnit, node); long line = compUnit.getLineMap().getLineNumber(pos); String name = compUnit.getSourceFile().getName(); msg = String.format("%s:%d:%s", name, line, Messages.get(msg)); verifier.getMessager().printMessage(Diagnostic.Kind.ERROR, msg, getElement(node)); } else { verifier.getMessager().printMessage(Diagnostic.Kind.ERROR, msg); } return Boolean.FALSE; }
@Override public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState state) { if (tree.getTypeDecls().size() <= 1) { for (Tree member : tree.getTypeDecls()) { if (member instanceof ClassTree) { ClassTree classMember = (ClassTree) member; "Expected at most one top-level class declaration, instead found: %s", Joiner.on(", ").join(names)); return buildDescription(firstNonNull(tree.getPackageName(), tree.getTypeDecls().get(0))) .setMessage(message) .build();
public void run(CompilationController compilationController) throws Exception { compilationController.toPhase(Phase.ELEMENTS_RESOLVED); Trees trees = compilationController.getTrees(); CompilationUnitTree compilationUnitTree = compilationController.getCompilationUnit(); List<?extends Tree> typeDecls = compilationUnitTree.getTypeDecls(); for (Tree tree : typeDecls) { Element element = trees.getElement(trees.getPath(compilationUnitTree, tree)); if (element != null && element.getKind() == ElementKind.CLASS && element.getSimpleName().contentEquals(fo.getName())){ long pos = trees.getSourcePositions().getStartPosition(compilationUnitTree, tree); line[0] = compilationUnitTree.getLineMap().getLineNumber(pos); break; } } } }, true);
SourcePositions positions = controller.getTrees().getSourcePositions(); TreePath mainPath = treeUtilities.pathFor(offset); CompilationUnitTree unitTree = controller.getCompilationUnit(); Tree packgTree = unitTree.getPackageName(); if (offset >= positions.getStartPosition(unitTree, packgTree) && offset <= positions.getEndPosition(unitTree, packgTree)) { isValid[0] = false; return; Tree tree = mainPath.getLeaf(); Tree.Kind kind = tree.getKind(); int startPos = (int)positions.getStartPosition(unitTree, tree); int endPos = (int)positions.getEndPosition(unitTree, tree); int startLine = Utilities.getLineOffset((BaseDocument)doc, startPos); tree = path.getLeaf(); kind = tree.getKind(); if (kind == Tree.Kind.IMPORT) { isValid[0] = false; TypeElement typeElement = (TypeElement)controller.getTrees().getElement(path); className[0] = ElementUtilities.getBinaryName(typeElement); path = path.getParentPath();
insideExpression(env, new TreePath(path, fa.getExpression())); TreePath parentPath = path.getParentPath(); ExpressionTree et = it.next(); if (et == fa || (et.getKind() == Tree.Kind.ASSIGNMENT && ((AssignmentTree) et).getExpression() == fa)) { if (type.getKind() == TypeKind.ERROR && el.getKind().isClass()) { el = controller.getElements().getPackageElement(((TypeElement) el).getQualifiedName()); if (type.getKind() == TypeKind.ERROR && el.getKind().isClass()) { el = controller.getElements().getPackageElement(((TypeElement) el).getQualifiedName()); Trees trees = controller.getTrees(); for (ExpressionTree thr : ((MethodTree) parent).getThrows()) { if (sourcePositions.getEndPosition(root, thr) >= offset) { break; TypeMirror t = trees.getTypeMirror(new TreePath(path, thr)); for (Iterator<TypeMirror> it = exs.iterator(); it.hasNext();) { if (types.isSubtype(it.next(), t)) { if (type.getKind() == TypeKind.ERROR && el.getKind().isClass()) { el = controller.getElements().getPackageElement(((TypeElement) el).getQualifiedName()); } else if (parent.getKind() == Tree.Kind.COMPILATION_UNIT && ((CompilationUnitTree) parent).getPackageName() == fa) { PackageElement pe = controller.getElements().getPackageElement(fullName(exp)); if (pe != null) {
continue; Trees trees = copy.getTrees(); Elements elements = copy.getElements(); ElementUtilities elementUtilities = copy.getElementUtilities(); pkgCounts.put(pkg, -2); ExpressionTree packageName = cut.getPackageName(); pkg = packageName != null ? (PackageElement)trees.getElement(TreePath.getPath(cut, packageName)) : null; if (pkg == null && packageName != null) { pkg = elements.getPackageElement(elements.getName(packageName.toString())); List<ImportTree> imports = new ArrayList<ImportTree>(cut.getImports()); for (ImportTree imp : imports) { Element e = getImportedElement(cut, imp); ExpressionTree qualIdent = qualIdentFor(currentToImportElement); if (isStar) { qualIdent = make.MemberSelect(qualIdent, elements.getName("*")); //NOI18N ImportTree nImport = make.Import(qualIdent, isStatic); while (currentExisting >= 0) { ImportTree imp = imports.get(currentExisting); return make.CompilationUnit(cut.getPackage(), imports, cut.getTypeDecls(), cut.getSourceFile());
public Void visitIdentifier(IdentifierTree node, Void p) { TreePath path = getCurrentPath(); Element element = info.getTrees().getElement(path); if (element != null && element.asType().getKind() != TypeKind.ERROR) { // solve the imports only when declared type!!! if (element.getKind().isClass() || element.getKind().isInterface() || (element.getKind().isField() && ((Symbol) element).isStatic())) { Tree parent = path.getParentPath() != null ? path.getParentPath().getLeaf() : null; if ( (parent != null && parent.getKind() == Kind.CASE && ((CaseTree) parent).getExpression() == node && element.getKind() == ElementKind.ENUM_CONSTANT) || (path.getCompilationUnit() != null && ((Symbol) element).enclClass() != null && path.getCompilationUnit().getSourceFile() == ((Symbol) element).enclClass().sourcefile)) { translateMap.put(node, make.Identifier(element.getSimpleName())); } else { translateMap.put(node, make.QualIdent(element)); } } } return null; }
private void captureJavadoc(TypeElement elem) { List<String> imports = new ArrayList<>(); List<? extends ImportTree> importLines = Trees.instance(env).getPath(elem).getCompilationUnit().getImports(); for (ImportTree importLine : importLines) { imports.add(importLine.getQualifiedIdentifier().toString()); try { ExecutableElement methodElement = (ExecutableElement) memberElement; Implementation implementation = memberElement.getAnnotation(Implementation.class); for (Modifier modifier : memberElement.getModifiers()) { documentedMethod.modifiers.add(modifier.toString());
if (p.getLeaf() == p.getCompilationUnit()) { JavaFileObject fo = p.getCompilationUnit().getSourceFile(); boolean isPkgInfo = fo.isNameCompatible("package-info", JavaFileObject.Kind.SOURCE); if (tree == null) { if (isPkgInfo) switch (env.currElement.getKind()) { case METHOD: case CONSTRUCTOR: {
SourcePositions sp = trees.getSourcePositions(); int length = treeNodes.size(); List<EditorContext.Operation> operations = new ArrayList<EditorContext.Operation>(length); LineMap lineMap = cu.getLineMap(); int indexesIndex = 0; int from = indexes[indexesIndex]; for (int treeIndex = 0; treeIndex < length; treeIndex++) { Tree node = treeNodes.get(treeIndex); Tree.Kind kind = node.getKind(); EditorContext.Operation op = null; if (kind.equals(Tree.Kind.METHOD_INVOCATION) || if (identifier.getKind() == Tree.Kind.IDENTIFIER) { methodName = ((IdentifierTree) identifier).getName().toString(); TreePath iPath = TreePath.getPath(cu, identifier); getStartPosFromMethodLength = true; ExpressionTree exp = ((MemberSelectTree) identifier).getExpression(); TreePath expPath = TreePath.getPath(cu, exp); pos = (int) sp.getEndPosition(cu, identifier); EditorContext.Position methodEndPosition = opCreationDelegate.createPosition(
URL url = treePath.getCompilationUnit().getSourceFile().toUri().toURL(); file = URLMapper.findFileObject(url); if (file == null) { return new TreePathHandle(new EmptyDelegate(url, treePath.getLeaf().getKind())); int position = ((JCTree) treePath.getLeaf()).pos; if (position == (-1)) { int index = listChildren(treePath.getParentPath().getLeaf()).indexOf(treePath.getLeaf()); assert index != (-1); return new TreePathHandle(new CountingDelegate(TreePathHandle.create(treePath.getParentPath(), info), index, treePath.getLeaf().getKind())); Element correspondingElement = info.getTrees().getElement(current); Element element = null; boolean child = false; while (current != null) { if ( TreeUtilities.CLASS_TREE_KINDS.contains(current.getLeaf().getKind()) || current.getLeaf().getKind() == Kind.VARIABLE || current.getLeaf().getKind() == Kind.METHOD) { Element loc = info.getTrees().getElement(current); if (isSupported(loc)) { if (child && info.getTreeUtilities().isSynthetic(info.getCompilationUnit(), current.getLeaf())) { if (element != null && element.getKind() != ElementKind.OTHER) { elementHandle = createHandle(element);
public void run(WorkingCopy workingCopy) throws Exception { workingCopy.toPhase(Phase.RESOLVED); CompilationUnitTree cut = workingCopy.getCompilationUnit(); gu = GenerationUtils.newInstance(workingCopy); for (Tree typeDecl : cut.getTypeDecls()) { if (TreeUtilities.CLASS_TREE_KINDS.contains(typeDecl.getKind())) { Element e = workingCopy.getTrees().getElement(new TreePath(new TreePath(workingCopy.getCompilationUnit()), typeDecl)); if (e != null && e.getKind().isClass()) { TypeElement te = (TypeElement) e; ClassTree ct = (ClassTree) typeDecl; workingCopy.rewrite(ct, generateInterfaces(workingCopy, te, ct, gu)); } } } } };
CompilationUnitTree cu = cc.getCompilationUnit(); ClassTree clazz = null; for (Tree typeDecl : cu.getTypeDecls()) { if (TreeUtilities.CLASS_TREE_KINDS.contains(typeDecl.getKind())) { ClassTree candidate = (ClassTree) typeDecl; if (candidate.getModifiers().getFlags().contains(javax.lang.model.element.Modifier.PUBLIC)) { Element classElement = cc.getTrees().getElement(cc.getTrees().getPath(cu, clazz)); for (VariableElement variable : ElementFilter.fieldsIn(classElement.getEnclosedElements())) { String name = variable.getSimpleName().toString(); String type = variable.asType().toString(); variables.put(name, type); for (ExecutableElement method : ElementFilter.methodsIn(classElement.getEnclosedElements())) { String type = method.getReturnType().toString(); String name = method.getSimpleName().toString();
/** * Must be run under MDR transaction! */ public javax.lang.model.element.Element getJavaClass() { TreePath path = null; // try { // path = getCompletionTreePath(getController(), endOffset, COMPLETION_QUERY_TYPE); // } catch (IOException ex) { // Exceptions.printStackTrace(ex); // } javax.lang.model.element.Element el = null; try { getController().toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); } catch (IOException ex) { Exceptions.printStackTrace(ex); } while ((el == null || !(ElementKind.CLASS == el.getKind() || ElementKind.INTERFACE == el.getKind())) && path != null) { path.getCompilationUnit().getTypeDecls(); el = getController().getTrees().getElement(path); path = path.getParentPath(); } return el; }
public String toString() { if (treePath == null) return name + " (Unknown Source)"; // just like stack trace, we just print the file name and // not the whole path. The idea is that the package name should // provide enough clue on which directory it lives. CompilationUnitTree compilationUnit = treePath.getCompilationUnit(); Trees trees = Trees.instance(env); long startPosition = trees.getSourcePositions().getStartPosition(compilationUnit, treePath.getLeaf()); return name + "(" + compilationUnit.getSourceFile().getName() + ":" + compilationUnit.getLineMap().getLineNumber(startPosition) + ")"; } };
final Trees ts = getTrees(); assert ts != null; List<? extends Tree> typeDecls = cu.getTypeDecls(); TreePath cuPath = new TreePath(cu); for( Tree t : typeDecls ) { TreePath p = new TreePath(cuPath,t); Element e = ts.getElement(p); if ( e != null && ( e.getKind().isClass() || e.getKind().isInterface() ) ) { result.add((TypeElement)e);
public SourcePosition findSourcePosition(Element element) { while (element instanceof DecoratedElement) { element = ((DecoratedElement) element).getDelegate(); } if (element instanceof ElementAdaptor) { return ((ElementAdaptor)element).getSourcePosition(); } TreePath path = this.trees.getPath(element); if (path != null) { CompilationUnitTree cu = path.getCompilationUnit(); SourcePositions positions = this.trees.getSourcePositions(); long position = positions.getStartPosition(cu, path.getLeaf()); long line = cu.getLineMap().getLineNumber(position); long column = cu.getLineMap().getColumnNumber(position); return new SourcePosition(path, cu.getSourceFile(), position, line, column); } else { return null; } }
@Override public void run(WorkingCopy workingCopy) throws Exception { workingCopy.toPhase(JavaSource.Phase.RESOLVED); CompilationUnitTree cut = workingCopy.getCompilationUnit(); for (Tree t: cut.getTypeDecls()) { if (TreeUtilities.CLASS_TREE_KINDS.contains(t.getKind())) { ClassTree classT = (ClassTree) t; if (sourceFile.getName().equals(classT.getSimpleName().toString())) { Trees trees = workingCopy.getTrees(); TypeElement classEl = (TypeElement) trees.getElement(trees.getPath(cut, classTree)); MethodTree method = null; ExecutableElement methodEl = null; && el.getModifiers().contains(Modifier.PUBLIC)) { MethodTree mt = trees.getTree(el); int start = (int) sp.getStartPosition(cut, body); int end = (int) sp.getEndPosition(cut, body); null); } else { // switch to void newMethod = make.Method( method.getModifiers(), method.getName(),
private CompilationUnitTree modifyCompUnitTypeDecl(CompilationUnitTree compilationUnit, int index, Tree typeDeclaration, Operation op) { CompilationUnitTree copy = CompilationUnit( compilationUnit.getPackageAnnotations(), compilationUnit.getPackageName(), compilationUnit.getImports(), c(compilationUnit.getTypeDecls(), index, typeDeclaration, op), compilationUnit.getSourceFile() ); return copy; }