com.sun.tools.javac.code.Attribute.TypeCompound Java Examples

The following examples show how to use com.sun.tools.javac.code.Attribute.TypeCompound. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SymbolMetadata.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public void setAttributes(SymbolMetadata other) {
    if (other == null) {
        throw new NullPointerException();
    }
    setDeclarationAttributes(other.getDeclarationAttributes());
    if ((sym.flags() & Flags.BRIDGE) != 0) {
        Assert.check(other.sym.kind == Kinds.MTH);
        ListBuffer<TypeCompound> typeAttributes = new ListBuffer<>();
        for (TypeCompound tc : other.getTypeAttributes()) {
            // Carry over only contractual type annotations: i.e nothing interior to method body.
            if (!tc.position.type.isLocal())
                typeAttributes.append(tc);
        }
        setTypeAttributes(typeAttributes.toList());
    } else {
        setTypeAttributes(other.getTypeAttributes());
    }
    if (sym.kind == Kinds.TYP) {
        setInitTypeAttributes(other.getInitTypeAttributes());
        setClassInitTypeAttributes(other.getClassInitTypeAttributes());
    }
}
 
Example #2
Source File: Gen.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private List<Attribute.TypeCompound> getAndRemoveNonFieldTAs(VarSymbol sym) {
    List<TypeCompound> tas = sym.getRawTypeAttributes();
    ListBuffer<Attribute.TypeCompound> fieldTAs = new ListBuffer<Attribute.TypeCompound>();
    ListBuffer<Attribute.TypeCompound> nonfieldTAs = new ListBuffer<Attribute.TypeCompound>();
    for (TypeCompound ta : tas) {
        if (ta.getPosition().type == TargetType.FIELD) {
            fieldTAs.add(ta);
        } else {
            if (typeAnnoAsserts) {
                Assert.error("Type annotation does not have a valid positior");
            }

            nonfieldTAs.add(ta);
        }
    }
    sym.setTypeAttributes(fieldTAs.toList());
    return nonfieldTAs.toList();
}
 
Example #3
Source File: Annotate.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/** Attribute and store a semantic representation of the type annotation tree {@code tree} into
 * the tree.attribute field.
 *
 * @param a the tree representing an annotation
 * @param expectedAnnotationType the expected (super)type of the annotation
 * @param env the the current env in where the annotation instance is found
 */
public Attribute.TypeCompound attributeTypeAnnotation(JCAnnotation a, Type expectedAnnotationType,
                                                      Env<AttrContext> env)
{
    // The attribute might have been entered if it is Target or Repetable
    // Because TreeCopier does not copy type, redo this if type is null
    if (a.attribute == null || a.type == null || !(a.attribute instanceof Attribute.TypeCompound)) {
        // Create a new TypeCompound
        List<Pair<MethodSymbol,Attribute>> elems =
                attributeAnnotationValues(a, expectedAnnotationType, env);

        Attribute.TypeCompound tc =
                new Attribute.TypeCompound(a.type, elems, TypeAnnotationPosition.unknown);
        a.attribute = tc;
        return tc;
    } else {
        // Use an existing TypeCompound
        return (Attribute.TypeCompound)a.attribute;
    }
}
 
Example #4
Source File: SymbolMetadata.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void setAttributes(SymbolMetadata other) {
    if (other == null) {
        throw new NullPointerException();
    }
    setDeclarationAttributes(other.getDeclarationAttributes());
    if ((sym.flags() & Flags.BRIDGE) != 0) {
        Assert.check(other.sym.kind == Kind.MTH);
        ListBuffer<TypeCompound> typeAttributes = new ListBuffer<>();
        for (TypeCompound tc : other.getTypeAttributes()) {
            // Carry over only contractual type annotations: i.e nothing interior to method body.
            if (!tc.position.type.isLocal())
                typeAttributes.append(tc);
        }
        setTypeAttributes(typeAttributes.toList());
    } else {
        setTypeAttributes(other.getTypeAttributes());
    }
    if (sym.kind == Kind.TYP) {
        setInitTypeAttributes(other.getInitTypeAttributes());
        setClassInitTypeAttributes(other.getClassInitTypeAttributes());
    }
}
 
Example #5
Source File: Gen.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
List<Pair<List<Attribute.TypeCompound>, JCExpression>> catchTypesWithAnnotationsFromMulticatch(JCTypeUnion tree, List<TypeCompound> first) {
    List<JCExpression> alts = tree.alternatives;
    List<Pair<List<TypeCompound>, JCExpression>> res = List.of(new Pair<>(first, alts.head));
    alts = alts.tail;

    while(alts != null && alts.head != null) {
        JCExpression alt = alts.head;
        if (alt instanceof JCAnnotatedType) {
            JCAnnotatedType a = (JCAnnotatedType)alt;
            res = res.prepend(new Pair<>(annotate.fromAnnotations(a.annotations), alt));
        } else {
            res = res.prepend(new Pair<>(List.nil(), alt));
        }
        alts = alts.tail;
    }
    return res.reverse();
}
 
Example #6
Source File: Gen.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private List<Attribute.TypeCompound> getAndRemoveNonFieldTAs(VarSymbol sym) {
    List<TypeCompound> tas = sym.getRawTypeAttributes();
    ListBuffer<Attribute.TypeCompound> fieldTAs = new ListBuffer<Attribute.TypeCompound>();
    ListBuffer<Attribute.TypeCompound> nonfieldTAs = new ListBuffer<Attribute.TypeCompound>();
    for (TypeCompound ta : tas) {
        if (ta.getPosition().type == TargetType.FIELD) {
            fieldTAs.add(ta);
        } else {
            if (typeAnnoAsserts) {
                Assert.error("Type annotation does not have a valid positior");
            }

            nonfieldTAs.add(ta);
        }
    }
    sym.setTypeAttributes(fieldTAs.toList());
    return nonfieldTAs.toList();
}
 
Example #7
Source File: SymbolMetadata.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public void setAttributes(SymbolMetadata other) {
    if (other == null) {
        throw new NullPointerException();
    }
    setDeclarationAttributes(other.getDeclarationAttributes());
    if ((sym.flags() & Flags.BRIDGE) != 0) {
        Assert.check(other.sym.kind == Kinds.MTH);
        ListBuffer<TypeCompound> typeAttributes = new ListBuffer<>();
        for (TypeCompound tc : other.getTypeAttributes()) {
            // Carry over only contractual type annotations: i.e nothing interior to method body.
            if (!tc.position.type.isLocal())
                typeAttributes.append(tc);
        }
        setTypeAttributes(typeAttributes.toList());
    } else {
        setTypeAttributes(other.getTypeAttributes());
    }
    if (sym.kind == Kinds.TYP) {
        setInitTypeAttributes(other.getInitTypeAttributes());
        setClassInitTypeAttributes(other.getClassInitTypeAttributes());
    }
}
 
Example #8
Source File: Annotate.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/** Attribute and store a semantic representation of the type annotation tree {@code tree} into
 * the tree.attribute field.
 *
 * @param a the tree representing an annotation
 * @param expectedAnnotationType the expected (super)type of the annotation
 * @param env the the current env in where the annotation instance is found
 */
public Attribute.TypeCompound attributeTypeAnnotation(JCAnnotation a, Type expectedAnnotationType,
                                                      Env<AttrContext> env)
{
    // The attribute might have been entered if it is Target or Repetable
    // Because TreeCopier does not copy type, redo this if type is null
    if (a.attribute == null || a.type == null || !(a.attribute instanceof Attribute.TypeCompound)) {
        // Create a new TypeCompound
        List<Pair<MethodSymbol,Attribute>> elems =
                attributeAnnotationValues(a, expectedAnnotationType, env);

        Attribute.TypeCompound tc =
                new Attribute.TypeCompound(a.type, elems, TypeAnnotationPosition.unknown);
        a.attribute = tc;
        return tc;
    } else {
        // Use an existing TypeCompound
        return (Attribute.TypeCompound)a.attribute;
    }
}
 
Example #9
Source File: Gen.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private List<Attribute.TypeCompound> getAndRemoveNonFieldTAs(VarSymbol sym) {
    List<TypeCompound> tas = sym.getRawTypeAttributes();
    ListBuffer<Attribute.TypeCompound> fieldTAs = new ListBuffer<Attribute.TypeCompound>();
    ListBuffer<Attribute.TypeCompound> nonfieldTAs = new ListBuffer<Attribute.TypeCompound>();
    for (TypeCompound ta : tas) {
        if (ta.getPosition().type == TargetType.FIELD) {
            fieldTAs.add(ta);
        } else {
            if (typeAnnoAsserts) {
                Assert.error("Type annotation does not have a valid positior");
            }

            nonfieldTAs.add(ta);
        }
    }
    sym.setTypeAttributes(fieldTAs.toList());
    return nonfieldTAs.toList();
}
 
Example #10
Source File: Gen.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private List<Attribute.TypeCompound> getAndRemoveNonFieldTAs(VarSymbol sym) {
    List<TypeCompound> tas = sym.getRawTypeAttributes();
    ListBuffer<Attribute.TypeCompound> fieldTAs = new ListBuffer<Attribute.TypeCompound>();
    ListBuffer<Attribute.TypeCompound> nonfieldTAs = new ListBuffer<Attribute.TypeCompound>();
    for (TypeCompound ta : tas) {
        if (ta.getPosition().type == TargetType.FIELD) {
            fieldTAs.add(ta);
        } else {
            if (typeAnnoAsserts) {
                Assert.error("Type annotation does not have a valid positior");
            }

            nonfieldTAs.add(ta);
        }
    }
    sym.setTypeAttributes(fieldTAs.toList());
    return nonfieldTAs.toList();
}
 
Example #11
Source File: Gen.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private List<Attribute.TypeCompound> getAndRemoveNonFieldTAs(VarSymbol sym) {
    List<TypeCompound> tas = sym.getRawTypeAttributes();
    ListBuffer<Attribute.TypeCompound> fieldTAs = new ListBuffer<Attribute.TypeCompound>();
    ListBuffer<Attribute.TypeCompound> nonfieldTAs = new ListBuffer<Attribute.TypeCompound>();
    for (TypeCompound ta : tas) {
        if (ta.getPosition().type == TargetType.FIELD) {
            fieldTAs.add(ta);
        } else {
            if (typeAnnoAsserts) {
                Assert.error("Type annotation does not have a valid positior");
            }

            nonfieldTAs.add(ta);
        }
    }
    sym.setTypeAttributes(fieldTAs.toList());
    return nonfieldTAs.toList();
}
 
Example #12
Source File: SymbolMetadata.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public void setAttributes(SymbolMetadata other) {
    if (other == null) {
        throw new NullPointerException();
    }
    setDeclarationAttributes(other.getDeclarationAttributes());
    if ((sym.flags() & Flags.BRIDGE) != 0) {
        Assert.check(other.sym.kind == Kind.MTH);
        ListBuffer<TypeCompound> typeAttributes = new ListBuffer<>();
        for (TypeCompound tc : other.getTypeAttributes()) {
            // Carry over only contractual type annotations: i.e nothing interior to method body.
            if (!tc.position.type.isLocal())
                typeAttributes.append(tc);
        }
        setTypeAttributes(typeAttributes.toList());
    } else {
        setTypeAttributes(other.getTypeAttributes());
    }
    if (sym.kind == Kind.TYP) {
        setInitTypeAttributes(other.getInitTypeAttributes());
        setClassInitTypeAttributes(other.getClassInitTypeAttributes());
    }
}
 
Example #13
Source File: JavaEnvironment.java    From j2cl with Apache License 2.0 6 votes vote down vote up
private static TypeDescriptor applyNullabilityAnnotations(
    TypeDescriptor typeDescriptor,
    Element declarationMethodElement,
    Predicate<TypeAnnotationPosition> positionSelector) {
  List<TypeCompound> methodAnnotations =
      ((Symbol) declarationMethodElement).getRawTypeAttributes();
  for (TypeCompound methodAnnotation : methodAnnotations) {
    TypeAnnotationPosition position = methodAnnotation.getPosition();
    if (!positionSelector.test(position) || !isNonNullAnnotation(methodAnnotation)) {
      continue;
    }
    typeDescriptor = applyNullabilityAnnotation(typeDescriptor, position.location);
  }

  return typeDescriptor;
}
 
Example #14
Source File: AnnotatedTypeImpl.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the annotations of this program element.
 * Return an empty array if there are none.
 */
@Override
public AnnotationDesc[] annotations() {
    List<? extends TypeCompound> tas = type.getAnnotationMirrors();
    if (tas == null ||
            tas.isEmpty()) {
        return new AnnotationDesc[0];
    }
    AnnotationDesc res[] = new AnnotationDesc[tas.length()];
    int i = 0;
    for (Attribute.Compound a : tas) {
        res[i++] = new AnnotationDescImpl(env, a);
    }
    return res;
}
 
Example #15
Source File: AnnotatedTypeImpl.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the annotations of this program element.
 * Return an empty array if there are none.
 */
@Override
public AnnotationDesc[] annotations() {
    List<? extends TypeCompound> tas = type.getAnnotationMirrors();
    if (tas == null ||
            tas.isEmpty()) {
        return new AnnotationDesc[0];
    }
    AnnotationDesc res[] = new AnnotationDesc[tas.length()];
    int i = 0;
    for (Attribute.Compound a : tas) {
        res[i++] = new AnnotationDescImpl(env, a);
    }
    return res;
}
 
Example #16
Source File: TypeAnnotations.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private void setTypeAnnotationPos(List<JCAnnotation> annotations,
        TypeAnnotationPosition position) {
    for (JCAnnotation anno : annotations) {
        // attribute might be null during DeferredAttr;
        // we will be back later.
        if (anno.attribute != null) {
            ((Attribute.TypeCompound) anno.attribute).position = position;
        }
    }
}
 
Example #17
Source File: TypeAnnotations.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private void setTypeAnnotationPos(List<JCAnnotation> annotations,
        TypeAnnotationPosition position) {
    for (JCAnnotation anno : annotations) {
        // attribute might be null during DeferredAttr;
        // we will be back later.
        if (anno.attribute != null) {
            ((Attribute.TypeCompound) anno.attribute).position = position;
        }
    }
}
 
Example #18
Source File: Annotate.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Apply the annotations to the particular type.
 */
public void annotateTypeSecondStage(JCTree tree, List<JCAnnotation> annotations, Type storeAt) {
    typeAnnotation(() -> {
        List<Attribute.TypeCompound> compounds = fromAnnotations(annotations);
        Assert.check(annotations.size() == compounds.size());
        storeAt.getMetadataOfKind(Kind.ANNOTATIONS).combine(new TypeMetadata.Annotations(compounds));
    });
}
 
Example #19
Source File: Annotate.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public List<TypeCompound> fromAnnotations(List<JCAnnotation> annotations) {
    if (annotations.isEmpty()) {
        return List.nil();
    }

    ListBuffer<TypeCompound> buf = new ListBuffer<>();
    for (JCAnnotation anno : annotations) {
        Assert.checkNonNull(anno.attribute);
        buf.append((TypeCompound) anno.attribute);
    }
    return buf.toList();
}
 
Example #20
Source File: Annotate.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Apply the annotations to the particular type.
 */
public void annotateTypeParameterSecondStage(JCTree tree, List<JCAnnotation> annotations) {
    typeAnnotation(() -> {
        List<Attribute.TypeCompound> compounds = fromAnnotations(annotations);
        Assert.check(annotations.size() == compounds.size());
    });
}
 
Example #21
Source File: TypeAnnotations.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private void copyNewClassAnnotationsToOwner(JCNewClass tree) {
    Symbol sym = tree.def.sym;
    TypeAnnotationPosition pos = new TypeAnnotationPosition();
    ListBuffer<Attribute.TypeCompound> newattrs =
        new ListBuffer<Attribute.TypeCompound>();

    for (Attribute.TypeCompound old : sym.getRawTypeAttributes()) {
        newattrs.append(new Attribute.TypeCompound(old.type, old.values,
                                                   pos));
    }

    pos.type = TargetType.NEW;
    pos.pos = tree.pos;
    sym.owner.appendUniqueTypeAttributes(newattrs.toList());
}
 
Example #22
Source File: SymbolMetadata.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public SymbolMetadata appendUniqueTypes(List<Attribute.TypeCompound> l) {
    if (l.isEmpty()) {
        // no-op
    } else if (type_attributes.isEmpty()) {
        type_attributes = l;
    } else {
        // TODO: in case we expect a large number of annotations, this
        // might be inefficient.
        for (Attribute.TypeCompound tc : l) {
            if (!type_attributes.contains(tc))
                type_attributes = type_attributes.append(tc);
        }
    }
    return this;
}
 
Example #23
Source File: TypeAnnotations.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void findTypeCompoundPosition(JCTree tree, JCTree frame, List<Attribute.TypeCompound> annotations) {
    if (!annotations.isEmpty()) {
        final TypeAnnotationPosition p =
                resolveFrame(tree, frame, frames, currentLambda, 0, new ListBuffer<>());
        for (TypeCompound tc : annotations)
            tc.position = p;
    }
}
 
Example #24
Source File: TypeVariableImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the annotations of this program element.
 * Return an empty array if there are none.
 */
public AnnotationDesc[] annotations() {
    if (!type.isAnnotated()) {
        return new AnnotationDesc[0];
    }
    List<? extends TypeCompound> tas = type.getAnnotationMirrors();
    AnnotationDesc res[] = new AnnotationDesc[tas.length()];
    int i = 0;
    for (Attribute.Compound a : tas) {
        res[i++] = new AnnotationDescImpl(env, a);
    }
    return res;
}
 
Example #25
Source File: SymbolMetadata.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public SymbolMetadata appendInitTypeAttributes(List<Attribute.TypeCompound> l) {
    if (l.isEmpty()) {
        ; // no-op
    } else if (init_type_attributes.isEmpty()) {
        init_type_attributes = l;
    } else {
        init_type_attributes = init_type_attributes.appendList(l);
    }
    return this;
}
 
Example #26
Source File: Gen.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/** Insert instance initializer code into initial constructor.
 *  @param md        The tree potentially representing a
 *                   constructor's definition.
 *  @param initCode  The list of instance initializer statements.
 *  @param initTAs  Type annotations from the initializer expression.
 */
void normalizeMethod(JCMethodDecl md, List<JCStatement> initCode, List<TypeCompound> initTAs) {
    if (md.name == names.init && TreeInfo.isInitialConstructor(md)) {
        // We are seeing a constructor that does not call another
        // constructor of the same class.
        List<JCStatement> stats = md.body.stats;
        ListBuffer<JCStatement> newstats = new ListBuffer<>();

        if (stats.nonEmpty()) {
            // Copy initializers of synthetic variables generated in
            // the translation of inner classes.
            while (TreeInfo.isSyntheticInit(stats.head)) {
                newstats.append(stats.head);
                stats = stats.tail;
            }
            // Copy superclass constructor call
            newstats.append(stats.head);
            stats = stats.tail;
            // Copy remaining synthetic initializers.
            while (stats.nonEmpty() &&
                   TreeInfo.isSyntheticInit(stats.head)) {
                newstats.append(stats.head);
                stats = stats.tail;
            }
            // Now insert the initializer code.
            newstats.appendList(initCode);
            // And copy all remaining statements.
            while (stats.nonEmpty()) {
                newstats.append(stats.head);
                stats = stats.tail;
            }
        }
        md.body.stats = newstats.toList();
        if (md.body.endpos == Position.NOPOS)
            md.body.endpos = TreeInfo.endPos(md.body.stats.last());

        md.sym.appendUniqueTypeAttributes(initTAs);
    }
}
 
Example #27
Source File: Gen.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private List<Attribute.TypeCompound> getAndRemoveNonFieldTAs(VarSymbol sym) {
    List<TypeCompound> tas = sym.getRawTypeAttributes();
    ListBuffer<Attribute.TypeCompound> fieldTAs = new ListBuffer<>();
    ListBuffer<Attribute.TypeCompound> nonfieldTAs = new ListBuffer<>();
    for (TypeCompound ta : tas) {
        Assert.check(ta.getPosition().type != TargetType.UNKNOWN);
        if (ta.getPosition().type == TargetType.FIELD) {
            fieldTAs.add(ta);
        } else {
            nonfieldTAs.add(ta);
        }
    }
    sym.setTypeAttributes(fieldTAs.toList());
    return nonfieldTAs.toList();
}
 
Example #28
Source File: TypeAnnotations.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private void copyNewClassAnnotationsToOwner(JCNewClass tree) {
    Symbol sym = tree.def.sym;
    final TypeAnnotationPosition pos =
        TypeAnnotationPosition.newObj(tree.pos);
    ListBuffer<Attribute.TypeCompound> newattrs = new ListBuffer<>();

    for (Attribute.TypeCompound old : sym.getRawTypeAttributes()) {
        newattrs.append(new Attribute.TypeCompound(old.type, old.values,
                                                   pos));
    }

    sym.owner.appendUniqueTypeAttributes(newattrs.toList());
}
 
Example #29
Source File: SymbolMetadata.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private List<Attribute.TypeCompound> getTypePlaceholders() {
    List<Attribute.TypeCompound> res = List.<Attribute.TypeCompound>nil();
    for (Attribute.TypeCompound a : type_attributes) {
        if (a instanceof Placeholder) {
            res = res.prepend(a);
        }
    }
    return res.reverse();
}
 
Example #30
Source File: SymbolMetadata.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public Placeholder(Annotate.AnnotateRepeatedContext<T> ctx, List<T> placeholderFor, Symbol on) {
    super(on.type, List.<Pair<Symbol.MethodSymbol, Attribute>>nil(),
            ctx.isTypeCompound ?
                    ((Attribute.TypeCompound)placeholderFor.head).position :
                        new TypeAnnotationPosition());
    this.ctx = ctx;
    this.placeholderFor = placeholderFor;
    this.on = on;
}