Search in sources :

Example 1 with Attribute

use of com.sun.tools.javac.code.Attribute in project bazel by bazelbuild.

the class TypeAnnotationUtils method createCompoundFromAnnotationMirror.

/**
     * Returns a newly created Attribute.Compound corresponding to an
     * argument AnnotationMirror.
     *
     * @param am  an AnnotationMirror, which may be part of an AST or an internally
     *            created subclass.
     * @return  a new Attribute.Compound corresponding to the AnnotationMirror
     */
public static Attribute.Compound createCompoundFromAnnotationMirror(ProcessingEnvironment env, AnnotationMirror am) {
    // Create a new Attribute to match the AnnotationMirror.
    List<Pair<Symbol.MethodSymbol, Attribute>> values = List.nil();
    for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : am.getElementValues().entrySet()) {
        Attribute attribute = attributeFromAnnotationValue(env, entry.getKey(), entry.getValue());
        values = values.append(new Pair<>((Symbol.MethodSymbol) entry.getKey(), attribute));
    }
    return new Attribute.Compound((Type.ClassType) am.getAnnotationType(), values);
}
Also used : ArrayType(javax.lang.model.type.ArrayType) TargetType(com.sun.tools.javac.code.TargetType) Type(com.sun.tools.javac.code.Type) Attribute(com.sun.tools.javac.code.Attribute) Symbol(com.sun.tools.javac.code.Symbol) TypeCompound(com.sun.tools.javac.code.Attribute.TypeCompound) Map(java.util.Map) Pair(com.sun.tools.javac.util.Pair)

Example 2 with Attribute

use of com.sun.tools.javac.code.Attribute in project bazel by bazelbuild.

the class TypeAnnotationUtils method createTypeCompoundFromAnnotationMirror.

/**
     * Returns a newly created Attribute.TypeCompound corresponding to an
     * argument AnnotationMirror.
     *
     * @param am  an AnnotationMirror, which may be part of an AST or an internally
     *            created subclass.
     * @param tapos  the type annotation position to use.
     * @return  a new Attribute.TypeCompound corresponding to the AnnotationMirror
     */
public static Attribute.TypeCompound createTypeCompoundFromAnnotationMirror(ProcessingEnvironment env, AnnotationMirror am, TypeAnnotationPosition tapos) {
    // Create a new Attribute to match the AnnotationMirror.
    List<Pair<Symbol.MethodSymbol, Attribute>> values = List.nil();
    for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : am.getElementValues().entrySet()) {
        Attribute attribute = attributeFromAnnotationValue(env, entry.getKey(), entry.getValue());
        values = values.append(new Pair<>((Symbol.MethodSymbol) entry.getKey(), attribute));
    }
    return new Attribute.TypeCompound((Type.ClassType) am.getAnnotationType(), values, tapos);
}
Also used : TypeCompound(com.sun.tools.javac.code.Attribute.TypeCompound) ArrayType(javax.lang.model.type.ArrayType) TargetType(com.sun.tools.javac.code.TargetType) Type(com.sun.tools.javac.code.Type) Attribute(com.sun.tools.javac.code.Attribute) Symbol(com.sun.tools.javac.code.Symbol) Map(java.util.Map) Pair(com.sun.tools.javac.util.Pair)

Example 3 with Attribute

use of com.sun.tools.javac.code.Attribute in project ceylon-compiler by ceylon.

the class JavacAnnotation method attributeToRefl.

private Object attributeToRefl(Attribute attr) {
    if (attr == null)
        return null;
    if (attr instanceof Attribute.Constant)
        return attr.getValue();
    if (attr instanceof Attribute.Array) {
        Attribute[] values = ((Attribute.Array) attr).values;
        List<Object> list = new ArrayList<Object>(values.length);
        for (Attribute elem : values) {
            list.add(attributeToRefl(elem));
        }
        return list;
    }
    if (attr instanceof Attribute.Compound)
        return new JavacAnnotation((Compound) attr);
    if (attr instanceof Attribute.Enum)
        return ((Attribute.Enum) attr).getValue().name.toString();
    if (attr instanceof Attribute.Class)
        return new JavacType(((Attribute.Class) attr).getValue());
    // FIXME: turn into error
    throw new RuntimeException("Unknown attribute type: " + attr);
}
Also used : Attribute(com.sun.tools.javac.code.Attribute) ArrayList(java.util.ArrayList) Compound(com.sun.tools.javac.code.Attribute.Compound)

Example 4 with Attribute

use of com.sun.tools.javac.code.Attribute in project ceylon-compiler by ceylon.

the class JavacAnnotation method getValue.

@Override
public Object getValue(String fieldName) {
    Object result = attributes.get(fieldName);
    if (result == null) {
        Attribute attr = member(fieldName);
        result = attributeToRefl(attr);
        attributes.put(fieldName, result);
    }
    return result;
}
Also used : Attribute(com.sun.tools.javac.code.Attribute)

Example 5 with Attribute

use of com.sun.tools.javac.code.Attribute in project ceylon-compiler by ceylon.

the class AnnotationMirrorImpl method toString.

/**
     * Returns a string representation of this annotation.
     * String is of one of the forms:
     *     @com.example.foo(name1=val1, name2=val2)
     *     @com.example.foo(val)
     *     @com.example.foo
     * Omit parens for marker annotations, and omit "value=" when allowed.
     */
public String toString() {
    StringBuilder sb = new StringBuilder("@");
    Constants.Formatter fmtr = Constants.getFormatter(sb);
    fmtr.append(anno.type.tsym);
    int len = anno.values.length();
    if (len > 0) {
        // omit parens for marker annotations
        sb.append('(');
        boolean first = true;
        for (Pair<MethodSymbol, Attribute> val : anno.values) {
            if (!first) {
                sb.append(", ");
            }
            first = false;
            Name name = val.fst.name;
            if (len > 1 || name != env.names.value) {
                fmtr.append(name);
                sb.append('=');
            }
            sb.append(new AnnotationValueImpl(env, val.snd, this));
        }
        sb.append(')');
    }
    return fmtr.toString();
}
Also used : Attribute(com.sun.tools.javac.code.Attribute) Name(com.sun.tools.javac.util.Name)

Aggregations

Attribute (com.sun.tools.javac.code.Attribute)6 TypeCompound (com.sun.tools.javac.code.Attribute.TypeCompound)2 Symbol (com.sun.tools.javac.code.Symbol)2 TargetType (com.sun.tools.javac.code.TargetType)2 Type (com.sun.tools.javac.code.Type)2 Pair (com.sun.tools.javac.util.Pair)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ArrayType (javax.lang.model.type.ArrayType)2 JavacTaskImpl (com.sun.tools.javac.api.JavacTaskImpl)1 Compound (com.sun.tools.javac.code.Attribute.Compound)1 Symtab (com.sun.tools.javac.code.Symtab)1 ClassReader (com.sun.tools.javac.jvm.ClassReader)1 ClassWriter (com.sun.tools.javac.jvm.ClassWriter)1 Pool (com.sun.tools.javac.jvm.Pool)1 JavacProcessingEnvironment (com.sun.tools.javac.processing.JavacProcessingEnvironment)1 Name (com.sun.tools.javac.util.Name)1 File (java.io.File)1 HashSet (java.util.HashSet)1 JavaCompiler (javax.tools.JavaCompiler)1