Example usage for org.eclipse.jdt.core.dom Annotation isNormalAnnotation

List of usage examples for org.eclipse.jdt.core.dom Annotation isNormalAnnotation

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom Annotation isNormalAnnotation.

Prototype

public boolean isNormalAnnotation() 

Source Link

Document

Returns whether this is a normal annotation ( NormalAnnotation ).

Usage

From source file:ca.ecliptical.pde.internal.ds.AnnotationProcessor.java

License:Open Source License

private void reportProblem(Annotation annotation, String member, int valueIndex,
        Collection<DSAnnotationProblem> problems, String message, String... args) {
    if (errorLevel.isNone())
        return;//from  w ww  .  ja v  a 2s  .c o m

    Expression memberValue = annotation;
    if (annotation.isNormalAnnotation() && member != null) {
        NormalAnnotation na = (NormalAnnotation) annotation;
        for (Object value : na.values()) {
            MemberValuePair pair = (MemberValuePair) value;
            if (member.equals(pair.getName().getIdentifier())) {
                memberValue = pair.getValue();
                break;
            }
        }
    } else if (annotation.isSingleMemberAnnotation()) {
        SingleMemberAnnotation sma = (SingleMemberAnnotation) annotation;
        memberValue = sma.getValue();
    }

    int start = memberValue.getStartPosition();
    int length = memberValue.getLength();

    if (valueIndex >= 0 && memberValue instanceof ArrayInitializer) {
        ArrayInitializer ai = (ArrayInitializer) memberValue;
        if (valueIndex < ai.expressions().size()) {
            Expression element = (Expression) ai.expressions().get(valueIndex);
            start = element.getStartPosition();
            length = element.getLength();
        }
    }

    if (start >= 0) {
        DSAnnotationProblem problem = new DSAnnotationProblem(errorLevel.isError(), message, args);
        problem.setSourceStart(start);
        problem.setSourceEnd(start + length - 1);
        problems.add(problem);
    }
}

From source file:ch.acanda.eclipse.pmd.java.resolution.SuppressWarningsQuickFix.java

License:Open Source License

private Annotation createReplacementSuppressWarningsAnnotation(final Annotation existingAnnotation,
        final AST ast) {
    final Annotation replacement;

    if (existingAnnotation == null || existingAnnotation.isMarkerAnnotation()) {
        final SingleMemberAnnotation annotation = createAnnotation(ast, SingleMemberAnnotation.class);
        annotation.setValue(createPMDLiteralValue(ast));
        replacement = annotation;// w  w  w  .ja v  a 2  s  .c  o  m

    } else if (existingAnnotation.isSingleMemberAnnotation()) {
        final SingleMemberAnnotation existingSingleMemberAnnotation = (SingleMemberAnnotation) existingAnnotation;
        final SingleMemberAnnotation annotation = createAnnotation(ast, SingleMemberAnnotation.class);
        annotation.setValue(createArrayInitializer(existingSingleMemberAnnotation.getValue()));
        replacement = annotation;

    } else if (existingAnnotation.isNormalAnnotation()) {
        final NormalAnnotation existingNormalAnnotation = (NormalAnnotation) existingAnnotation;
        final NormalAnnotation annotation = createAnnotation(ast, NormalAnnotation.class);
        createAnnotationValues(existingNormalAnnotation, annotation);
        replacement = annotation;

    } else {
        replacement = existingAnnotation;
    }

    return replacement;
}

From source file:com.crispico.flower.mp.metamodel.codesyncjava.algorithm.JavaSyncUtils.java

License:Open Source License

public static Object getLookupKeyForJavaAnnotation(Annotation annotation) {
    StringBuilder key = new StringBuilder(annotation.getTypeName().toString() + '(');

    if (annotation.isSingleMemberAnnotation()) {
        key.append(((SingleMemberAnnotation) annotation).getValue().toString() + ",");
    } else if (annotation.isNormalAnnotation()) {
        for (Object o : ((NormalAnnotation) annotation).values()) {
            MemberValuePair mvp = (MemberValuePair) o;
            key.append(mvp.getName().toString() + "=" + mvp.getValue().toString() + ",");
        }//www . j a va 2s .  c  o  m
    }
    key.append(')');
    return key.toString();
}

From source file:com.crispico.flower.mp.metamodel.codesyncjava.algorithm.reverse.ReverseJavaAnnotation.java

License:Open Source License

@Override
protected Object getFeatureValueFromAST(EStructuralFeature feature, JavaAnnotationHolder astElement) {
    Annotation annotation = astElement.getAnnotation();

    if (astElement == null)
        throw new IllegalArgumentException("astElement is null");
    switch (feature.getFeatureID()) {
    case EcorePackage.EANNOTATION__SOURCE:
        return annotation.getTypeName().toString();
    case EcorePackage.EANNOTATION__DETAILS:
        if (annotation.isNormalAnnotation()) {
            return ((NormalAnnotation) annotation).values();
        } else if (annotation.isSingleMemberAnnotation()) {
            return ((SingleMemberAnnotation) annotation).getValue();
        } else/*from   ww  w  .ja va2  s . c om*/
            return null;
    default:
        throw new IllegalArgumentException("Cannot get value for feature in ReverseJavaAnnotation:" + feature);
    }
}

From source file:com.halware.nakedide.eclipse.ext.annot.ast.MultipleValueAnnotationEvaluatorAndModifier.java

License:Open Source License

/**
 * @param value - a LinkedHashMap<String,Object>.
 *//*  w  ww. j  av  a  2  s. com*/
public void modify(Object object, Object value) {
    AbstractNode node = (AbstractNode) object;
    Annotation annotation = annotation(node);
    if (annotation != null && !annotation.isNormalAnnotation()) {
        return;
    }
    LinkedHashMap<String, Object> elementValues = Generics.asT(value);

    try {
        if (value == null) {
            if (annotation != null) {
                removeAnnotation(node);
            }
        } else {
            if (annotation == null) {
                addNormalAnnotation(node, elementValues);
            } else {
                updateNormalAnnotation(node, elementValues);
            }
        }
    } catch (JavaModelException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MalformedTreeException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (BadLocationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (CoreException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.halware.nakedide.eclipse.ext.annot.ast.MultipleValueAnnotationSingleMemberEvaluatorAndModifier.java

License:Open Source License

/**
* @param value - an Object/*from   ww  w .  ja  v a2s.c  o m*/
*/
public void modify(Object object, Object value) {
    AbstractNode node = (AbstractNode) object;
    Annotation annotation = annotation(node);
    if (annotation != null && !annotation.isNormalAnnotation()) {
        return;
    }

    // get existing members as map (if any), and update
    NormalAnnotation normalAnnotation = (NormalAnnotation) annotation;
    LinkedHashMap<String, Object> memberValues = ASTTools.memberValues(normalAnnotation);
    if (value != null) {
        memberValues.put(memberName, Generics.asT(value));
    } else {
        memberValues.remove(memberName);
    }

    try {
        if (memberValues.size() == 0) {
            if (annotation != null) {
                removeAnnotation(node);
            }
        } else {
            if (annotation == null) {
                addNormalAnnotation(node, memberValues);
            } else {
                updateNormalAnnotation(node, memberValues);
            }
        }
    } catch (JavaModelException e) {
        getLOGGER().error(e);
    } catch (MalformedTreeException e) {
        getLOGGER().error(e);
    } catch (BadLocationException e) {
        getLOGGER().error(e);
    } catch (CoreException e) {
        getLOGGER().error(e);
    }
}

From source file:com.halware.nakedide.eclipse.ext.annot.utils.dali.ASTTools.java

License:Open Source License

public static MemberValuePair memberValuePair(Annotation annotation, String elementName) {
    if ((annotation != null) && annotation.isNormalAnnotation()) {
        return memberValuePair((NormalAnnotation) annotation, elementName);
    }// www  . j  a v  a  2  s. co m
    return null;
}

From source file:com.wuetherich.osgi.ds.annotations.internal.builder.DsAnnotationAstVisitor.java

License:Open Source License

/**
 * <p>/*from w ww .  ja v  a2s .com*/
 * </p>
 * 
 * @param node
 */
private void handleDsAnnotation(Annotation node) {

    try {

        //
        if (_currentTypeDeclaration != null) {

            //
            if (node.resolveTypeBinding().getQualifiedName().equals(Component.class.getName())) {

                //
                _descriptions.put(_currentTypeDeclaration,
                        new AstBasedComponentDescription(_currentTypeDeclaration));
                getCurrentComponentDescription().setComponentDefaults();

                //
                if (node.isNormalAnnotation()) {
                    handleNormalComponentAnnotation((NormalAnnotation) node);
                } else if (node.isSingleMemberAnnotation()) {
                    //
                } else if (node.isMarkerAnnotation()) {
                    //
                }
            }

            if (_currentMethodDeclaration != null) {

                //
                if (node.resolveTypeBinding().getQualifiedName().equals(Activate.class.getName())) {
                    getCurrentComponentDescription()
                            .setActivateMethod(_currentMethodDeclaration.getName().getFullyQualifiedName());
                }

                //
                else if (node.resolveTypeBinding().getQualifiedName().equals(Deactivate.class.getName())) {
                    getCurrentComponentDescription()
                            .setDeactivateMethod(_currentMethodDeclaration.getName().getFullyQualifiedName());
                }

                //
                else if (node.resolveTypeBinding().getQualifiedName().equals(Modified.class.getName())) {
                    getCurrentComponentDescription()
                            .setModifiedMethod(_currentMethodDeclaration.getName().getFullyQualifiedName());
                }

                //
                else if (node.resolveTypeBinding().getQualifiedName().equals(Reference.class.getName())) {

                    //
                    if (node.isNormalAnnotation()) {
                        handleNormalReferenceAnnotation((NormalAnnotation) node);
                    } else if (node.isSingleMemberAnnotation()) {
                        //
                    } else if (node.isMarkerAnnotation()) {

                        //
                        String service = _currentMethodDeclaration.resolveBinding().getParameterTypes()[0]
                                .getBinaryName();
                        String bind = _currentMethodDeclaration.getName().getFullyQualifiedName();

                        //
                        getCurrentComponentDescription().addReference(service, bind, null, null, null, null,
                                null);
                    }
                }
            }
        }

    } catch (Exception e) {

        ASTNode astNode = node;

        //
        if (e instanceof DsAnnotationException) {

            //
            DsAnnotationException annotationException = (DsAnnotationException) e;

            //
            if (annotationException.hasAnnotationField()) {

                astNode = node;

                if (node instanceof NormalAnnotation) {

                    for (Object object : ((NormalAnnotation) node).values()) {

                        //
                        MemberValuePair pair = (MemberValuePair) object;
                        String valueName = pair.getName().toString();

                        //
                        if (valueName.equals(annotationException.getAnnotationField())) {
                            astNode = pair;
                            break;
                        }
                    }
                }
            }
        }

        //
        if (getCurrentComponentDescription() != null) {
            getCurrentComponentDescription().getProblems()
                    .add(new DsAnnotationProblem(
                            e.getMessage() != null ? e.getMessage() : "Unknown error in annotation",
                            astNode.getStartPosition(), astNode.getStartPosition() + astNode.getLength()));
        }
    }
}

From source file:edu.cmu.cs.crystal.annotations.AnnotationFinder.java

License:Open Source License

/**
 * Get multiple annotations out of an annotation array.
 * This presumes that baseAnno was marked as a multi. If so, it is
 * expected to be either a singleMemberAnno, or a normal anno with an
 * "annos" identifier./*from  www .j ava2  s. co m*/
 * 
 * @param baseAnno The multi annotation
 * @return The CrystalAnnotations found inside of it
 * @throws CrystalRuntimeException if anything went wrong during parsing. This means
 * that the annotation was not actually a multi annotation and should not
 * be marked as such.
 */
private List<? extends ICrystalAnnotation> getMulti(Annotation baseAnno) {
    CrystalRuntimeException err = new CrystalRuntimeException(
            "Hey! " + "If you have a multi annotation, it better have an array of Annotations!" + " Found "
                    + baseAnno.toString() + " without an array.");
    List<Expression> realAnnos = null;
    List<ICrystalAnnotation> crystalAnnos = new ArrayList<ICrystalAnnotation>();

    if (baseAnno.isSingleMemberAnnotation()) {
        SingleMemberAnnotation anno = (SingleMemberAnnotation) baseAnno;
        if (!(anno.getValue() instanceof ArrayInitializer)) {
            realAnnos = Collections.singletonList(anno.getValue());
        } else {
            realAnnos = ((ArrayInitializer) anno.getValue()).expressions();
        }
    } else if (baseAnno.isNormalAnnotation()) {
        NormalAnnotation anno = (NormalAnnotation) baseAnno;
        for (MemberValuePair pair : (List<MemberValuePair>) anno.values()) {
            if (pair.getName().getIdentifier().equals("annos") && pair.getValue() instanceof ArrayInitializer) {
                realAnnos = ((ArrayInitializer) pair.getValue()).expressions();
                break;
            }
        }
        if (realAnnos == null)
            throw err;
    } else
        throw err;

    //ok, now we have the array of annotations
    for (Expression exp : realAnnos) {
        if (!(exp instanceof Annotation))
            throw err;
        IAnnotationBinding binding = ((Annotation) exp).resolveAnnotationBinding();
        crystalAnnos.add(db.createAnnotation(binding));
    }
    return crystalAnnos;
}

From source file:net.atos.optimus.common.tools.jdt.ASTHelper.java

License:Open Source License

/**
 * Return the hashCode of a Body Declaration (from @Generated annotation)
 *//*  www.j a  v a  2s  . c o  m*/
public static int getHashCodeFromGeneratedAnnotation(BodyDeclaration bodyDeclaration) {
    Annotation generatedAnnotation = ASTHelper.getAnnotation(JavaCodeHelper.GENERATED_SIMPLECLASSNAME,
            bodyDeclaration);

    if (generatedAnnotation == null) {
        // @Generated not found, the BodyDeclaration must not be merged
        throw new UnsupportedOperationException();
    }

    if (generatedAnnotation.isNormalAnnotation()) {
        String stringHashcode = GeneratedAnnotationHelper
                .getGeneratedAnnotationHashcode((NormalAnnotation) generatedAnnotation);
        if (stringHashcode != null) {
            try {
                return Integer.parseInt(stringHashcode);
            } catch (NumberFormatException e) {
                Activator.getDefault()
                        .logError("Hashcode can't be parsed to int in: \n" + bodyDeclaration.toString(), e);
                return -1;
            }
        }
    }

    // If the hashCode cannot be found, throw an IllegalArgumentException
    throw new IllegalArgumentException();
}