Example usage for org.eclipse.jdt.core.dom AST newNormalAnnotation

List of usage examples for org.eclipse.jdt.core.dom AST newNormalAnnotation

Introduction

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

Prototype

public NormalAnnotation newNormalAnnotation() 

Source Link

Document

Creates and returns a new unparented normal annotation node with an unspecified type name and an empty list of member value pairs.

Usage

From source file:com.crispico.flower.mp.codesync.code.java.adapter.JavaAbstractAstNodeModelAdapter.java

License:Open Source License

@Override
public Object createChildOnContainmentFeature(Object element, Object feature, Object correspondingChild) {
    if (AstCacheCodePackage.eINSTANCE.getModifiableElement_Modifiers().equals(feature)) {
        if (!(element instanceof BodyDeclaration || element instanceof SingleVariableDeclaration)) {
            return null;
        } else {/*from   w  ww.  j  ava 2s .c o m*/
            IExtendedModifier extendedModifier = null;

            if (correspondingChild instanceof com.crispico.flower.mp.model.astcache.code.Modifier) {
                ASTNode parent = (ASTNode) element;
                AST ast = parent.getAST();
                com.crispico.flower.mp.model.astcache.code.Modifier modifier = (com.crispico.flower.mp.model.astcache.code.Modifier) correspondingChild;

                extendedModifier = ast.newModifier(Modifier.ModifierKeyword.fromFlagValue(modifier.getType()));
                if (parent instanceof BodyDeclaration) {
                    ((BodyDeclaration) parent).modifiers().add(extendedModifier);
                } else {
                    ((SingleVariableDeclaration) parent).modifiers().add(extendedModifier);
                }
            }

            if (correspondingChild instanceof com.crispico.flower.mp.model.astcache.code.Annotation) {
                ASTNode parent = (ASTNode) element;
                AST ast = parent.getAST();
                com.crispico.flower.mp.model.astcache.code.Annotation annotation = (com.crispico.flower.mp.model.astcache.code.Annotation) correspondingChild;
                if (annotation.getValues().size() == 0) {
                    MarkerAnnotation markerAnnotation = ast.newMarkerAnnotation();
                    extendedModifier = markerAnnotation;
                }
                if (annotation.getValues().size() == 1) {
                    SingleMemberAnnotation singleMemberAnnotation = ast.newSingleMemberAnnotation();
                    extendedModifier = singleMemberAnnotation;
                } else {
                    NormalAnnotation normalAnnotation = ast.newNormalAnnotation();
                    extendedModifier = normalAnnotation;
                }
                if (parent instanceof BodyDeclaration) {
                    ((BodyDeclaration) parent).modifiers().add(extendedModifier);
                } else {
                    ((SingleVariableDeclaration) parent).modifiers().add(extendedModifier);
                }
            }
            return extendedModifier;
        }
    }

    return null;
}

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

License:Open Source License

/**
 * Creates a new appropriate {@link Annotation java Annotation} from the
 * given {@link EAnnotation modelEAnnotation}. The returned
 * {@link Annotation} can be a {@link MarkerAnnotation}, a
 * {@link SingleMemberAnnotation} or a {@link NormalAnnotation} according to
 * the <code>modelEAnnotation</code> features.
 * //from  ww w.  jav  a 2s  .com
 * @param ast
 *            the parent {@link AST} used to create the new
 *            {@link Annotation}
 * @param modelEAnnotation
 *            the model element {@link EAnnotation}
 * @return a new unparented {@link Annotation}
 * 
 * @author Luiza
 */
@SuppressWarnings("unchecked")
public static Annotation createNewJavaAnnotationFromModelEAnnotation(AST ast, EAnnotation modelEAnnotation) {
    EMap<String, String> detailsMap = modelEAnnotation.getDetails();
    Annotation newAnnotation = null;

    if (detailsMap.size() == 0) {
        newAnnotation = ast.newMarkerAnnotation();
    } else if (detailsMap.size() == 1 && detailsMap.get("") != null) {
        String value = detailsMap.get("");
        newAnnotation = ast.newSingleMemberAnnotation();
        ((SingleMemberAnnotation) newAnnotation).setValue(makeExpression(ast, value));
    } else {
        newAnnotation = ast.newNormalAnnotation();
        for (Entry<String, String> mapEntry : detailsMap) {
            MemberValuePair mvp = ast.newMemberValuePair();
            mvp.setName(ast.newSimpleName(mapEntry.getKey()));
            mvp.setValue(makeExpression(ast, mapEntry.getValue()));
            ((NormalAnnotation) newAnnotation).values().add(mvp);
        }
    }
    newAnnotation.setTypeName(ast.newName(modelEAnnotation.getSource()));
    return newAnnotation;
}

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

License:Open Source License

public static NormalAnnotation createNormalAnnotation(AST ast, String annotationName,
        Map<String, Object> memberValues, boolean forceFullyQualified) {
    NormalAnnotation newNormalAnnotation = ast.newNormalAnnotation();
    String name = forceFullyQualified ? annotationName : unqualified(annotationName);
    newNormalAnnotation.setTypeName(ast.newName(name));
    //newNormalAnnotation.
    //TODO//from w w  w  .j a va2 s . c o  m
    //newNormalAnnotation.setValue(AstUtils.createExpression(ast, memberValues));
    return newNormalAnnotation;
}

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

License:Open Source License

/**
 * Convenience method on AST to create a new NormalAnnotation with the given annotation name.
 * If annotationName is "Table", this will result in  "@Table()" in the java source when it is added 
 * to a BodyDeclaration. See addAnnotation(BodyDeclaration, Annotation)
 *//* w  w w.  jav  a2 s  .c om*/
public static NormalAnnotation newNormalAnnotation(AST ast, String annotationName) {
    NormalAnnotation annotation = ast.newNormalAnnotation();
    annotation.setTypeName(ast.newSimpleName(annotationName));
    return annotation;
}

From source file:de.dentrassi.varlink.generator.util.JdtHelper.java

License:Open Source License

public static NormalAnnotation addAnnotation(final BodyDeclaration decl, final String name) {
    final AST ast = decl.getAST();
    final NormalAnnotation ann = ast.newNormalAnnotation();
    ann.setTypeName(ast.newName(name));/* www.j  a  v a2 s  .c  om*/
    decl.modifiers().add(ann);
    return ann;
}

From source file:es.bsc.servicess.ide.editors.ImplementationFormPage.java

License:Apache License

private void modifyConstraintInCompilationUnit(ICompilationUnit cu, String methodName, String constraintName,
        String constraintValue, String localtypeName)
        throws JavaModelException, MalformedTreeException, BadLocationException {
    if (cu != null) {
        Document document = new Document(cu.getSource());
        // IDocument document = textFileBuffer.getDocument();
        log.debug("Loading document for modify constraint " + constraintName + " in method " + methodName);
        ASTParser parser = ASTParser.newParser(AST.JLS3); // handles JDK
        // 1.0, 1.1,
        // 1.2, 1.3,
        // 1.4, 1.5, 1.6
        parser.setSource(cu);//w  ww  .  j av a 2s.  c  o m
        // In order to parse 1.5 code, some compiler options need to be set
        // to 1.5
        // Map options = JavaCore.getOptions();
        // JavaCore.setComplianceOptions(JavaCore.VERSION_1_6, options);
        // parser.setCompilerOptions(options);
        CompilationUnit result = (CompilationUnit) parser.createAST(null);
        if (result != null) {
            result.recordModifications();
            AST ast = result.getAST();
            java.util.List<AbstractTypeDeclaration> types = result.types();
            log.debug("pack: " + result.getPackage().toString() + " types: " + result.types().size());
            if (result.types().size() > 0) {
                boolean find = false;
                for (AbstractTypeDeclaration type : types) {
                    log.debug("Type: " + type.getName().getIdentifier() + "("
                            + type.getName().getFullyQualifiedName() + ")");
                    if (type.getName().getIdentifier().equals(localtypeName)) {
                        MethodDeclaration[] methods = ((TypeDeclaration) type).getMethods();
                        for (MethodDeclaration m : methods) {
                            log.debug("method FQDN: " + m.getName().getFullyQualifiedName() + " identifier: "
                                    + m.getName().getIdentifier());
                            if (m.getName().getIdentifier().equals(methodName)) {
                                java.util.List<IExtendedModifier> mods = m.modifiers();
                                for (IExtendedModifier mod : mods) {
                                    log.debug("modifier: " + mod.getClass().toString());
                                    if (mod.isAnnotation()) {
                                        if (((Annotation) mod).isNormalAnnotation()) {
                                            log.debug("annotation: "
                                                    + ((NormalAnnotation) mod).getTypeName().toString());
                                            if (((NormalAnnotation) mod).getTypeName().toString()
                                                    .equals("Constraints")) {
                                                java.util.List<MemberValuePair> vals = ((NormalAnnotation) mod)
                                                        .values();
                                                MemberValuePair value = null;
                                                for (MemberValuePair v : vals) {
                                                    log.debug("member: " + v.getName().getIdentifier());
                                                    if (v.getName().getIdentifier().equals(constraintName)) {
                                                        value = v;
                                                        break;
                                                    }
                                                }
                                                Expression qn = ConstraintsUtils.convertValueToExpression(
                                                        constraintName, constraintValue, ast);
                                                if (value == null) {
                                                    value = ast.newMemberValuePair();
                                                    value.setName(ast.newSimpleName(constraintName));
                                                    value.setValue(qn);
                                                    log.debug("Adding property to annotation: "
                                                            + value.toString());
                                                    vals.add(value);
                                                } else {
                                                    value.setValue(qn);
                                                    log.debug("Changing direction: " + value.toString());
                                                }
                                                find = true;
                                                break;
                                            }
                                        }
                                    }
                                }
                                if (find)
                                    break;
                                else {
                                    ImportDeclaration imp = ast.newImportDeclaration();
                                    imp.setName(ast.newName("integratedtoolkit.types.annotations.Constraints"));
                                    if (!result.imports().contains(imp))
                                        result.imports().add(imp);
                                    NormalAnnotation annotation = ast.newNormalAnnotation();
                                    annotation.setTypeName(ast.newSimpleName("Constraints"));
                                    java.util.List<MemberValuePair> vals = annotation.values();
                                    MemberValuePair value = ast.newMemberValuePair();
                                    value.setName(ast.newSimpleName(constraintName));
                                    value.setValue(ConstraintsUtils.convertValueToExpression(constraintName,
                                            constraintValue, ast));
                                    log.debug("Adding property to annotation: " + value.toString());
                                    vals.add(value);
                                    m.modifiers().add(0, annotation);
                                    find = true;
                                }
                            }
                        }
                        if (find)
                            break;
                    }

                }
                if (find) {

                    TextEdit edits = result.rewrite(document, cu.getJavaProject().getOptions(true));
                    edits.apply(document);
                    String newSource = document.get();
                    cu.getBuffer().setContents(newSource);
                    cu.save(null, true);
                    log.debug("writting modifications " + newSource);

                } else {
                    log.warn("Varaible and annotation not found");
                }

            } else {
                log.warn("No types found in the Compilation unit from AST");
            }

        } else {
            log.error("Error parsing Compilation unit with AST");
        }
    } else {
        log.error("Error getting Interface Compilation Unit");
    }
}

From source file:org.asup.dk.compiler.rpj.writer.JDTNamedNodeWriter.java

License:Open Source License

@SuppressWarnings("unchecked")
public void writeEnumField(EnumConstantDeclaration enumField, QSpecialElement elem) {
    AST ast = enumField.getAST();
    NormalAnnotation normalAnnotation = ast.newNormalAnnotation();

    String name = new String("*" + enumField.getName());
    if (elem.getValue() != null && !name.equals(elem.getValue())) {
        normalAnnotation.setTypeName(ast.newSimpleName(Special.class.getSimpleName()));
        MemberValuePair memberValuePair = ast.newMemberValuePair();
        memberValuePair.setName(ast.newSimpleName("value"));
        StringLiteral stringLiteral = ast.newStringLiteral();
        stringLiteral.setLiteralValue(elem.getValue());
        memberValuePair.setValue(stringLiteral);
        normalAnnotation.values().add(memberValuePair);

        enumField.modifiers().add(normalAnnotation);
    }/*from   w  ww.j  av a2s.  c om*/
}

From source file:org.asup.dk.compiler.rpj.writer.JDTUnitWriter.java

License:Open Source License

@SuppressWarnings("unchecked")
public void writeSuppressWarning(BodyDeclaration target) {

    AST ast = target.getAST();

    NormalAnnotation annotation = ast.newNormalAnnotation();
    annotation.setTypeName(ast.newSimpleName(SuppressWarnings.class.getSimpleName()));

    MemberValuePair memberValuePair = ast.newMemberValuePair();
    memberValuePair.setName(ast.newSimpleName("value"));
    StringLiteral stringLiteral = ast.newStringLiteral();
    stringLiteral.setLiteralValue("static-access");
    memberValuePair.setValue(stringLiteral);
    annotation.values().add(memberValuePair);
    target.modifiers().add(annotation);//from   ww  w .j a v a 2  s  .  com

}

From source file:org.decojer.cavaj.utils.Annotations.java

License:Open Source License

/**
 * Decompile annotation.//from  w  w w.  ja  va  2s.c  om
 *
 * @param a
 *            annotation
 * @param context
 *            context
 * @return annotation AST Node
 */
@Nonnull
private static Annotation decompileAnnotation(@Nonnull final A a, @Nonnull final Element context) {
    final AST ast = context.getCu().getAst();
    final Set<Entry<String, Object>> members = a.getMembers();
    if (!members.isEmpty()) {
        if (members.size() == 1) {
            final Object memberValue = a.getMember("value");
            if (memberValue != null) {
                // a single member name "value=" is optional
                final SingleMemberAnnotation singleMemberAnnotation = ast.newSingleMemberAnnotation();
                singleMemberAnnotation.setTypeName(newTypeName(a.getT(), context));
                singleMemberAnnotation.setValue(decompileAnnotationDefaultValue(memberValue, context));
                return singleMemberAnnotation;
            }
        }
        final NormalAnnotation normalAnnotation = ast.newNormalAnnotation();
        normalAnnotation.setTypeName(newTypeName(a.getT(), context));
        for (final Entry<String, Object> member : members) {
            final Expression expression = decompileAnnotationDefaultValue(member.getValue(), context);
            if (expression != null) {
                final MemberValuePair newMemberValuePair = ast.newMemberValuePair();
                newMemberValuePair.setName(newSimpleName(member.getKey(), ast));
                newMemberValuePair.setValue(expression);
                normalAnnotation.values().add(newMemberValuePair);
            }
        }
        if (normalAnnotation.values().size() > 0) {
            return normalAnnotation;
        }
    }
    final MarkerAnnotation markerAnnotation = ast.newMarkerAnnotation();
    markerAnnotation.setTypeName(newTypeName(a.getT(), context));
    return markerAnnotation;
}

From source file:org.eclipse.jpt.common.core.internal.utility.jdt.AbstractDeclarationAnnotationAdapter.java

License:Open Source License

protected NormalAnnotation newNormalAnnotation(AST ast, String name) {
    NormalAnnotation annotation = ast.newNormalAnnotation();
    annotation.setTypeName(ast.newName(name));
    return annotation;
}