Java Code Examples for com.sun.tools.javac.code.Attribute#TypeCompound

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: 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: 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 3
Source File: SrcClassUtil.java    From manifold with Apache License 2.0 5 votes vote down vote up
private void addAnnotation( SrcType srcType, Attribute.TypeCompound attr )
{
  String fqn = attr.type.toString();
  if( fqn.equals( "jdk.internal.HotSpotIntrinsicCandidate" ) )
  {
    // Since java 10 we have to keep these out of stubbed java source
    return;
  }
  SrcAnnotationExpression annoExpr = new SrcAnnotationExpression( fqn );
  for( Pair<Symbol.MethodSymbol, Attribute> value: attr.values )
  {
    annoExpr.addArgument( value.fst.flatName().toString(), new SrcType( value.snd.type.toString() ), value.snd.getValue() );
  }
  srcType.addAnnotation( annoExpr );
}
 
Example 4
Source File: SrcClassUtil.java    From manifold with Apache License 2.0 5 votes vote down vote up
private boolean isTargetIndex( TargetType targetType, Attribute.TypeCompound attr, int index )
{
  switch( targetType )
  {
    case METHOD_FORMAL_PARAMETER:
      return attr.getPosition().parameter_index == index;

    case THROWS:
      return attr.getPosition().type_index == index;

    default:
      return index < 0;
  }
}
 
Example 5
Source File: SrcClassUtil.java    From manifold with Apache License 2.0 5 votes vote down vote up
private SrcType makeSrcType( Type type, Symbol symbol, TargetType targetType, int index )
{
  SrcType srcType;
  List<Attribute.TypeCompound> annotationMirrors = type.getAnnotationMirrors();
  if( annotationMirrors != null && !annotationMirrors.isEmpty() )
  {
    String unannotatedType = isJava8()
                             ? ReflectUtil.method( type, "unannotatedType" ).invoke().toString()
                             : ReflectUtil.method( type, "cloneWithMetadata", ReflectUtil.type( "com.sun.tools.javac.code.TypeMetadata" ) )
                               .invoke( ReflectUtil.field( "com.sun.tools.javac.code.TypeMetadata", "EMPTY" ).getStatic() ).toString();
    srcType = new SrcType( unannotatedType );
  }
  else
  {
    srcType = new SrcType( typeNoAnnotations( type ) );
  }
  SymbolMetadata metadata = symbol.getMetadata();
  if( metadata == null || metadata.isTypesEmpty() )
  {
    return srcType;
  }
  List<Attribute.TypeCompound> typeAttributes = metadata.getTypeAttributes();
  if( typeAttributes.isEmpty() )
  {
    return null;
  }

  java.util.List<Attribute.TypeCompound> targetedTypeAttrs = typeAttributes.stream()
    .filter( attr -> attr.getPosition().type == targetType && isTargetIndex( targetType, attr, index ) )
    .collect( Collectors.toList() );

  annotateType( srcType, targetedTypeAttrs );
  return srcType;
}
 
Example 6
Source File: ExtensionTransformer.java    From manifold with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("WeakerAccess")
public static boolean isJailbreakSymbol( Symbol sym )
{
  if( sym == null )
  {
    return false;
  }

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

  List<Attribute.TypeCompound> typeAttributes = metadata.getTypeAttributes();
  if( !typeAttributes.isEmpty() )
  {
    return typeAttributes.stream()
      .anyMatch( attr -> attr.type.toString().equals( Jailbreak.class.getTypeName() ) );
  }

  List<Attribute.Compound> attributes = metadata.getDeclarationAttributes();
  if( !attributes.isEmpty() )
  {
    return attributes.stream()
      .anyMatch( attr -> attr.type.toString().equals( Jailbreak.class.getTypeName() ) );
  }
  return false;
}
 
Example 7
Source File: DPrinter.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void visitCompound(Attribute.Compound a) {
    if (a instanceof Attribute.TypeCompound) {
        Attribute.TypeCompound ta = (Attribute.TypeCompound) a;
        // consider a custom printer?
        printObject("position", ta.position, Details.SUMMARY);
    }
    printObject("synthesized", a.isSynthesized(), Details.SUMMARY);
    printList("values", a.values);
    visitAttribute(a);
}
 
Example 8
Source File: DPrinter.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public void visitCompound(Attribute.Compound a) {
    if (a instanceof Attribute.TypeCompound) {
        Attribute.TypeCompound ta = (Attribute.TypeCompound) a;
        // consider a custom printer?
        printObject("position", ta.position, Details.SUMMARY);
    }
    printObject("synthesized", a.isSynthesized(), Details.SUMMARY);
    printList("values", a.values);
    visitAttribute(a);
}
 
Example 9
Source File: DPrinter.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public void visitCompound(Attribute.Compound a) {
    if (a instanceof Attribute.TypeCompound) {
        Attribute.TypeCompound ta = (Attribute.TypeCompound) a;
        // consider a custom printer?
        printObject("position", ta.position, Details.SUMMARY);
    }
    printObject("synthesized", a.isSynthesized(), Details.SUMMARY);
    printList("values", a.values);
    visitAttribute(a);
}
 
Example 10
Source File: NullabilityUtil.java    From NullAway with MIT License 5 votes vote down vote up
private static Stream<? extends AnnotationMirror> getTypeUseAnnotations(Symbol symbol) {
  Stream<Attribute.TypeCompound> rawTypeAttributes = symbol.getRawTypeAttributes().stream();
  if (symbol instanceof Symbol.MethodSymbol) {
    // for methods, we want the type-use annotations on the return type
    return rawTypeAttributes.filter((t) -> t.position.type.equals(TargetType.METHOD_RETURN));
  }
  return rawTypeAttributes;
}
 
Example 11
Source File: ClassReader.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void run() {
    JavaFileObject previousClassFile = currentClassFile;
    try {
        currentClassFile = classFile;
        List<Attribute.TypeCompound> newList = deproxyTypeCompoundList(proxies);
        sym.setTypeAttributes(newList.prependList(sym.getRawTypeAttributes()));
    } finally {
        currentClassFile = previousClassFile;
    }
}
 
Example 12
Source File: DPrinter.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public void visitCompound(Attribute.Compound a) {
    if (a instanceof Attribute.TypeCompound) {
        Attribute.TypeCompound ta = (Attribute.TypeCompound) a;
        // consider a custom printer?
        printObject("position", ta.position, Details.SUMMARY);
    }
    printObject("synthesized", a.isSynthesized(), Details.SUMMARY);
    printList("values", a.values);
    visitAttribute(a);
}
 
Example 13
Source File: ManTypes.java    From manifold with Apache License 2.0 5 votes vote down vote up
private boolean hasSelfType( Type type )
{
  for( Attribute.TypeCompound anno: type.getAnnotationMirrors() )
  {
    if( anno.type.toString().equals( SELF_TYPE_NAME ) )
    {
      return true;
    }
  }

  if( type instanceof Type.ArrayType )
  {
    return hasSelfType( ((Type.ArrayType)type).getComponentType() );
  }

  for( Type typeParam: type.getTypeArguments() )
  {
    if( hasSelfType( typeParam ) )
    {
      return true;
    }
  }

  if( type instanceof Type.IntersectionClassType )
  {
    for( Type compType: ((Type.IntersectionClassType)type).getComponents() )
    {
      if( hasSelfType( compType ) )
      {
        return true;
      }
    }
  }

  return false;
}
 
Example 14
Source File: DPrinter.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public void visitCompound(Attribute.Compound a) {
    if (a instanceof Attribute.TypeCompound) {
        Attribute.TypeCompound ta = (Attribute.TypeCompound) a;
        // consider a custom printer?
        printObject("position", ta.position, Details.SUMMARY);
    }
    printObject("synthesized", a.isSynthesized(), Details.SUMMARY);
    printList("values", a.values);
    visitAttribute(a);
}
 
Example 15
Source File: DPrinter.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public void visitCompound(Attribute.Compound a) {
    if (a instanceof Attribute.TypeCompound) {
        Attribute.TypeCompound ta = (Attribute.TypeCompound) a;
        // consider a custom printer?
        printObject("position", ta.position, Details.SUMMARY);
    }
    printObject("synthesized", a.isSynthesized(), Details.SUMMARY);
    printList("values", a.values);
    visitAttribute(a);
}
 
Example 16
Source File: DPrinter.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public void visitCompound(Attribute.Compound a) {
    if (a instanceof Attribute.TypeCompound) {
        Attribute.TypeCompound ta = (Attribute.TypeCompound) a;
        // consider a custom printer?
        printObject("position", ta.position, Details.SUMMARY);
    }
    printObject("synthesized", a.isSynthesized(), Details.SUMMARY);
    printList("values", a.values);
    visitAttribute(a);
}
 
Example 17
Source File: LambdaToMethod.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Translate a lambda into a method to be inserted into the class.
 * Then replace the lambda site with an invokedynamic call of to lambda
 * meta-factory, which will use the lambda method.
 * @param tree
 */
@Override
public void visitLambda(JCLambda tree) {
    LambdaTranslationContext localContext = (LambdaTranslationContext)context;
    MethodSymbol sym = (MethodSymbol)localContext.translatedSym;
    MethodType lambdaType = (MethodType) sym.type;

    {
        Symbol owner = localContext.owner;
        ListBuffer<Attribute.TypeCompound> ownerTypeAnnos = new ListBuffer<Attribute.TypeCompound>();
        ListBuffer<Attribute.TypeCompound> lambdaTypeAnnos = new ListBuffer<Attribute.TypeCompound>();

        for (Attribute.TypeCompound tc : owner.getRawTypeAttributes()) {
            if (tc.position.onLambda == tree) {
                lambdaTypeAnnos.append(tc);
            } else {
                ownerTypeAnnos.append(tc);
            }
        }
        if (lambdaTypeAnnos.nonEmpty()) {
            owner.setTypeAttributes(ownerTypeAnnos.toList());
            sym.setTypeAttributes(lambdaTypeAnnos.toList());
        }
    }

    //create the method declaration hoisting the lambda body
    JCMethodDecl lambdaDecl = make.MethodDef(make.Modifiers(sym.flags_field),
            sym.name,
            make.QualIdent(lambdaType.getReturnType().tsym),
            List.<JCTypeParameter>nil(),
            localContext.syntheticParams,
            lambdaType.getThrownTypes() == null ?
                List.<JCExpression>nil() :
                make.Types(lambdaType.getThrownTypes()),
            null,
            null);
    lambdaDecl.sym = sym;
    lambdaDecl.type = lambdaType;

    //translate lambda body
    //As the lambda body is translated, all references to lambda locals,
    //captured variables, enclosing members are adjusted accordingly
    //to refer to the static method parameters (rather than i.e. acessing to
    //captured members directly).
    lambdaDecl.body = translate(makeLambdaBody(tree, lambdaDecl));

    //Add the method to the list of methods to be added to this class.
    kInfo.addMethod(lambdaDecl);

    //now that we have generated a method for the lambda expression,
    //we can translate the lambda into a method reference pointing to the newly
    //created method.
    //
    //Note that we need to adjust the method handle so that it will match the
    //signature of the SAM descriptor - this means that the method reference
    //should be added the following synthetic arguments:
    //
    // * the "this" argument if it is an instance method
    // * enclosing locals captured by the lambda expression

    ListBuffer<JCExpression> syntheticInits = new ListBuffer<>();

    if (!sym.isStatic()) {
        syntheticInits.append(makeThis(
                sym.owner.enclClass().asType(),
                localContext.owner.enclClass()));
    }

    //add captured locals
    for (Symbol fv : localContext.getSymbolMap(CAPTURED_VAR).keySet()) {
        if (fv != localContext.self) {
            JCTree captured_local = make.Ident(fv).setType(fv.type);
            syntheticInits.append((JCExpression) captured_local);
        }
    }

    //then, determine the arguments to the indy call
    List<JCExpression> indy_args = translate(syntheticInits.toList(), localContext.prev);

    //build a sam instance using an indy call to the meta-factory
    int refKind = referenceKind(sym);

    //convert to an invokedynamic call
    result = makeMetafactoryIndyCall(context, refKind, sym, indy_args);
}
 
Example 18
Source File: LambdaToMethod.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Translate a lambda into a method to be inserted into the class.
 * Then replace the lambda site with an invokedynamic call of to lambda
 * meta-factory, which will use the lambda method.
 * @param tree
 */
@Override
public void visitLambda(JCLambda tree) {
    LambdaTranslationContext localContext = (LambdaTranslationContext)context;
    MethodSymbol sym = (MethodSymbol)localContext.translatedSym;
    MethodType lambdaType = (MethodType) sym.type;

    {
        Symbol owner = localContext.owner;
        ListBuffer<Attribute.TypeCompound> ownerTypeAnnos = new ListBuffer<Attribute.TypeCompound>();
        ListBuffer<Attribute.TypeCompound> lambdaTypeAnnos = new ListBuffer<Attribute.TypeCompound>();

        for (Attribute.TypeCompound tc : owner.getRawTypeAttributes()) {
            if (tc.position.onLambda == tree) {
                lambdaTypeAnnos.append(tc);
            } else {
                ownerTypeAnnos.append(tc);
            }
        }
        if (lambdaTypeAnnos.nonEmpty()) {
            owner.setTypeAttributes(ownerTypeAnnos.toList());
            sym.setTypeAttributes(lambdaTypeAnnos.toList());
        }
    }

    //create the method declaration hoisting the lambda body
    JCMethodDecl lambdaDecl = make.MethodDef(make.Modifiers(sym.flags_field),
            sym.name,
            make.QualIdent(lambdaType.getReturnType().tsym),
            List.<JCTypeParameter>nil(),
            localContext.syntheticParams,
            lambdaType.getThrownTypes() == null ?
                List.<JCExpression>nil() :
                make.Types(lambdaType.getThrownTypes()),
            null,
            null);
    lambdaDecl.sym = sym;
    lambdaDecl.type = lambdaType;

    //translate lambda body
    //As the lambda body is translated, all references to lambda locals,
    //captured variables, enclosing members are adjusted accordingly
    //to refer to the static method parameters (rather than i.e. acessing to
    //captured members directly).
    lambdaDecl.body = translate(makeLambdaBody(tree, lambdaDecl));

    //Add the method to the list of methods to be added to this class.
    kInfo.addMethod(lambdaDecl);

    //now that we have generated a method for the lambda expression,
    //we can translate the lambda into a method reference pointing to the newly
    //created method.
    //
    //Note that we need to adjust the method handle so that it will match the
    //signature of the SAM descriptor - this means that the method reference
    //should be added the following synthetic arguments:
    //
    // * the "this" argument if it is an instance method
    // * enclosing locals captured by the lambda expression

    ListBuffer<JCExpression> syntheticInits = new ListBuffer<>();

    if (!sym.isStatic()) {
        syntheticInits.append(makeThis(
                sym.owner.enclClass().asType(),
                localContext.owner.enclClass()));
    }

    //add captured locals
    for (Symbol fv : localContext.getSymbolMap(CAPTURED_VAR).keySet()) {
        if (fv != localContext.self) {
            JCTree captured_local = make.Ident(fv).setType(fv.type);
            syntheticInits.append((JCExpression) captured_local);
        }
    }

    //then, determine the arguments to the indy call
    List<JCExpression> indy_args = translate(syntheticInits.toList(), localContext.prev);

    //build a sam instance using an indy call to the meta-factory
    int refKind = referenceKind(sym);

    //convert to an invokedynamic call
    result = makeMetafactoryIndyCall(context, refKind, sym, indy_args);
}
 
Example 19
Source File: ClassWriter.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
void writeTypeAnnotation(Attribute.TypeCompound c) {
    writePosition(c.position);
    writeCompoundAttribute(c);
}
 
Example 20
Source File: LambdaToMethod.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Translate a lambda into a method to be inserted into the class.
 * Then replace the lambda site with an invokedynamic call of to lambda
 * meta-factory, which will use the lambda method.
 * @param tree
 */
@Override
public void visitLambda(JCLambda tree) {
    LambdaTranslationContext localContext = (LambdaTranslationContext)context;
    MethodSymbol sym = localContext.translatedSym;
    MethodType lambdaType = (MethodType) sym.type;

    {
        Symbol owner = localContext.owner;
        ListBuffer<Attribute.TypeCompound> ownerTypeAnnos = new ListBuffer<Attribute.TypeCompound>();
        ListBuffer<Attribute.TypeCompound> lambdaTypeAnnos = new ListBuffer<Attribute.TypeCompound>();

        for (Attribute.TypeCompound tc : owner.getRawTypeAttributes()) {
            if (tc.position.onLambda == tree) {
                lambdaTypeAnnos.append(tc);
            } else {
                ownerTypeAnnos.append(tc);
            }
        }
        if (lambdaTypeAnnos.nonEmpty()) {
            owner.setTypeAttributes(ownerTypeAnnos.toList());
            sym.setTypeAttributes(lambdaTypeAnnos.toList());
        }
    }

    //create the method declaration hoisting the lambda body
    JCMethodDecl lambdaDecl = make.MethodDef(make.Modifiers(sym.flags_field),
            sym.name,
            make.QualIdent(lambdaType.getReturnType().tsym),
            List.<JCTypeParameter>nil(),
            localContext.syntheticParams,
            lambdaType.getThrownTypes() == null ?
                List.<JCExpression>nil() :
                make.Types(lambdaType.getThrownTypes()),
            null,
            null);
    lambdaDecl.sym = sym;
    lambdaDecl.type = lambdaType;

    //translate lambda body
    //As the lambda body is translated, all references to lambda locals,
    //captured variables, enclosing members are adjusted accordingly
    //to refer to the static method parameters (rather than i.e. acessing to
    //captured members directly).
    lambdaDecl.body = translate(makeLambdaBody(tree, lambdaDecl));

    //Add the method to the list of methods to be added to this class.
    kInfo.addMethod(lambdaDecl);

    //now that we have generated a method for the lambda expression,
    //we can translate the lambda into a method reference pointing to the newly
    //created method.
    //
    //Note that we need to adjust the method handle so that it will match the
    //signature of the SAM descriptor - this means that the method reference
    //should be added the following synthetic arguments:
    //
    // * the "this" argument if it is an instance method
    // * enclosing locals captured by the lambda expression

    ListBuffer<JCExpression> syntheticInits = new ListBuffer<>();

    if (localContext.methodReferenceReceiver != null) {
        syntheticInits.append(localContext.methodReferenceReceiver);
    } else if (!sym.isStatic()) {
        syntheticInits.append(makeThis(
                sym.owner.enclClass().asType(),
                localContext.owner.enclClass()));
    }

    //add captured locals
    for (Symbol fv : localContext.getSymbolMap(CAPTURED_VAR).keySet()) {
        if (fv != localContext.self) {
            JCTree captured_local = make.Ident(fv).setType(fv.type);
            syntheticInits.append((JCExpression) captured_local);
        }
    }

    //then, determine the arguments to the indy call
    List<JCExpression> indy_args = translate(syntheticInits.toList(), localContext.prev);

    //build a sam instance using an indy call to the meta-factory
    int refKind = referenceKind(sym);

    //convert to an invokedynamic call
    result = makeMetafactoryIndyCall(context, refKind, sym, indy_args);
}