Search in sources :

Example 31 with ASTVisitor

use of org.eclipse.jdt.core.dom.ASTVisitor in project flux by eclipse.

the class ASTNodes method getLeftMostSimpleName.

public static SimpleName getLeftMostSimpleName(Name name) {
    if (name instanceof SimpleName) {
        return (SimpleName) name;
    } else {
        final SimpleName[] result = new SimpleName[1];
        ASTVisitor visitor = new ASTVisitor() {

            @Override
            public boolean visit(QualifiedName qualifiedName) {
                Name left = qualifiedName.getQualifier();
                if (left instanceof SimpleName)
                    result[0] = (SimpleName) left;
                else
                    left.accept(this);
                return false;
            }
        };
        name.accept(visitor);
        return result[0];
    }
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name)

Example 32 with ASTVisitor

use of org.eclipse.jdt.core.dom.ASTVisitor in project flux by eclipse.

the class ASTNodes method getTypeName.

/**
 * Returns the simple name of the type, followed by array dimensions.
 * Skips qualifiers, type arguments, and type annotations.
 * <p>
 * Does <b>not</b> work for WildcardTypes, etc.!
 *
 * @param type a type that has a simple name
 * @return the simple name, followed by array dimensions
 * @see #getSimpleNameIdentifier(Name)
 * @since 3.10
 */
public static String getTypeName(Type type) {
    final StringBuffer buffer = new StringBuffer();
    ASTVisitor visitor = new ASTVisitor() {

        @Override
        public boolean visit(PrimitiveType node) {
            buffer.append(node.getPrimitiveTypeCode().toString());
            return false;
        }

        @Override
        public boolean visit(SimpleType node) {
            buffer.append(getSimpleNameIdentifier(node.getName()));
            return false;
        }

        @Override
        public boolean visit(QualifiedType node) {
            buffer.append(node.getName().getIdentifier());
            return false;
        }

        @Override
        public boolean visit(NameQualifiedType node) {
            buffer.append(node.getName().getIdentifier());
            return false;
        }

        @Override
        public boolean visit(ParameterizedType node) {
            node.getType().accept(this);
            return false;
        }

        @Override
        public void endVisit(ArrayType node) {
            for (int i = 0; i < node.dimensions().size(); i++) {
                // $NON-NLS-1$
                buffer.append("[]");
            }
        }
    };
    type.accept(visitor);
    return buffer.toString();
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

Example 33 with ASTVisitor

use of org.eclipse.jdt.core.dom.ASTVisitor in project flux by eclipse.

the class RenameService method computeReferences.

public JSONArray computeReferences(String username, String resourcePath, int offset, int length) {
    try {
        ICompilationUnit unit = liveEditUnits.getLiveEditUnit(username, resourcePath);
        if (unit != null) {
            final ASTParser parser = ASTParser.newParser(AST.JLS4);
            // Parse the class as a compilation unit.
            parser.setKind(ASTParser.K_COMPILATION_UNIT);
            parser.setSource(unit);
            parser.setResolveBindings(true);
            // Return the compiled class as a compilation unit
            final ASTNode compilationUnit = parser.createAST(null);
            final ASTNode nameNode = NodeFinder.perform(compilationUnit, offset, length);
            final List<ASTNode> nodes = new ArrayList<ASTNode>();
            if (nameNode instanceof SimpleName) {
                compilationUnit.accept(new ASTVisitor() {

                    @Override
                    public boolean visit(SimpleName node) {
                        if (node.getIdentifier().equals(((SimpleName) nameNode).getIdentifier())) {
                            nodes.add(node);
                        }
                        return super.visit(node);
                    }
                });
            }
            JSONArray references = new JSONArray();
            for (ASTNode astNode : nodes) {
                JSONObject nodeObject = new JSONObject();
                nodeObject.put("offset", astNode.getStartPosition());
                nodeObject.put("length", astNode.getLength());
                references.put(nodeObject);
            }
            return references;
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) JSONObject(org.json.JSONObject) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) ASTParser(org.eclipse.jdt.core.dom.ASTParser) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

Example 34 with ASTVisitor

use of org.eclipse.jdt.core.dom.ASTVisitor in project sts4 by spring-projects.

the class ActiveProfilesProvider method getLiveHoverHints.

@Override
public Collection<Range> getLiveHoverHints(Annotation annotation, TextDocument doc, SpringBootApp[] runningApps) {
    if (runningApps.length > 0) {
        Builder<Range> ranges = ImmutableList.builder();
        nameRange(doc, annotation).ifPresent(ranges::add);
        Set<String> allActiveProfiles = getAllActiveProfiles(runningApps);
        annotation.accept(new ASTVisitor() {

            @Override
            public boolean visit(StringLiteral node) {
                String value = ASTUtils.getLiteralValue(node);
                if (value != null && allActiveProfiles.contains(value)) {
                    rangeOf(doc, node).ifPresent(ranges::add);
                }
                return true;
            }
        });
        return ranges.build();
    }
    return ImmutableList.of();
}
Also used : StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) ASTUtils.nameRange(org.springframework.ide.vscode.boot.java.utils.ASTUtils.nameRange) Range(org.eclipse.lsp4j.Range) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

Example 35 with ASTVisitor

use of org.eclipse.jdt.core.dom.ASTVisitor in project sts4 by spring-projects.

the class AbstractSourceLinks method findTypeRegion.

protected Region findTypeRegion(CompilationUnit cu, String fqName) {
    if (cu == null) {
        return null;
    }
    int[] values = new int[] { 0, -1 };
    int lastDotIndex = fqName.lastIndexOf('.');
    String packageName = fqName.substring(0, lastDotIndex);
    String typeName = fqName.substring(lastDotIndex + 1);
    if (packageName.equals(cu.getPackage().getName().getFullyQualifiedName())) {
        Stack<String> visitedType = new Stack<>();
        cu.accept(new ASTVisitor() {

            private boolean visitDeclaration(AbstractTypeDeclaration node) {
                visitedType.push(node.getName().getIdentifier());
                if (values[1] < 0) {
                    if (String.join("$", visitedType.toArray(new String[visitedType.size()])).equals(typeName)) {
                        values[0] = node.getName().getStartPosition();
                        values[1] = node.getName().getLength();
                    }
                }
                return values[1] < 0;
            }

            @Override
            public boolean visit(TypeDeclaration node) {
                return visitDeclaration(node);
            }

            @Override
            public boolean visit(AnnotationTypeDeclaration node) {
                return visitDeclaration(node);
            }

            @Override
            public void endVisit(AnnotationTypeDeclaration node) {
                visitedType.pop();
                super.endVisit(node);
            }

            @Override
            public void endVisit(TypeDeclaration node) {
                visitedType.pop();
                super.endVisit(node);
            }
        });
    }
    return values[1] < 0 ? null : new Region(values[0], values[1]);
}
Also used : AnnotationTypeDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeDeclaration) Region(org.springframework.ide.vscode.commons.util.text.Region) AnnotationTypeDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) Stack(java.util.Stack) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Aggregations

ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)66 ArrayList (java.util.ArrayList)27 ASTNode (org.eclipse.jdt.core.dom.ASTNode)20 SimpleName (org.eclipse.jdt.core.dom.SimpleName)20 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)18 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)15 List (java.util.List)14 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)14 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)12 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)12 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)12 ASTParser (org.eclipse.jdt.core.dom.ASTParser)11 Block (org.eclipse.jdt.core.dom.Block)10 Expression (org.eclipse.jdt.core.dom.Expression)10 ImportDeclaration (org.eclipse.jdt.core.dom.ImportDeclaration)10 Name (org.eclipse.jdt.core.dom.Name)10 Statement (org.eclipse.jdt.core.dom.Statement)10 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)9 SearchResult (com.liferay.blade.api.SearchResult)8 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)8