Java Code Examples for com.sun.tools.javac.code.Type#ArrayType

The following examples show how to use com.sun.tools.javac.code.Type#ArrayType . 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: ExtensionTransformer.java    From manifold with Apache License 2.0 6 votes vote down vote up
private Symbol.MethodSymbol findReflectUtilMethod( JCTree tree, Type returnType, boolean isStatic )
{
  String name = "invoke" + (isStatic ? "Static" : "") + '_' + typeForReflect( returnType );

  Symtab symtab = _tp.getSymtab();
  Type.ArrayType classArrayType = new Type.ArrayType( symtab.classType, symtab.arrayClass );
  Type.ArrayType objectArrayType = new Type.ArrayType( symtab.objectType, symtab.arrayClass );
  List<Type> paramTypes;
  if( isStatic )
  {
    paramTypes = List.of( symtab.classType, symtab.stringType, classArrayType, objectArrayType );
  }
  else
  {
    paramTypes = List.of( symtab.objectType, symtab.stringType, classArrayType, objectArrayType );
  }

  Names names = Names.instance( _tp.getContext() );
  Symbol.ClassSymbol reflectMethodClassSym =
    IDynamicJdk.instance().getTypeElement( _tp.getContext(), _tp.getCompilationUnit(), ReflectionRuntimeMethods.class.getName() );

  return resolveMethod( tree.pos(), names.fromString( name ), reflectMethodClassSym.type, paramTypes );
}
 
Example 2
Source File: ExtensionTransformer.java    From manifold with Apache License 2.0 5 votes vote down vote up
private String makeLiteralName( Type type )
{
  StringBuilder sb = new StringBuilder();
  for( ;type instanceof Type.ArrayType; type = ((Type.ArrayType)type).getComponentType() )
  {
    sb.append( "[]" );
  }
  return type.tsym.flatName() + sb.toString();
}
 
Example 3
Source File: JavacBinder.java    From manifold with Apache License 2.0 5 votes vote down vote up
@Override
public Void visitArrayType( Type.ArrayType t, Type pt )
{
  if( pt instanceof Type.ArrayType )
  {
    fetchTypeVars( t.getComponentType(), ((Type.ArrayType)pt).getComponentType(), _map );
  }
  return null;
}
 
Example 4
Source File: ManTypes.java    From manifold with Apache License 2.0 5 votes vote down vote up
private Type makeArray( Type unannotatedType, Type receiverType )
{
  if( unannotatedType instanceof Type.ArrayType )
  {
    return makeArray( ((Type.ArrayType)unannotatedType).getComponentType(), new Type.ArrayType( receiverType, _syms.arrayClass ) );
  }
  return receiverType;
}
 
Example 5
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 6
Source File: StructuralTypeEraser.java    From manifold with Apache License 2.0 5 votes vote down vote up
@Override
public Type visitArrayType( Type.ArrayType t, Void aVoid )
{
  Type compType = visit( t.getComponentType() );
  if( compType == t.getComponentType() )
  {
    return t;
  }
  return new Type.ArrayType( compType, t.tsym );
}
 
Example 7
Source File: ExtensionTransformer.java    From manifold with Apache License 2.0 5 votes vote down vote up
private Symbol.MethodSymbol findReflectUtilConstructor( JCTree.JCNewClass tree )
{
  Symtab symtab = _tp.getSymtab();
  Type.ArrayType classArrayType = new Type.ArrayType( symtab.classType, symtab.arrayClass );
  Type.ArrayType objectArrayType = new Type.ArrayType( symtab.objectType, symtab.arrayClass );
  List<Type> paramTypes = List.of( symtab.classType, classArrayType, objectArrayType );

  Symbol.ClassSymbol reflectMethodClassSym =
    IDynamicJdk.instance().getTypeElement( _tp.getContext(), _tp.getCompilationUnit(), ReflectionRuntimeMethods.class.getName() );

  Names names = Names.instance( _tp.getContext() );
  return resolveMethod( tree.pos(), names.fromString( "construct" ), reflectMethodClassSym.type, paramTypes );
}
 
Example 8
Source File: ExtensionTransformer.java    From manifold with Apache License 2.0 5 votes vote down vote up
private boolean isPrimitiveOrPrimitiveArray( Type type )
{
  while( type instanceof Type.ArrayType )
  {
    type = ((Type.ArrayType)type).getComponentType();
  }
  return type.isPrimitive();
}
 
Example 9
Source File: JNIWriter.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public Type visitArrayType(Type.ArrayType t, StringBuilder s) {
    s.append("[");
    return t.getComponentType().accept(this, s);
}
 
Example 10
Source File: ExtensionTransformer.java    From manifold with Apache License 2.0 4 votes vote down vote up
private JCTree replaceWithReflection( JCTree.JCNewClass tree )
{
  if( tree.constructor == null )
  {
    return tree;
  }

  TreeMaker make = _tp.getTreeMaker();
  JavacElements javacElems = _tp.getElementUtil();

  Type type = ((JCTree.JCAnnotatedType)tree.clazz).underlyingType.type;

  if( tree.constructor instanceof Symbol.ClassSymbol )
  {
    //assert tree.constructor.kind == com.sun.tools.javac.code.Kinds.ERR;
    return tree;
  }

  List<Symbol.VarSymbol> parameters = ((Symbol.MethodSymbol)tree.constructor).getParameters();
  ArrayList<JCExpression> paramTypes = new ArrayList<>();
  for( Symbol.VarSymbol param: parameters )
  {
    paramTypes.add( makeClassExpr( tree, param.type ) );
  }
  Symtab symTab = _tp.getSymtab();
  JCTree.JCNewArray paramTypesArray = make.NewArray(
    make.Type( symTab.classType ), List.nil(), List.from( paramTypes ) );
  paramTypesArray.type = new Type.ArrayType( symTab.classType, symTab.arrayClass );

  JCTree.JCNewArray argsArray = make.NewArray(
    make.Type( symTab.objectType ), List.nil(), tree.getArguments() );
  argsArray.type = new Type.ArrayType( symTab.objectType, symTab.arrayClass );

  ArrayList<JCExpression> newArgs = new ArrayList<>();
  newArgs.add( makeClassExpr( tree, type ) ); // the class
  newArgs.add( paramTypesArray ); // param types
  newArgs.add( argsArray ); // args


  Symbol.ClassSymbol reflectMethodClassSym =
    IDynamicJdk.instance().getTypeElement( _tp.getContext(), _tp.getCompilationUnit(), ReflectionRuntimeMethods.class.getName() );

  Symbol.MethodSymbol reflectMethodSym = findReflectUtilConstructor( tree );
  JCTree.JCMethodInvocation reflectCall =
    make.Apply( List.nil(),
      memberAccess( make, javacElems, ReflectionRuntimeMethods.class.getName() + "." + reflectMethodSym.flatName().toString() ),
      List.from( newArgs ) );
  reflectCall.setPos( tree.pos );
  reflectCall.type = type;
  JCTree.JCFieldAccess newMethodSelect = (JCTree.JCFieldAccess)reflectCall.getMethodSelect();
  newMethodSelect.sym = reflectMethodSym;
  newMethodSelect.type = reflectMethodSym.type;
  assignTypes( newMethodSelect.selected, reflectMethodClassSym );

  return reflectCall;
}
 
Example 11
Source File: ExtensionTransformer.java    From manifold with Apache License 2.0 4 votes vote down vote up
private JCTree.JCMethodInvocation replaceWithReflection( JCTree.JCMethodInvocation tree )
{
  //## todo: maybe try to avoid reflection if the method is accessible -- at least check if the method and its enclosing nest of classes are all public

  Type type = tree.getMethodSelect().type;
  if( type instanceof Type.ErrorType )
  {
    // No such field/method or wrong params
    return tree;
  }

  TreeMaker make = _tp.getTreeMaker();
  JavacElements javacElems = _tp.getElementUtil();

  JCExpression methodSelect = tree.getMethodSelect();
  if( methodSelect instanceof JCTree.JCFieldAccess )
  {
    JCTree.JCFieldAccess m = (JCTree.JCFieldAccess)methodSelect;
    boolean isStatic = m.sym.getModifiers().contains( javax.lang.model.element.Modifier.STATIC );
    if( !(m.sym instanceof Symbol.MethodSymbol) )
    {
      return tree;
    }
    Type returnType = ((Symbol.MethodSymbol)m.sym).getReturnType();
    Symbol.MethodSymbol reflectMethodSym = findReflectUtilMethod( tree, returnType, isStatic );

    List<Symbol.VarSymbol> parameters = ((Symbol.MethodSymbol)m.sym).getParameters();
    ArrayList<JCExpression> paramTypes = new ArrayList<>();
    for( Symbol.VarSymbol param: parameters )
    {
      JCExpression classExpr = makeClassExpr( tree, param.type );
      paramTypes.add( classExpr );
    }
    Symtab symTab = _tp.getSymtab();
    JCTree.JCNewArray paramTypesArray = make.NewArray(
      make.Type( symTab.classType ), List.nil(), List.from( paramTypes ) );
    paramTypesArray.type = new Type.ArrayType( symTab.classType, symTab.arrayClass );

    JCTree.JCNewArray argsArray = make.NewArray(
      make.Type( symTab.objectType ), List.nil(), tree.getArguments() );
    argsArray.type = new Type.ArrayType( symTab.objectType, symTab.arrayClass );

    ArrayList<JCExpression> newArgs = new ArrayList<>();
    newArgs.add( isStatic ? makeClassExpr( tree, m.selected.type ) : m.selected ); // receiver or class
    newArgs.add( make.Literal( m.sym.flatName().toString() ) ); // method name
    newArgs.add( paramTypesArray ); // param types
    newArgs.add( argsArray ); // args

    Symbol.ClassSymbol reflectMethodClassSym =
      IDynamicJdk.instance().getTypeElement( _tp.getContext(), _tp.getCompilationUnit(), ReflectionRuntimeMethods.class.getName() );

    JCTree.JCMethodInvocation reflectCall =
      make.Apply( List.nil(),
        memberAccess( make, javacElems, ReflectionRuntimeMethods.class.getName() + "." + reflectMethodSym.flatName().toString() ),
        List.from( newArgs ) );
    reflectCall.setPos( tree.pos );
    reflectCall.type = returnType;
    reflectCall.pos = tree.pos;
    JCTree.JCFieldAccess newMethodSelect = (JCTree.JCFieldAccess)reflectCall.getMethodSelect();
    newMethodSelect.sym = reflectMethodSym;
    newMethodSelect.type = reflectMethodSym.type;
    assignTypes( newMethodSelect.selected, reflectMethodClassSym );
    newMethodSelect.pos = tree.pos;
    return reflectCall;
  }
  return tree;
}
 
Example 12
Source File: DynamicProxyFactory.java    From manifold with Apache License 2.0 4 votes vote down vote up
private static boolean hasCallMethod( BasicJavacTask javacTask, Symbol.ClassSymbol classSymbol )
{
  Name call = Names.instance( javacTask.getContext() ).fromString( "call" );
  Iterable<Symbol> elems = IDynamicJdk.instance().getMembersByName( classSymbol, call );
  for( Symbol s : elems )
  {
    if( s instanceof Symbol.MethodSymbol )
    {
      List<Symbol.VarSymbol> parameters = ((Symbol.MethodSymbol)s).getParameters();
      if( parameters.size() != 6 )
      {
        return false;
      }

      Symtab symbols = Symtab.instance( javacTask.getContext() );
      Types types = Types.instance( javacTask.getContext() );
      return types.erasure( parameters.get( 0 ).asType() ).equals( types.erasure( symbols.classType ) ) &&
             parameters.get( 1 ).asType().equals( symbols.stringType ) &&
             parameters.get( 2 ).asType().equals( symbols.stringType ) &&
             types.erasure( parameters.get( 3 ).asType() ).equals( types.erasure( symbols.classType ) ) &&
             parameters.get( 4 ).asType() instanceof Type.ArrayType && types.erasure( ((Type.ArrayType)parameters.get( 4 ).asType()).getComponentType() ).equals( types.erasure( symbols.classType ) ) &&
             parameters.get( 5 ).asType() instanceof Type.ArrayType && ((Type.ArrayType)parameters.get( 5 ).asType()).getComponentType().equals( symbols.objectType );
    }
  }
  Type superclass = classSymbol.getSuperclass();
  if( !(superclass instanceof NoType) )
  {
    if( hasCallMethod( javacTask, (Symbol.ClassSymbol)superclass.tsym ) )
    {
      return true;
    }
  }
  for( Type iface : classSymbol.getInterfaces() )
  {
    if( hasCallMethod( javacTask, (Symbol.ClassSymbol)iface.tsym ) )
    {
      return true;
    }
  }
  return false;
}
 
Example 13
Source File: JNIWriter.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public R visitArrayType(Type.ArrayType t, P p) {
    return defaultAction(t, p);
}
 
Example 14
Source File: SrcClassUtil.java    From manifold with Apache License 2.0 4 votes vote down vote up
private String typeNoAnnotations( Type type )
{
  if( isJava8() )
  {
    return type.toString();
  }

  StringBuilder sb = new StringBuilder();
  if( type instanceof Type.ClassType )
  {
    if( type.getEnclosingType().hasTag( CLASS ) &&
        ReflectUtil.field( type.tsym.owner, "kind" ).get() == ReflectUtil.field( "com.sun.tools.javac.code.Kinds$Kind", "TYP" ).getStatic() )
    {
      sb.append( typeNoAnnotations( type.getEnclosingType() ) );
      sb.append( "." );
      sb.append( ReflectUtil.method( type, "className", Symbol.class, boolean.class ).invoke( type.tsym, false ) );
    }
    else
    {
      sb.append( ReflectUtil.method( type, "className", Symbol.class, boolean.class ).invoke( type.tsym, true ) );
    }

    List<Type> typeArgs = type.getTypeArguments();
    if( typeArgs.nonEmpty() )
    {
      sb.append( '<' );
      for( int i = 0; i < typeArgs.size(); i++ )
      {
        if( i > 0 )
        {
          sb.append( ", " );
        }
        Type typeArg = typeArgs.get( i );
        sb.append( typeNoAnnotations( typeArg ) );
      }
      sb.append( ">" );
    }
  }
  else if( type instanceof Type.ArrayType )
  {
    sb.append( typeNoAnnotations( ((Type.ArrayType)type).getComponentType() ) ).append( "[]" );
  }
  else if( type instanceof Type.WildcardType )
  {
    Type.WildcardType wildcardType = (Type.WildcardType)type;
    BoundKind kind = wildcardType.kind;
    sb.append( kind.toString() );
    if( kind != BoundKind.UNBOUND )
    {
      sb.append( typeNoAnnotations( wildcardType.type ) );
    }
  }
  else
  {
    sb.append( type.toString() );
  }
  return sb.toString();
}
 
Example 15
Source File: JNIWriter.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Type visitArrayType(Type.ArrayType t, StringBuilder s) {
    s.append("[");
    return t.getComponentType().accept(this, s);
}
 
Example 16
Source File: TypeMirrorHandle.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public Void visitArrayType(Type.ArrayType t, Void s) {
    if (t.elemtype != null)
        t.elemtype.accept(this, s);
    return null;
}
 
Example 17
Source File: JNIWriter.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public R visitArrayType(Type.ArrayType t, P p) {
    return defaultAction(t, p);
}