Search in sources :

Example 51 with TypeDeclaration

use of org.eclipse.jdt.core.dom.TypeDeclaration 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)

Example 52 with TypeDeclaration

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

the class ComponentSymbolProvider method getBeanName.

private String getBeanName(Annotation node) {
    ASTNode parent = node.getParent();
    if (parent instanceof TypeDeclaration) {
        TypeDeclaration type = (TypeDeclaration) parent;
        String beanName = type.getName().toString();
        if (beanName.length() > 0 && Character.isUpperCase(beanName.charAt(0))) {
            beanName = Character.toLowerCase(beanName.charAt(0)) + beanName.substring(1);
        }
        return beanName;
    }
    return null;
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 53 with TypeDeclaration

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

the class ConditionalsLiveHoverProvider method matchesAnnotation.

/**
 * @param annotation
 * @param jsonKey
 * @return true if the annotation matches the information in the json key from
 *         the running app.
 */
protected boolean matchesAnnotation(Annotation annotation, LiveConditional liveConditional) {
    // First check that the annotation matches the live conditional annotation
    String annotationName = annotation.resolveTypeBinding().getName();
    if (!liveConditional.getMessage().contains(annotationName)) {
        return false;
    }
    // Check that Java type in annotation in editor matches Java information in the live Conditional
    ASTNode parent = annotation.getParent();
    String typeInfo = liveConditional.getTypeInfo();
    if (parent instanceof MethodDeclaration) {
        MethodDeclaration methodDec = (MethodDeclaration) parent;
        IMethodBinding binding = methodDec.resolveBinding();
        String annotationDeclaringClassName = binding.getDeclaringClass().getName();
        String annotationMethodName = binding.getName();
        return typeInfo.contains(annotationDeclaringClassName) && typeInfo.contains(annotationMethodName);
    } else if (parent instanceof TypeDeclaration) {
        TypeDeclaration typeDec = (TypeDeclaration) parent;
        String annotationDeclaringClassName = typeDec.resolveBinding().getName();
        return typeInfo.contains(annotationDeclaringClassName);
    }
    return false;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) MarkedString(org.eclipse.lsp4j.MarkedString) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 54 with TypeDeclaration

use of org.eclipse.jdt.core.dom.TypeDeclaration in project che by eclipse.

the class UnimplementedCodeFix method createMakeTypeAbstractFix.

public static UnimplementedCodeFix createMakeTypeAbstractFix(CompilationUnit root, IProblemLocation problem) {
    ASTNode typeNode = getSelectedTypeNode(root, problem);
    if (!(typeNode instanceof TypeDeclaration))
        return null;
    TypeDeclaration typeDeclaration = (TypeDeclaration) typeNode;
    MakeTypeAbstractOperation operation = new MakeTypeAbstractOperation(typeDeclaration);
    String label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_addabstract_description, BasicElementLabels.getJavaElementName(typeDeclaration.getName().getIdentifier()));
    return new UnimplementedCodeFix(label, root, new CompilationUnitRewriteOperation[] { operation });
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 55 with TypeDeclaration

use of org.eclipse.jdt.core.dom.TypeDeclaration in project che by eclipse.

the class UnimplementedCodeFix method createCleanUp.

public static ICleanUpFix createCleanUp(CompilationUnit root, boolean addMissingMethod, boolean makeTypeAbstract, IProblemLocation[] problems) {
    Assert.isLegal(!addMissingMethod || !makeTypeAbstract);
    if (!addMissingMethod && !makeTypeAbstract)
        return null;
    if (problems.length == 0)
        return null;
    ArrayList<CompilationUnitRewriteOperation> operations = new ArrayList<CompilationUnitRewriteOperation>();
    for (int i = 0; i < problems.length; i++) {
        IProblemLocation problem = problems[i];
        if (addMissingMethod) {
            ASTNode typeNode = getSelectedTypeNode(root, problem);
            if (typeNode != null && !isTypeBindingNull(typeNode)) {
                operations.add(new AddUnimplementedMethodsOperation(typeNode));
            }
        } else {
            ASTNode typeNode = getSelectedTypeNode(root, problem);
            if (typeNode instanceof TypeDeclaration) {
                operations.add(new MakeTypeAbstractOperation((TypeDeclaration) typeNode));
            }
        }
    }
    if (operations.size() == 0)
        return null;
    String label;
    if (addMissingMethod) {
        label = CorrectionMessages.UnimplementedMethodsCorrectionProposal_description;
    } else {
        label = CorrectionMessages.UnimplementedCodeFix_MakeAbstractFix_label;
    }
    return new UnimplementedCodeFix(label, root, operations.toArray(new CompilationUnitRewriteOperation[operations.size()]));
}
Also used : ArrayList(java.util.ArrayList) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IProblemLocation(org.eclipse.jdt.ui.text.java.IProblemLocation) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Aggregations

TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)111 ASTNode (org.eclipse.jdt.core.dom.ASTNode)64 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)51 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)40 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)38 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)34 SimpleName (org.eclipse.jdt.core.dom.SimpleName)24 Type (org.eclipse.jdt.core.dom.Type)24 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)23 AST (org.eclipse.jdt.core.dom.AST)22 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)21 ArrayList (java.util.ArrayList)20 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)20 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)17 AnnotationTypeDeclaration (org.eclipse.jdt.core.dom.AnnotationTypeDeclaration)15 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)13 Block (org.eclipse.jdt.core.dom.Block)13 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)13 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)13 List (java.util.List)12