Search in sources :

Example 26 with IAnnotationBinding

use of org.eclipse.jdt.core.dom.IAnnotationBinding in project j2cl by google.

the class JsInteropUtils method getJsInfo.

/**
 * Simply resolve the JsInfo from annotations. Do not do any extra computations. For example, if
 * there is no "name" is specified in the annotation, just returns null for JsName.
 */
public static JsInfo getJsInfo(IMethodBinding methodBinding) {
    IAnnotationBinding annotation = JsInteropAnnotationUtils.getJsMethodAnnotation(methodBinding);
    if (annotation == null) {
        annotation = JsInteropAnnotationUtils.getJsConstructorAnnotation(methodBinding);
    }
    if (annotation == null) {
        annotation = JsInteropAnnotationUtils.getJsPropertyAnnotation(methodBinding);
    }
    boolean isPropertyAccessor = JsInteropAnnotationUtils.getJsPropertyAnnotation(methodBinding) != null;
    return getJsInfo(methodBinding, methodBinding.getDeclaringClass(), annotation, isPropertyAccessor);
}
Also used : IAnnotationBinding(org.eclipse.jdt.core.dom.IAnnotationBinding)

Example 27 with IAnnotationBinding

use of org.eclipse.jdt.core.dom.IAnnotationBinding in project lsp4jakarta by eclipse.

the class ReplaceAnnotationProposal method getRewrite.

@Override
protected ASTRewrite getRewrite() throws CoreException {
    CompilationUnit fInvocationNode = getInvocationNode();
    IBinding fBinding = getBinding();
    String[] annotations = getAnnotations();
    ASTNode declNode = null;
    ASTNode boundNode = fInvocationNode.findDeclaringNode(fBinding);
    CompilationUnit newRoot = fInvocationNode;
    if (boundNode != null) {
        // is same CU
        declNode = boundNode;
    } else {
        newRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
        declNode = newRoot.findDeclaringNode(fBinding.getKey());
    }
    ImportRewrite imports = createImportRewrite(newRoot);
    boolean isField = declNode instanceof VariableDeclarationFragment;
    if (isField) {
        declNode = declNode.getParent();
    }
    if (declNode instanceof TypeDeclaration || isField) {
        AST ast = declNode.getAST();
        ASTRewrite rewrite = ASTRewrite.create(ast);
        ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(declNode, imports);
        // remove annotations in the removeAnnotations list
        @SuppressWarnings("unchecked") List<? extends ASTNode> children = (List<? extends ASTNode>) (declNode.getParent() instanceof CompilationUnit ? declNode : declNode.getParent()).getStructuralProperty(TypeDeclaration.MODIFIERS2_PROPERTY);
        for (ASTNode child : children) {
            if (child instanceof Annotation) {
                Annotation annotation = (Annotation) child;
                IAnnotationBinding annotationBinding = annotation.resolveAnnotationBinding();
                boolean containsAnnotation = Arrays.stream(removeAnnotations).anyMatch(annotationBinding.getName()::contains);
                if (containsAnnotation) {
                    rewrite.remove(child, null);
                }
            }
        }
        for (String annotation : annotations) {
            Annotation marker = ast.newMarkerAnnotation();
            // $NON-NLS-1$
            marker.setTypeName(ast.newName(imports.addImport(annotation, importRewriteContext)));
            rewrite.getListRewrite(declNode, isField ? FieldDeclaration.MODIFIERS2_PROPERTY : TypeDeclaration.MODIFIERS2_PROPERTY).insertFirst(marker, null);
        }
        return rewrite;
    }
    return null;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) AST(org.eclipse.jdt.core.dom.AST) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) IBinding(org.eclipse.jdt.core.dom.IBinding) Annotation(org.eclipse.jdt.core.dom.Annotation) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) IAnnotationBinding(org.eclipse.jdt.core.dom.IAnnotationBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) List(java.util.List) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Aggregations

IAnnotationBinding (org.eclipse.jdt.core.dom.IAnnotationBinding)27 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)14 Annotation (org.eclipse.jdt.core.dom.Annotation)7 ASTNode (org.eclipse.jdt.core.dom.ASTNode)6 ArrayType (org.eclipse.jdt.core.dom.ArrayType)6 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)6 Type (org.eclipse.jdt.core.dom.Type)6 ArrayList (java.util.ArrayList)4 List (java.util.List)4 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)4 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)4 Dimension (org.eclipse.jdt.core.dom.Dimension)4 InternalEList (org.eclipse.emf.ecore.util.InternalEList)3 IJavaProject (org.eclipse.jdt.core.IJavaProject)3 AST (org.eclipse.jdt.core.dom.AST)3 IBinding (org.eclipse.jdt.core.dom.IBinding)3 IExtendedModifier (org.eclipse.jdt.core.dom.IExtendedModifier)3 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)3 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)3 ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)3