Example usage for org.eclipse.jdt.core.dom NormalAnnotation setTypeName

List of usage examples for org.eclipse.jdt.core.dom NormalAnnotation setTypeName

Introduction

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

Prototype

public void setTypeName(Name typeName) 

Source Link

Document

Sets the annotation type name of this annotation.

Usage

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/*ww  w  .ja  v  a2s . co 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)
 *//*from  w w w. java2  s.c  o  m*/
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));
    decl.modifiers().add(ann);/*  w ww.  jav  a 2 s.  c  o m*/
    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);//from w  ww  . j  a v a2  s  . c om
        // 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.RPJCompilerManagerImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/* w w w.java  2s  .com*/
public void writeDatabaseFile(QCompilationUnit compilationUnit, QCompilationSetup setup, OutputStream output)
        throws IOException {

    QDatabaseFile databaseFile = (QDatabaseFile) compilationUnit.getRoot();

    JDTDatabaseFileWriter databaseFileWriter = null;

    if (databaseFile instanceof QLogicalFile) {
        QLogicalFile logicalFile = (QLogicalFile) databaseFile;

        if (databaseFile.getDatabaseFormat().isEmpty()) {
            String table = logicalFile.getTables().get(0);
            databaseFileWriter = new JDTDatabaseFileWriter(null, compilationUnit, setup,
                    compilationUnit.getRoot().getName(), table);
        } else
            databaseFileWriter = new JDTDatabaseFileWriter(null, compilationUnit, setup,
                    compilationUnit.getRoot().getName(), QRecordWrapper.class);

        QViewDef viewDef = compilationUnit.getContext().getAdapter(logicalFile, QViewDef.class);
        if (viewDef != null) {
            databaseFileWriter.writeImport(Query.class);

            // @Query("SELECT * FROM TABLE")
            NormalAnnotation programAnnotation = databaseFileWriter.getAST().newNormalAnnotation();
            programAnnotation
                    .setTypeName(databaseFileWriter.getAST().newSimpleName(Query.class.getSimpleName()));
            MemberValuePair memberValuePair = databaseFileWriter.getAST().newMemberValuePair();
            memberValuePair.setName(databaseFileWriter.getAST().newSimpleName("value"));
            StringLiteral stringLiteral = databaseFileWriter.getAST().newStringLiteral();
            stringLiteral.setLiteralValue(viewDef.getQuerySelect());
            memberValuePair.setValue(stringLiteral);
            programAnnotation.values().add(memberValuePair);

            databaseFileWriter.getTarget().modifiers().add(0, programAnnotation);

        }
    } else
        databaseFileWriter = new JDTDatabaseFileWriter(null, compilationUnit, setup,
                compilationUnit.getRoot().getName(), QRecordWrapper.class);

    // @Format("BRARTIR")
    databaseFileWriter.writeImport(Format.class);
    NormalAnnotation programAnnotation = databaseFileWriter.getAST().newNormalAnnotation();
    programAnnotation.setTypeName(databaseFileWriter.getAST().newSimpleName(Format.class.getSimpleName()));
    MemberValuePair memberValuePair = databaseFileWriter.getAST().newMemberValuePair();
    memberValuePair.setName(databaseFileWriter.getAST().newSimpleName("value"));
    StringLiteral stringLiteral = databaseFileWriter.getAST().newStringLiteral();
    stringLiteral.setLiteralValue(databaseFile.getDatabaseFormat().getName());
    memberValuePair.setValue(stringLiteral);
    programAnnotation.values().add(memberValuePair);

    databaseFileWriter.getTarget().modifiers().add(0, programAnnotation);

    databaseFileWriter.writeDatabaseFile(databaseFile);

    databaseFileWriter.writeOutputStream(output);
}

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();/* www.j  a v a  2 s .  c om*/
    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 source file:org.asup.dk.compiler.rpj.writer.JDTNamedNodeWriter.java

License:Open Source License

@SuppressWarnings("unchecked")
public void writeAnnotation(ASTNode node, Class<?> annotationKlass, String key, Object value) {

    writeImport(annotationKlass);//  w  w w  . j ava  2  s .  co  m

    NormalAnnotation annotation = null;

    if (node instanceof EnumDeclaration) {
        EnumDeclaration enumDeclaration = (EnumDeclaration) node;
        annotation = findAnnotation(enumDeclaration.modifiers(), annotationKlass);

        if (annotation == null) {
            annotation = getAST().newNormalAnnotation();
            annotation.setTypeName(getAST().newName(annotationKlass.getSimpleName()));
            enumDeclaration.modifiers().add(annotation);
        }
    } else if (node instanceof FieldDeclaration) {
        FieldDeclaration field = (FieldDeclaration) node;
        annotation = findAnnotation(field.modifiers(), annotationKlass);

        if (annotation == null) {
            annotation = getAST().newNormalAnnotation();
            annotation.setTypeName(getAST().newName(annotationKlass.getSimpleName()));
            field.modifiers().add(annotation);
        }
    } else if (node instanceof EnumConstantDeclaration) {
        EnumConstantDeclaration field = (EnumConstantDeclaration) node;
        annotation = findAnnotation(field.modifiers(), annotationKlass);

        if (annotation == null) {
            annotation = getAST().newNormalAnnotation();
            annotation.setTypeName(getAST().newName(annotationKlass.getSimpleName()));
            field.modifiers().add(annotation);
        }
    } else if (node instanceof SingleVariableDeclaration) {
        SingleVariableDeclaration field = (SingleVariableDeclaration) node;
        annotation = findAnnotation(field.modifiers(), annotationKlass);

        if (annotation == null) {
            annotation = getAST().newNormalAnnotation();
            annotation.setTypeName(getAST().newName(annotationKlass.getSimpleName()));
            field.modifiers().add(annotation);
        }
    } else
        throw new RuntimeException("Unexpected runtime exception 5k43jwh45j8srkf");

    if (key != null) {
        MemberValuePair memberValuePair = getAST().newMemberValuePair();
        memberValuePair.setName(getAST().newSimpleName(key));
        if (value instanceof Number) {
            memberValuePair.setValue(getAST().newNumberLiteral(value.toString()));
            annotation.values().add(memberValuePair);
        } else if (value instanceof Boolean) {
            memberValuePair.setValue(getAST().newBooleanLiteral((boolean) value));
            annotation.values().add(memberValuePair);
        } else if (value instanceof String) {
            StringLiteral stringLiteral = getAST().newStringLiteral();
            stringLiteral.setLiteralValue((String) value);
            memberValuePair.setValue(stringLiteral);
            annotation.values().add(memberValuePair);
        } else if (value instanceof Enum) {
            Enum<?> enumValue = (Enum<?>) value;
            String enumName = enumValue.getClass().getSimpleName() + "." + ((Enum<?>) value).name();
            memberValuePair.setValue(getAST().newName(enumName.split("\\.")));
            annotation.values().add(memberValuePair);
        } else if (value instanceof List) {
            List<String> listValues = (List<String>) value;

            ArrayInitializer arrayInitializer = getAST().newArrayInitializer();
            for (String listValue : listValues) {
                StringLiteral stringLiteral = getAST().newStringLiteral();
                stringLiteral.setLiteralValue(listValue);
                arrayInitializer.expressions().add(stringLiteral);
            }

            memberValuePair.setValue(arrayInitializer);
            annotation.values().add(memberValuePair);
        } else
            throw new RuntimeException("Unexpected runtime exception k7548j4s67vo4kk");
    }
}

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

License:Open Source License

@SuppressWarnings("unchecked")
public void writeProgramAnnotation(QProgram program) {
    QConversion conversion = program.getFacet(QConversion.class);
    if (conversion != null) {
        MarkerAnnotation conversionAnnotation = getAST().newMarkerAnnotation();

        switch (conversion.getStatus()) {
        case POSSIBLE:
            break;
        case SUPPORTED:
            writeImport(Supported.class);
            conversionAnnotation.setTypeName(getAST().newSimpleName(Supported.class.getSimpleName()));
            getTarget().modifiers().add(conversionAnnotation);
            break;
        case TODO:
            writeImport(ToDo.class);
            conversionAnnotation.setTypeName(getAST().newSimpleName(ToDo.class.getSimpleName()));
            getTarget().modifiers().add(conversionAnnotation);
            break;
        case UNSUPPORTED:
            writeImport(Unsupported.class);
            conversionAnnotation.setTypeName(getAST().newSimpleName(Unsupported.class.getSimpleName()));
            getTarget().modifiers().add(conversionAnnotation);
            break;
        }//  ww  w .j  ava2s. c  o  m
    }

    // @Program(name=)
    NormalAnnotation programAnnotation = getAST().newNormalAnnotation();
    programAnnotation.setTypeName(getAST().newSimpleName(Program.class.getSimpleName()));
    MemberValuePair memberValuePair = getAST().newMemberValuePair();
    memberValuePair.setName(getAST().newSimpleName("name"));
    StringLiteral stringLiteral = getAST().newStringLiteral();
    stringLiteral.setLiteralValue(program.getName());
    memberValuePair.setValue(stringLiteral);
    programAnnotation.values().add(memberValuePair);

    getTarget().modifiers().add(0, programAnnotation);

    // @Test(category = "", object = "")
    programAnnotation = getAST().newNormalAnnotation();
    programAnnotation.setTypeName(getAST().newSimpleName(Test.class.getSimpleName()));

    MemberValuePair categoryValuePair = getAST().newMemberValuePair();
    categoryValuePair.setName(getAST().newSimpleName("category"));
    stringLiteral = getAST().newStringLiteral();
    stringLiteral.setLiteralValue("ILDATA");
    categoryValuePair.setValue(stringLiteral);
    programAnnotation.values().add(categoryValuePair);

    MemberValuePair objectValuePair = getAST().newMemberValuePair();
    objectValuePair.setName(getAST().newSimpleName("object"));
    stringLiteral = getAST().newStringLiteral();
    stringLiteral.setLiteralValue(program.getName());
    objectValuePair.setValue(stringLiteral);
    programAnnotation.values().add(objectValuePair);

    getTarget().modifiers().add(1, programAnnotation);
}

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

License:Open Source License

@SuppressWarnings("unchecked")
public void writeProgramAnnotation(QProgram program) {
    QConversion conversion = program.getFacet(QConversion.class);
    if (conversion != null) {
        MarkerAnnotation conversionAnnotation = getAST().newMarkerAnnotation();

        switch (conversion.getStatus()) {
        case POSSIBLE:
            break;
        case SUPPORTED:
            writeImport(Supported.class);
            conversionAnnotation.setTypeName(getAST().newSimpleName(Supported.class.getSimpleName()));
            getTarget().modifiers().add(conversionAnnotation);
            break;
        case TODO:
            writeImport(ToDo.class);
            conversionAnnotation.setTypeName(getAST().newSimpleName(ToDo.class.getSimpleName()));
            getTarget().modifiers().add(conversionAnnotation);
            break;
        case UNSUPPORTED:
            writeImport(Unsupported.class);
            conversionAnnotation.setTypeName(getAST().newSimpleName(Unsupported.class.getSimpleName()));
            getTarget().modifiers().add(conversionAnnotation);
            break;
        }/*from w  ww.  jav a 2s.  co  m*/
    }

    // @Program(name=)
    NormalAnnotation programAnnotation = getAST().newNormalAnnotation();
    programAnnotation.setTypeName(getAST().newSimpleName(Program.class.getSimpleName()));
    MemberValuePair memberValuePair = getAST().newMemberValuePair();
    memberValuePair.setName(getAST().newSimpleName("name"));
    StringLiteral stringLiteral = getAST().newStringLiteral();
    stringLiteral.setLiteralValue(program.getName());
    memberValuePair.setValue(stringLiteral);
    programAnnotation.values().add(memberValuePair);

    getTarget().modifiers().add(0, programAnnotation);
}

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();/*from   w  ww.j ava  2  s.com*/

    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);

}