com.sun.tools.javac.code.TypeAnnotationPosition Java Examples

The following examples show how to use com.sun.tools.javac.code.TypeAnnotationPosition. 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: 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 #2
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 #3
Source File: ManTypes.java    From manifold with Apache License 2.0 6 votes vote down vote up
private java.util.List<TypeAnnotationPosition> findSelfAnnotationLocation( Symbol sym )
{
  if( sym == null )
  {
    return null;
  }

  SymbolMetadata metadata = sym.getMetadata();
  if( metadata == null || metadata.isTypesEmpty() )
  {
    return null;
  }

  List<Attribute.TypeCompound> typeAttributes = metadata.getTypeAttributes();
  if( typeAttributes.isEmpty() )
  {
    return null;
  }

  return typeAttributes.stream()
    .filter( attr -> attr.type.toString().equals( SELF_TYPE_NAME ) )
    .map( Attribute.TypeCompound::getPosition )
    .collect( Collectors.toList() );
}
 
Example #4
Source File: IndexFileParser.java    From annotation-tools with MIT License 6 votes vote down vote up
private void parseInnerTypes(ATypeElement e, int offset)
        throws IOException, ParseException {
    while (matchKeyword("inner-type")) {
        ArrayList<Integer> locNumbers = new ArrayList<>();
        locNumbers.add(offset + expectNonNegative(matchNNInteger()));
        // TODO: currently, we simply read the binary representation.
        // Should we read a higher-level format?
        while (matchChar(',')) {
            locNumbers.add(expectNonNegative(matchNNInteger()));
        }
        InnerTypeLocation loc;
        try {
            loc = new InnerTypeLocation(TypeAnnotationPosition.getTypePathFromBinary(locNumbers));
        } catch (AssertionError ex) {
            throw new ParseException(ex.getMessage(), ex);
        }
        AElement it = e.innerTypes.getVivify(loc);
        expectChar(':');
        parseAnnotations(it);
    }
}
 
Example #5
Source File: IndexFileParser.java    From annotation-tools with MIT License 6 votes vote down vote up
private Pair<ASTPath, InnerTypeLocation> splitNewArrayType(ASTPath astPath) {
    ASTPath outerPath = astPath;
    InnerTypeLocation loc = null;
    int last = astPath.size() - 1;

    if (last > 0) {
        ASTPath.ASTEntry entry = astPath.get(last);
        if (entry.getTreeKind() == Kind.NEW_ARRAY && entry.childSelectorIs(ASTPath.TYPE)) {
            int a = entry.getArgument();
            if (a > 0) {
                outerPath = astPath.getParentPath().extend(new ASTPath.ASTEntry(Kind.NEW_ARRAY, ASTPath.TYPE, 0));
                loc = new InnerTypeLocation(TypeAnnotationPosition.getTypePathFromBinary(Collections.nCopies(2 * a, 0)));
            }
        }
    }
    return Pair.of(outerPath, loc);
}
 
Example #6
Source File: TestSceneLib.java    From annotation-tools with MIT License 6 votes vote down vote up
@Test
public void testStoreParse1() {
    AScene s1 = newScene();

    s1.classes.getVivify("Foo").fields.getVivify("x").tlAnnotationsHere
            .add(createEmptyAnnotation(ready));
    Annotation myAuthor = Annotations.createValueAnnotation(adAuthor, "Matt M.");
    s1.classes.getVivify("Foo");
    s1.classes.getVivify("Foo").methods.getVivify("y()Z");
    s1.classes.getVivify("Foo").methods.getVivify("y()Z").parameters.getVivify(5);
    @SuppressWarnings("unused")
    Object dummy =
      s1.classes.getVivify("Foo").methods.getVivify("y()Z").parameters.getVivify(5).type;
    @SuppressWarnings("unused")
    Object dummy2 =
      s1.classes.getVivify("Foo").methods.getVivify("y()Z").parameters.getVivify(5).type.innerTypes;
    s1.classes.getVivify("Foo").methods.getVivify("y()Z").parameters.getVivify(5).type.innerTypes
            .getVivify(new InnerTypeLocation(
                    TypeAnnotationPosition.getTypePathFromBinary(
                            Arrays.asList(new Integer[] { 0, 0, 3, 2 })))).tlAnnotationsHere
            .add(myAuthor);

    doParseTest(fooIndexContents, "fooIndexContents", s1);
}
 
Example #7
Source File: ClassReader.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
List<TypeAnnotationPosition.TypePathEntry> readTypePath() {
    int len = nextByte();
    ListBuffer<Integer> loc = new ListBuffer<>();
    for (int i = 0; i < len * TypeAnnotationPosition.TypePathEntry.bytesPerEntry; ++i)
        loc = loc.append(nextByte());

    return TypeAnnotationPosition.getTypePathFromBinary(loc.toList());

}
 
Example #8
Source File: JsInteropAnnotationUtils.java    From j2cl with Apache License 2.0 5 votes vote down vote up
/** Determines whether the annotation is an annotation on the symbol {@code sym}. */
private static boolean isAnnotationOnType(Symbol sym, TypeAnnotationPosition position) {
  if (!position.location.isEmpty()) {
    return false;
  }
  switch (sym.getKind()) {
    case LOCAL_VARIABLE:
      return position.type == TargetType.LOCAL_VARIABLE;
    case FIELD:
      return position.type == TargetType.FIELD;
    case CONSTRUCTOR:
    case METHOD:
      return position.type == TargetType.METHOD_RETURN;
    case PARAMETER:
      switch (position.type) {
        case METHOD_FORMAL_PARAMETER:
          return ((MethodSymbol) sym.owner).getParameters().indexOf(sym)
              == position.parameter_index;
        default:
          return false;
      }
    case CLASS:
      // There are no type annotations on the top-level type of the class being declared, only
      // on other types in the signature (e.g. `class Foo extends Bar<@A Baz> {}`).
      return false;
    default:
      throw new InternalCompilerError(
          "Unsupported element kind in MoreAnnotation#isAnnotationOnType: %s.", sym.getKind());
  }
}
 
Example #9
Source File: TestSceneLib.java    From annotation-tools with MIT License 5 votes vote down vote up
private void checkConstructor(AMethod constructor) {
    Annotation ann = ((Annotation) constructor.receiver.type.lookup("p2.D"));
    Assert.assertEquals(Collections.singletonMap("value", "spam"), ann.fieldValues);
    ATypeElement l = (ATypeElement) constructor.body.locals
                    .get(new LocalLocation(1, 3, 5)).type;
    AElement i = (AElement) l.innerTypes.get(new InnerTypeLocation(
            TypeAnnotationPosition.getTypePathFromBinary(
                            Arrays.asList(new Integer[] { 0, 0 }))));
    Assert.assertNotNull(i.lookup("p2.C"));
    AField l2 =
            constructor.body.locals.get(new LocalLocation(1, 3, 6));
    Assert.assertNull(l2);
}
 
Example #10
Source File: ClassReader.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
TypeAnnotationProxy readTypeAnnotation() {
    TypeAnnotationPosition position = readPosition();
    CompoundAnnotationProxy proxy = readCompoundAnnotation();

    return new TypeAnnotationProxy(proxy, position);
}
 
Example #11
Source File: ClassReader.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public TypeAnnotationProxy(CompoundAnnotationProxy compound,
        TypeAnnotationPosition position) {
    this.compound = compound;
    this.position = position;
}
 
Example #12
Source File: ManTypes.java    From manifold with Apache License 2.0 4 votes vote down vote up
private java.util.List<TypeAnnotationPosition> singleMutable( TypeAnnotationPosition posCopy )
{
  ArrayList<TypeAnnotationPosition> single = new ArrayList<>();
  single.add( posCopy );
  return single;
}
 
Example #13
Source File: TestSceneLib.java    From annotation-tools with MIT License 4 votes vote down vote up
private void assignId(ATypeElement myField, int id,
        Integer... ls) {
    AElement el = myField.innerTypes.getVivify(
            new InnerTypeLocation(TypeAnnotationPosition.getTypePathFromBinary(Arrays.asList(ls))));
    el.tlAnnotationsHere.add(makeTLIdAnno(id));
}
 
Example #14
Source File: TestSceneLib.java    From annotation-tools with MIT License 4 votes vote down vote up
@Test
public void testTypeASTMapper() {
    // Construct a TAST for the type structure:
    // 0< 3<4>[1][2], 5<6, 8[7], 9, 10> >
    MyTAST tast = MyTAST.parameterization(0,
            MyTAST.arrayOf(1,
                    MyTAST.arrayOf(2,
                            MyTAST.parameterization(3,
                                    new MyTAST(4)
                            )
                    )
            ),
            MyTAST.parameterization(5,
                    new MyTAST(6),
                    MyTAST.arrayOf(7,
                            new MyTAST(8)),
                    new MyTAST(9),
                    new MyTAST(10)
            )
    );

    // Pretend myField represents a field of the type represented by tast.
    // We have to do this because clients are no longer allowed to create
    // AElements directly; instead, they must vivify.
    AElement myAField =
        new AScene().classes.getVivify("someclass").fields.getVivify("somefield");
    ATypeElement myAFieldType = myAField.type;
    // load it with annotations we can check against IDs
    myAFieldType.tlAnnotationsHere.add(makeTLIdAnno(0));

    final int ARRAY = TypePathEntryKind.ARRAY.tag;
    final int TYPE_ARGUMENT = TypePathEntryKind.TYPE_ARGUMENT.tag;

    assignId(myAFieldType, 1, TYPE_ARGUMENT, 0);
    assignId(myAFieldType, 2, TYPE_ARGUMENT, 0, ARRAY, 0);
    assignId(myAFieldType, 3, TYPE_ARGUMENT, 0, ARRAY, 0, ARRAY, 0);
    assignId(myAFieldType, 4, TYPE_ARGUMENT, 0, ARRAY, 0, ARRAY, 0, TYPE_ARGUMENT, 0);
    assignId(myAFieldType, 5, TYPE_ARGUMENT, 1);
    assignId(myAFieldType, 6, TYPE_ARGUMENT, 1, TYPE_ARGUMENT, 0);
    assignId(myAFieldType, 7, TYPE_ARGUMENT, 1, TYPE_ARGUMENT, 1);
    assignId(myAFieldType, 8, TYPE_ARGUMENT, 1, TYPE_ARGUMENT, 1, ARRAY, 0);
    assignId(myAFieldType, 9, TYPE_ARGUMENT, 1, TYPE_ARGUMENT, 2);
    // to test vivification, we don't assign 10

    // now visit and make sure the ID numbers match up
    MyTASTMapper mapper = new MyTASTMapper();
    mapper.traverse(tast, myAFieldType);

    for (int i = 0; i < 11; i++) {
        Assert.assertTrue(mapper.saw[i]);
    }
    // make sure it vivified #10 and our annotation stuck
    AElement e10 = myAFieldType.innerTypes.get(
            new InnerTypeLocation(TypeAnnotationPosition.getTypePathFromBinary(Arrays.asList(TYPE_ARGUMENT, 1, TYPE_ARGUMENT, 3))));
    Assert.assertNotNull(e10);
    int e10aid = (Integer) e10.lookup("IdAnno").getFieldValue("id");
    Assert.assertEquals(e10aid, 10);
}