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

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

Introduction

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

Prototype

public AnnotationTypeMemberDeclaration newAnnotationTypeMemberDeclaration() 

Source Link

Document

Creates and returns a new unparented annotation type member declaration node for an unspecified, but legal, member name and type; no modifiers; no javadoc; and no default value.

Usage

From source file:org.decojer.cavaj.transformers.TrOutline.java

License:Open Source License

private static void decompileMethod(@Nonnull final M m, @Nonnull final CU cu, final boolean strictFp) {
    if (checkMethodIgnore(m, cu)) {
        return;/*from   w w w .ja  va 2 s  .c o m*/
    }
    final String name = m.getName();
    final T t = m.getT();
    assert t != null : "decompile method cannot be dynamic";
    final AST ast = cu.getAst();

    final boolean isAnnotationMember = t.getAf(AF.ANNOTATION);

    // decompile BodyDeclaration, possible subtypes:
    // MethodDeclaration (method or constructor),
    // AnnotationTypeMemberDeclaration (all methods in @interface) or
    // Initializer (static {})
    final BodyDeclaration methodDeclaration;
    if (m.isInitializer()) {
        methodDeclaration = ast.newInitializer();
    } else if (m.isConstructor()) {
        // MethodDeclaration with type declaration name as name
        methodDeclaration = ast.newMethodDeclaration();
        ((MethodDeclaration) methodDeclaration).setConstructor(true);
        ((MethodDeclaration) methodDeclaration).setName(newSimpleName(
                cu.check(DFlag.START_TD_ONLY) || t.isAnonymous() ? t.getPName() : t.getSimpleName(), ast));
    } else if (isAnnotationMember) {
        // AnnotationTypeMemberDeclaration
        methodDeclaration = ast.newAnnotationTypeMemberDeclaration();
        ((AnnotationTypeMemberDeclaration) methodDeclaration).setName(newSimpleName(name, ast));
        // check if default value (e.g.: byte byteTest() default 2;)
        if (m.getAnnotationDefaultValue() != null) {
            final Expression expression = Annotations
                    .decompileAnnotationDefaultValue(m.getAnnotationDefaultValue(), t);
            if (expression != null) {
                ((AnnotationTypeMemberDeclaration) methodDeclaration).setDefault(expression);
            }
        }
    } else {
        // MethodDeclaration
        methodDeclaration = ast.newMethodDeclaration();
        ((MethodDeclaration) methodDeclaration).setName(newSimpleName(name, ast));
    }
    m.setAstNode(methodDeclaration);

    // decompile synthetic Javadoc-comment if no annotation set
    if (m.isSynthetic()) {
        final Javadoc javadoc = ast.newJavadoc();
        final TagElement tagElement = ast.newTagElement();
        tagElement.setTagName("is synthetic");
        javadoc.tags().add(tagElement);
        methodDeclaration.setJavadoc(javadoc);
    }
    // decompile deprecated Javadoc-tag if no annotation set
    if (m.getAf(AF.DEPRECATED) && !Annotations.isDeprecatedAnnotation(m.getAs())) {
        final Javadoc javadoc = ast.newJavadoc();
        final TagElement tagElement = ast.newTagElement();
        tagElement.setTagName("@deprecated");
        javadoc.tags().add(tagElement);
        methodDeclaration.setJavadoc(javadoc);
    }

    final List<IExtendedModifier> modifiers = methodDeclaration.modifiers();
    assert modifiers != null;

    // decompile annotations:
    // add annotation modifiers before other modifiers, order preserved in
    // source code generation through Eclipse JDT
    if (m.getAs() != null) {
        Annotations.decompileAnnotations(m.getAs(), modifiers, t);
    }

    final boolean isInterfaceMember = t.isInterface();

    // decompile modifier flags:
    // interfaces can have default methods since JVM 8
    if (isInterfaceMember && m.getCfg() != null && !m.isStatic()) {
        if (t.isBelow(Version.JVM_8)) {
            log.warn("Default methods are not known before JVM 8! Adding default keyword anyway, check this.");
        }
        modifiers.add(ast.newModifier(ModifierKeyword.DEFAULT_KEYWORD));
    }
    // public is default for interface and annotation type declarations
    if (m.getAf(AF.PUBLIC) && !isAnnotationMember && !isInterfaceMember) {
        modifiers.add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
    }
    if (m.getAf(AF.PRIVATE)) {
        modifiers.add(ast.newModifier(ModifierKeyword.PRIVATE_KEYWORD));
    }
    if (m.getAf(AF.PROTECTED)) {
        modifiers.add(ast.newModifier(ModifierKeyword.PROTECTED_KEYWORD));
    }
    if (m.isStatic()) {
        modifiers.add(ast.newModifier(ModifierKeyword.STATIC_KEYWORD));
    }
    if (m.getAf(AF.FINAL)) {
        modifiers.add(ast.newModifier(ModifierKeyword.FINAL_KEYWORD));
    }
    if (m.getAf(AF.SYNCHRONIZED)) {
        modifiers.add(ast.newModifier(ModifierKeyword.SYNCHRONIZED_KEYWORD));
    }
    if (m.getAf(AF.BRIDGE)) {
        // TODO
    }
    if (m.getAf(AF.NATIVE)) {
        modifiers.add(ast.newModifier(ModifierKeyword.NATIVE_KEYWORD));
    }
    // abstract is default for interface and annotation type declarations
    if (m.getAf(AF.ABSTRACT) && !isAnnotationMember && !isInterfaceMember) {
        modifiers.add(ast.newModifier(ModifierKeyword.ABSTRACT_KEYWORD));
    }
    if (m.getAf(AF.STRICTFP) && !strictFp) {
        modifiers.add(ast.newModifier(ModifierKeyword.STRICTFP_KEYWORD));
    }
    /*
     * AF.CONSTRUCTOR, AF.DECLARED_SYNCHRONIZED nothing, Dalvik only?
     */
    if (methodDeclaration instanceof MethodDeclaration) {
        decompileTypeParams(m.getTypeParams(), ((MethodDeclaration) methodDeclaration).typeParameters(), t);
        decompileMethodParams(m);
        if (!m.getAf(AF.ABSTRACT) && !m.getAf(AF.NATIVE)) {
            // create method block for valid syntax, abstract and native methods have none
            final Block block = ast.newBlock();
            ((MethodDeclaration) methodDeclaration).setBody(block);
            final CFG cfg = m.getCfg();
            if (cfg != null) {
                // could have no CFG because of empty or incomplete read code attribute
                cfg.setBlock(block);
            }
        }
    } else if (methodDeclaration instanceof Initializer) {
        // Initializer (static{}) has block per default
        assert ((Initializer) methodDeclaration).getBody() != null;

        final CFG cfg = m.getCfg();
        if (cfg != null) {
            // could have no CFG because of empty or incomplete read code attribute
            cfg.setBlock(((Initializer) methodDeclaration).getBody());
        }
    } else if (methodDeclaration instanceof AnnotationTypeMemberDeclaration) {
        assert m.getParamTs().length == 0;

        ((AnnotationTypeMemberDeclaration) methodDeclaration).setType(newType(m.getReturnT(), t));
    }
}