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

The following examples show how to use com.sun.tools.javac.code.TargetType. 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: SrcClassUtil.java    From manifold with Apache License 2.0 6 votes vote down vote up
private void addField( SrcClass srcClass, Symbol sym )
{
  Symbol.VarSymbol field = (Symbol.VarSymbol)sym;
  SrcField srcField = new SrcField( field.name.toString(), makeSrcType( field.type, sym, TargetType.FIELD, -1 ) );
  if( sym.isEnum() )
  {
    srcField.enumConst();
    srcClass.addEnumConst( srcField );
  }
  else
  {
    srcField.modifiers( field.getModifiers() );
    if( Modifier.isFinal( (int)srcField.getModifiers() ) )
    {
      srcField.initializer( new SrcRawExpression( getValueForType( sym.type ) ) );
    }
    srcClass.addField( srcField );
  }
}
 
Example #2
Source File: NullabilityUtil.java    From NullAway with MIT License 5 votes vote down vote up
/**
 * Works for method parameters defined either in source or in class files
 *
 * @param symbol the method symbol
 * @param paramInd index of the parameter
 * @return all declaration and type-use annotations for the parameter
 */
public static Stream<? extends AnnotationMirror> getAllAnnotationsForParameter(
    Symbol.MethodSymbol symbol, int paramInd) {
  Symbol.VarSymbol varSymbol = symbol.getParameters().get(paramInd);
  return Stream.concat(
      varSymbol.getAnnotationMirrors().stream(),
      symbol
          .getRawTypeAttributes()
          .stream()
          .filter(
              t ->
                  t.position.type.equals(TargetType.METHOD_FORMAL_PARAMETER)
                      && t.position.parameter_index == paramInd));
}
 
Example #3
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 #4
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 #5
Source File: JavaEnvironment.java    From j2cl with Apache License 2.0 5 votes vote down vote up
private static TypeDescriptor applyParameterNullabilityAnnotations(
    TypeDescriptor typeDescriptor, ExecutableElement declarationMethodElement, int index) {
  return applyNullabilityAnnotations(
      typeDescriptor,
      declarationMethodElement,
      position ->
          position.parameter_index == index
              && position.type == TargetType.METHOD_FORMAL_PARAMETER);
}
 
Example #6
Source File: JavaEnvironment.java    From j2cl with Apache License 2.0 5 votes vote down vote up
private static TypeDescriptor applyReturnTypeNullabilityAnnotations(
    TypeDescriptor typeDescriptor, ExecutableElement declarationMethodElement) {
  return applyNullabilityAnnotations(
      typeDescriptor,
      declarationMethodElement,
      position -> position.type == TargetType.METHOD_RETURN);
}
 
Example #7
Source File: JavaEnvironment.java    From j2cl with Apache License 2.0 5 votes vote down vote up
public <T extends TypeDescriptor> ImmutableList<T> createTypeDescriptors(
    List<? extends TypeMirror> typeMirrors, Class<T> clazz, Element declarationElement) {
  ImmutableList.Builder<T> typeDescriptorsBuilder = ImmutableList.builder();
  for (int i = 0; i < typeMirrors.size(); i++) {
    final int index = i;
    typeDescriptorsBuilder.add(
        clazz.cast(
            applyNullabilityAnnotations(
                createTypeDescriptor(typeMirrors.get(i), clazz),
                declarationElement,
                position ->
                    position.type == TargetType.CLASS_EXTENDS && position.type_index == index)));
  }
  return typeDescriptorsBuilder.build();
}
 
Example #8
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 #9
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 #10
Source File: SrcClassUtil.java    From manifold with Apache License 2.0 4 votes vote down vote up
private void addMethod( IModule module, SrcClass srcClass, Symbol.MethodSymbol method, BasicJavacTask javacTask )
{
  String name = method.flatName().toString();
  SrcMethod srcMethod = new SrcMethod( srcClass, name.equals( "<init>" ) );
  addAnnotations( srcMethod, method );
  srcMethod.modifiers( method.getModifiers() );
  if( (method.flags() & Flags.VARARGS) != 0 )
  {
    srcMethod.modifiers( srcMethod.getModifiers() | 0x00000080 ); // Modifier.VARARGS
  }
  if( name.equals( "<clinit>" ) )
  {
    return;
  }
  if( !srcMethod.isConstructor() )
  {
    srcMethod.name( name );
    srcMethod.returns( makeSrcType( method.getReturnType(), method, TargetType.METHOD_RETURN, -1 ) );
  }
  for( Symbol.TypeVariableSymbol typeVar: method.getTypeParameters() )
  {
    srcMethod.addTypeVar( makeTypeVarType( typeVar ) );
  }
  List<Symbol.VarSymbol> parameters = method.getParameters();
  for( int i = 0; i < parameters.size(); i++ )
  {
    Symbol.VarSymbol param = parameters.get( i );
    SrcParameter srcParam = new SrcParameter( param.flatName().toString(), makeSrcType( param.type, method, TargetType.METHOD_FORMAL_PARAMETER, i ) );
    srcMethod.addParam( srcParam );
    addAnnotations( srcParam, param );
  }
  List<Type> thrownTypes = method.getThrownTypes();
  for( int i = 0; i < thrownTypes.size(); i++ )
  {
    Type throwType = thrownTypes.get( i );
    srcMethod.addThrowType( makeSrcType( throwType, method, TargetType.THROWS, i ) );
  }
  String bodyStmt;
  if( srcMethod.isConstructor() && !srcClass.isEnum() )
  {
    // Note we can't just throw an exception for the ctor body, the compiler will
    // still complain about the missing super() call if the super class does not have
    // an accessible default ctor. To appease the compiler we generate a super(...)
    // call to the first accessible constructor we can find in the super class.
    bodyStmt = genSuperCtorCall( module, srcClass, javacTask );
  }
  else
  {
    bodyStmt = "throw new RuntimeException();";
  }
  srcMethod.body( new SrcStatementBlock()
    .addStatement(
      new SrcRawStatement()
        .rawText( bodyStmt ) ) );
  srcClass.addMethod( srcMethod );
}
 
Example #11
Source File: SafeTypeAnnotationVisitor.java    From annotation-tools with MIT License 4 votes vote down vote up
/**
 * Checks that the extended information this has visited is valid.
 *
 * @throws InvalidTypeAnnotationException if extended information is
 *  not valid
 */
private void checkX() {
  // First, check to see that only one target type was specified, and
  // then dispatch to checkListSize() based on that target type.
  if (xTargetTypeArgs.size() != 1) {
    throw new
    InvalidTypeAnnotationException("More than one target type visited.");
  }

  if (xNameAndArgsCount != 1) {
    throw new InvalidTypeAnnotationException("Name and args count should "
        + " be visited 1 time, actually visited " + xNameAndArgsCount
        + " times.");
  }

  // Since the correct size of xLocationArgs is specified by
  // xLocationLengthArgs, this information must be looked up first.
  int c = 0;
  if (xLocationLengthArgs.size() > 0) {
    c = xLocationLengthArgs.get(0);
  }

  switch(TargetType.fromTargetTypeValue(xTargetTypeArgs.get(0))) {
  case CAST:
    checkListSize(0, 0, c, 1, 1, 0, 0, 0, 1,
        "Invalid typecast annotation:");
    break;
  case INSTANCEOF:
    checkListSize(0, 0, c, 1, 1, 0, 0, 0, 0,
    "Invalid type test annotation:");
    break;
  case NEW:
    checkListSize(0, 0, c, 1, 1, 0, 0, 0, 0,
    "Invalid object creation annotation:");
    break;
  case METHOD_RECEIVER:
    checkListSize(0, 0, c, 1, 0, 0, 0, 0, 0,
    "Invalid method receiver annotation:");
    break;
  case LOCAL_VARIABLE:
    checkListSize(1, 1, c, 1, 0, 1, 0, 0, 0,
    "Invalid local variable annotation:");
    break;
  case METHOD_RETURN:
    checkListSize(0, 0, c, 1, 0, 0, 0, 0, 0,
    "Invalid method return type annotation:");
    break;
  case METHOD_FORMAL_PARAMETER:
    checkListSize(0, 0, c, 1, 0, 0, 1, 0, 0,
    "Invalid method parameter annotation:");
    break;
  case FIELD:
    checkListSize(0, 0, c, 1, 0, 0, 0, 0, 0,
    "Invalid field annotation:");
    break;
  case CLASS_TYPE_PARAMETER:
    checkListSize(0, 0, c, 1, 0, 0, 1, 0, 0,
    "Invalid class type parameter annotation:");
    break;
  case CLASS_TYPE_PARAMETER_BOUND:
    checkListSize(0, 0, c, 1, 0, 0, 1, 1, 0,
    "Invalid class type parameter bound annotation:");
    break;
  case METHOD_TYPE_PARAMETER:
    checkListSize(0, 0, c, 1, 0, 0, 1, 0, 0,
    "Invalid method type parameter annotation:");
    break;
  case METHOD_TYPE_PARAMETER_BOUND:
    checkListSize(0, 0, c, 1, 0, 0, 1, 1, 0,
    "Invalid method type parameter bound annotation:");
    break;
  case CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT:
  case METHOD_INVOCATION_TYPE_ARGUMENT:
  case CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT:
  case METHOD_REFERENCE_TYPE_ARGUMENT:
    // TODO
    break;
  case CLASS_EXTENDS:
    checkListSize(0, 0, c, 1, 0, 0, 0, 0, 1,
    "Invalid class extends/implements annotation:");
    break;
  case THROWS:
    checkListSize(0, 0, c, 1, 0, 0, 0, 0, 1,
    "Invalid exception type in throws annotation:");
    break;
  default:
    throw new InvalidTypeAnnotationException(
        "Unknown target type given: " + xTargetTypeArgs.get(0));
  }
}
 
Example #12
Source File: ClassAnnotationSceneWriter.java    From annotation-tools with MIT License 4 votes vote down vote up
/**
 * Has xav visit the given target type.
 */
private void visitTargetType(TypeAnnotationVisitor xav, TargetType t) {
  xav.visitXTargetType(t.targetTypeValue());
}