Search in sources :

Example 16 with Annotation

use of org.eclipse.jdt.core.dom.Annotation in project Main by SpartanRefactoring.

the class ZZZ___Fixture_ModelClass method a29.

@Test
@method
public void a29() {
    for (final Annotation a : annotations()) {
        final SingleMemberAnnotation ¢ = az.singleMemberAnnotation(a);
        if (¢ == null)
            continue;
        final List<SimpleName> ns = annotees.of(¢);
        assert ns != null;
        final SimpleName n = the.firstOf(ns);
        assert n != null;
        if (!"hashCode".equals(n + ""))
            continue;
        final List<? extends ASTNode> s = scope.of(n);
        assert s != null : //
        fault.dump() + "\n\t scope = " + //
        s + "\n\t ¢ = " + //
        ¢ + "\n\t n = " + //
        n + "\n\t ns = " + //
        ns + fault.done();
    }
}
Also used : Annotation(org.eclipse.jdt.core.dom.Annotation)

Example 17 with Annotation

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

the class StubUtility2 method createParameters.

private static List<SingleVariableDeclaration> createParameters(IJavaProject project, ImportRewrite imports, ImportRewriteContext context, AST ast, IMethodBinding binding, String[] paramNames, MethodDeclaration decl) {
    boolean is50OrHigher = JavaModelUtil.is50OrHigher(project);
    List<SingleVariableDeclaration> parameters = decl.parameters();
    ITypeBinding[] params = binding.getParameterTypes();
    if (paramNames == null || paramNames.length < params.length) {
        paramNames = StubUtility.suggestArgumentNames(project, binding);
    }
    for (int i = 0; i < params.length; i++) {
        SingleVariableDeclaration var = ast.newSingleVariableDeclaration();
        ITypeBinding type = params[i];
        if (type.isWildcardType()) {
            ITypeBinding bound = type.getBound();
            type = (bound != null) ? bound : type.getErasure();
        }
        if (!is50OrHigher) {
            type = type.getErasure();
            var.setType(imports.addImport(type, ast, context));
        } else if (binding.isVarargs() && type.isArray() && i == params.length - 1) {
            var.setVarargs(true);
            /*
				 * Varargs annotations are special.
				 * Example:
				 *     foo(@O Object @A [] @B ... arg)
				 * => @B is not an annotation on the array dimension that constitutes the vararg.
				 * It's the type annotation of the *innermost* array dimension.
				 */
            int dimensions = type.getDimensions();
            @SuppressWarnings("unchecked") List<Annotation>[] dimensionAnnotations = (List<Annotation>[]) new List<?>[dimensions];
            for (int dim = 0; dim < dimensions; dim++) {
                dimensionAnnotations[dim] = new ArrayList<Annotation>();
                for (IAnnotationBinding annotation : type.getTypeAnnotations()) {
                    dimensionAnnotations[dim].add(imports.addAnnotation(annotation, ast, context));
                }
                type = type.getComponentType();
            }
            Type elementType = imports.addImport(type, ast, context);
            if (dimensions == 1) {
                var.setType(elementType);
            } else {
                ArrayType arrayType = ast.newArrayType(elementType, dimensions - 1);
                List<Dimension> dimensionNodes = arrayType.dimensions();
                for (int dim = 0; dim < dimensions - 1; dim++) {
                    // all except the innermost dimension
                    Dimension dimension = dimensionNodes.get(dim);
                    dimension.annotations().addAll(dimensionAnnotations[dim]);
                }
                var.setType(arrayType);
            }
            List<Annotation> varargTypeAnnotations = dimensionAnnotations[dimensions - 1];
            var.varargsAnnotations().addAll(varargTypeAnnotations);
        } else {
            var.setType(imports.addImport(type, ast, context));
        }
        var.setName(ast.newSimpleName(paramNames[i]));
        IAnnotationBinding[] annotations = binding.getParameterAnnotations(i);
        for (IAnnotationBinding annotation : annotations) {
            if (StubUtility2.isCopyOnInheritAnnotation(annotation.getAnnotationType(), project))
                var.modifiers().add(imports.addAnnotation(annotation, ast, context));
        }
        parameters.add(var);
    }
    return parameters;
}
Also used : SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ArrayList(java.util.ArrayList) Dimension(org.eclipse.jdt.core.dom.Dimension) Annotation(org.eclipse.jdt.core.dom.Annotation) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) IAnnotationBinding(org.eclipse.jdt.core.dom.IAnnotationBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) List(java.util.List) ArrayList(java.util.ArrayList)

Example 18 with Annotation

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

the class StubUtility2 method addOverrideAnnotation.

public static void addOverrideAnnotation(IJavaProject project, ASTRewrite rewrite, MethodDeclaration decl, IMethodBinding binding) {
    if (binding.getDeclaringClass().isInterface()) {
        String version = project.getOption(JavaCore.COMPILER_COMPLIANCE, true);
        if (JavaModelUtil.isVersionLessThan(version, JavaCore.VERSION_1_6))
            // not allowed in 1.5
            return;
        if (JavaCore.DISABLED.equals(project.getOption(JavaCore.COMPILER_PB_MISSING_OVERRIDE_ANNOTATION_FOR_INTERFACE_METHOD_IMPLEMENTATION, true)))
            // user doesn't want to use 1.6 style
            return;
    }
    Annotation marker = rewrite.getAST().newMarkerAnnotation();
    //$NON-NLS-1$
    marker.setTypeName(rewrite.getAST().newSimpleName("Override"));
    rewrite.getListRewrite(decl, MethodDeclaration.MODIFIERS2_PROPERTY).insertFirst(marker, null);
}
Also used : Annotation(org.eclipse.jdt.core.dom.Annotation)

Example 19 with Annotation

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

the class StubUtility2 method getImplementationModifiers.

private static List<IExtendedModifier> getImplementationModifiers(AST ast, IMethodBinding method, boolean inInterface, ImportRewrite importRewrite, ImportRewriteContext context) throws JavaModelException {
    IJavaProject javaProject = importRewrite.getCompilationUnit().getJavaProject();
    int modifiers = method.getModifiers() & ~Modifier.ABSTRACT & ~Modifier.NATIVE & ~Modifier.PRIVATE;
    if (inInterface) {
        modifiers = modifiers & ~Modifier.PROTECTED;
        if (!method.getDeclaringClass().isInterface()) {
            modifiers = modifiers | Modifier.PUBLIC;
        }
    } else {
        modifiers = modifiers & ~Modifier.DEFAULT;
    }
    IAnnotationBinding[] annotations = method.getAnnotations();
    if (modifiers != Modifier.NONE && annotations.length > 0) {
        // need an AST of the source method to preserve order of modifiers
        IMethod iMethod = (IMethod) method.getJavaElement();
        if (iMethod != null && JavaElementUtil.isSourceAvailable(iMethod)) {
            CheASTParser parser = CheASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
            parser.setSource(iMethod.getTypeRoot());
            parser.setIgnoreMethodBodies(true);
            CompilationUnit otherCU = (CompilationUnit) parser.createAST(null);
            ASTNode otherMethod = NodeFinder.perform(otherCU, iMethod.getSourceRange());
            if (otherMethod instanceof MethodDeclaration) {
                MethodDeclaration otherMD = (MethodDeclaration) otherMethod;
                ArrayList<IExtendedModifier> result = new ArrayList<IExtendedModifier>();
                List<IExtendedModifier> otherModifiers = otherMD.modifiers();
                for (IExtendedModifier otherModifier : otherModifiers) {
                    if (otherModifier instanceof Modifier) {
                        int otherFlag = ((Modifier) otherModifier).getKeyword().toFlagValue();
                        if ((otherFlag & modifiers) != 0) {
                            modifiers = ~otherFlag & modifiers;
                            result.addAll(ast.newModifiers(otherFlag));
                        }
                    } else {
                        Annotation otherAnnotation = (Annotation) otherModifier;
                        String n = otherAnnotation.getTypeName().getFullyQualifiedName();
                        for (IAnnotationBinding annotation : annotations) {
                            ITypeBinding otherAnnotationType = annotation.getAnnotationType();
                            String qn = otherAnnotationType.getQualifiedName();
                            if (qn.endsWith(n) && (qn.length() == n.length() || qn.charAt(qn.length() - n.length() - 1) == '.')) {
                                if (StubUtility2.isCopyOnInheritAnnotation(otherAnnotationType, javaProject))
                                    result.add(importRewrite.addAnnotation(annotation, ast, context));
                                break;
                            }
                        }
                    }
                }
                result.addAll(ASTNodeFactory.newModifiers(ast, modifiers));
                return result;
            }
        }
    }
    ArrayList<IExtendedModifier> result = new ArrayList<IExtendedModifier>();
    for (IAnnotationBinding annotation : annotations) {
        if (StubUtility2.isCopyOnInheritAnnotation(annotation.getAnnotationType(), javaProject))
            result.add(importRewrite.addAnnotation(annotation, ast, context));
    }
    result.addAll(ASTNodeFactory.newModifiers(ast, modifiers));
    return result;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ArrayList(java.util.ArrayList) Annotation(org.eclipse.jdt.core.dom.Annotation) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) IJavaProject(org.eclipse.jdt.core.IJavaProject) IAnnotationBinding(org.eclipse.jdt.core.dom.IAnnotationBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IMethod(org.eclipse.jdt.core.IMethod) CheASTParser(org.eclipse.jdt.core.dom.CheASTParser) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) Modifier(org.eclipse.jdt.core.dom.Modifier)

Example 20 with Annotation

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

the class ModifierCorrectionSubProcessor method removeOverrideAnnotationProposal.

public static void removeOverrideAnnotationProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws CoreException {
    ICompilationUnit cu = context.getCompilationUnit();
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (!(selectedNode instanceof MethodDeclaration)) {
        return;
    }
    MethodDeclaration methodDecl = (MethodDeclaration) selectedNode;
    //$NON-NLS-1$
    Annotation annot = findAnnotation("java.lang.Override", methodDecl.modifiers());
    if (annot != null) {
        ASTRewrite rewrite = ASTRewrite.create(annot.getAST());
        rewrite.remove(annot, null);
        String label = CorrectionMessages.ModifierCorrectionSubProcessor_remove_override;
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.REMOVE_OVERRIDE, image);
        proposals.add(proposal);
        QuickAssistProcessor.getCreateInSuperClassProposals(context, methodDecl.getName(), proposals);
    }
}
Also used : ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Image(org.eclipse.swt.graphics.Image) Annotation(org.eclipse.jdt.core.dom.Annotation)

Aggregations

Annotation (org.eclipse.jdt.core.dom.Annotation)30 ASTNode (org.eclipse.jdt.core.dom.ASTNode)9 IExtendedModifier (org.eclipse.jdt.core.dom.IExtendedModifier)7 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)7 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)5 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)4 ArrayType (org.eclipse.jdt.core.dom.ArrayType)4 Name (org.eclipse.jdt.core.dom.Name)4 ArrayList (java.util.ArrayList)3 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)3 ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)3 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)3 Type (org.eclipse.jdt.core.dom.Type)3 HashSet (java.util.HashSet)2 List (java.util.List)2 AST (org.eclipse.jdt.core.dom.AST)2 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)2 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)2 CastExpression (org.eclipse.jdt.core.dom.CastExpression)2 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)2