Java Code Examples for com.sun.tools.javac.tree.TreeMaker#Apply

The following examples show how to use com.sun.tools.javac.tree.TreeMaker#Apply . 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 JCExpression replaceStringLiteral( Symbol.ClassSymbol fragSym, JCTree tree, String methodName, String type )
{
  TreeMaker make = _tp.getTreeMaker();
  Names names = Names.instance( _tp.getContext() );

  Symbol.MethodSymbol fragmentValueMethod = resolveMethod( tree.pos(), names.fromString( methodName ),
    fragSym.type, List.nil() );

  JCTree.JCMethodInvocation fragmentValueCall = make.Apply( List.nil(),
    memberAccess( make, _tp.getElementUtil(), fragSym.getQualifiedName() + "." + methodName ), List.nil() );
  fragmentValueCall.type = fragmentValueMethod.getReturnType(); // type
  fragmentValueCall.setPos( tree.pos );
  JCTree.JCFieldAccess newMethodSelect = (JCTree.JCFieldAccess)fragmentValueCall.getMethodSelect();
  newMethodSelect.sym = fragmentValueMethod;
  newMethodSelect.type = fragmentValueMethod.type;
  assignTypes( newMethodSelect.selected, fragSym );

  return fragmentValueCall;
}
 
Example 2
Source File: ExtensionTransformer.java    From manifold with Apache License 2.0 6 votes vote down vote up
private JCExpression classForNameCall( Type type, JCTree tree )
{
  TreeMaker make = _tp.getTreeMaker();
  JavacElements javacElems = _tp.getElementUtil();

  JCTree.JCMethodInvocation typeCall = make.Apply( List.nil(),
    memberAccess( make, javacElems, ReflectUtil.class.getName() + ".type" ),
    List.of( make.Literal( makeLiteralName( type ) ) ) );
  typeCall.setPos( Position.NOPOS );
  typeCall.type = _tp.getSymtab().classType;
  JCTree.JCFieldAccess newMethodSelect = (JCTree.JCFieldAccess)typeCall.getMethodSelect();

  Symbol.ClassSymbol reflectMethodClassSym =
    IDynamicJdk.instance().getTypeElement( _tp.getContext(), _tp.getCompilationUnit(), ReflectUtil.class.getName() );
  Symbol.MethodSymbol typeMethodSymbol = resolveMethod( tree.pos(),
    Names.instance( _tp.getContext() ).fromString( "type" ),
    reflectMethodClassSym.type, List.of( _tp.getSymtab().stringType ) );
  newMethodSelect.sym = typeMethodSymbol;
  newMethodSelect.type = typeMethodSymbol.type;
  newMethodSelect.pos = tree.pos;
  assignTypes( newMethodSelect.selected, reflectMethodClassSym );

  return typeCall;
}
 
Example 3
Source File: ExtensionTransformer.java    From manifold with Apache License 2.0 5 votes vote down vote up
private JCTree replaceWithReflection( JCTree.JCAssign assignTree )
{
  JCTree.JCFieldAccess tree = (JCTree.JCFieldAccess)assignTree.lhs;

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

  boolean isStatic = tree.sym.getModifiers().contains( javax.lang.model.element.Modifier.STATIC );
  if( tree.sym instanceof Symbol.MethodSymbol )
  {
    return assignTree;
  }

  Type type = tree.sym.type;
  Symbol.MethodSymbol reflectMethodSym = findFieldAccessReflectUtilMethod( tree, type, isStatic, true );

  ArrayList<JCExpression> newArgs = new ArrayList<>();
  newArgs.add( isStatic ? makeClassExpr( tree, tree.selected.type ) : tree.selected ); // receiver or class
  newArgs.add( make.Literal( tree.sym.flatName().toString() ) ); // field name
  newArgs.add( assignTree.rhs ); // field value

  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 = type;
  JCTree.JCFieldAccess newMethodSelect = (JCTree.JCFieldAccess)reflectCall.getMethodSelect();
  newMethodSelect.sym = reflectMethodSym;
  newMethodSelect.type = reflectMethodSym.type;
  assignTypes( newMethodSelect.selected, reflectMethodClassSym );

  return reflectCall;
}
 
Example 4
Source File: BootstrapInserter.java    From manifold with Apache License 2.0 5 votes vote down vote up
private JCTree.JCStatement buildBootstrapStaticBlock()
{
  TreeMaker make = _javacJacker.getTreeMaker();
  JavacElements javacElems = _javacJacker.getJavacElements();

  JCTree.JCMethodInvocation bootstrapInitCall = make.Apply( List.nil(), memberAccess( make, javacElems, IBootstrap.class.getName() + ".dasBoot" ), List.nil() );
  return make.Block( Modifier.STATIC, List.of( make.Exec( bootstrapInitCall ) ) );
}
 
Example 5
Source File: ExtensionTransformer.java    From manifold with Apache License 2.0 4 votes vote down vote up
@Override
public void visitUnary( JCTree.JCUnary tree )
{
  super.visitUnary( tree );

  if( _tp.isGenerate() && !shouldProcessForGeneration() )
  {
    // Don't process tree during GENERATE, unless the tree was generated e.g., a bridge method
    return;
  }

  Symbol op = IDynamicJdk.instance().getOperator( tree );
  if( op instanceof OverloadOperatorSymbol ) // handle negation overload
  {
    TreeMaker make = _tp.getTreeMaker();

    // Handle operator overload expressions

    Symbol.MethodSymbol operatorMethod = (Symbol.MethodSymbol)op;
    while( operatorMethod instanceof OverloadOperatorSymbol )
    {
      operatorMethod = ((OverloadOperatorSymbol)operatorMethod).getMethod();
    }

    if( operatorMethod != null )
    {
      JCTree.JCMethodInvocation methodCall;
      JCExpression receiver = tree.getExpression();
      methodCall = make.Apply( List.nil(), make.Select( receiver, operatorMethod ), List.nil() );
      methodCall.setPos( tree.pos );
      methodCall.type = operatorMethod.getReturnType();

      // If methodCall is an extension method, rewrite it accordingly
      Symbol.MethodSymbol extMethod = findExtMethod( methodCall );
      if( extMethod != null )
      {
        // Replace with extension method call
        methodCall = replaceExtCall( methodCall, extMethod );
      }

      result = methodCall;
    }
  }
  else if( isJailbreakReceiver( tree ) )
  {
    Tree.Kind kind = tree.getKind();
    if( kind == Tree.Kind.POSTFIX_INCREMENT || kind == Tree.Kind.POSTFIX_DECREMENT ||
        kind == Tree.Kind.PREFIX_INCREMENT || kind == Tree.Kind.PREFIX_DECREMENT )
    {
      // ++, -- operators not supported with jailbreak access to fields, only direct assignment
      _tp.report( tree, Diagnostic.Kind.ERROR, ExtIssueMsg.MSG_INCREMENT_OP_NOT_ALLOWED_REFLECTION.get() );
      Types types = Types.instance( ((BasicJavacTask)_tp.getJavacTask()).getContext() );
      tree.type = types.createErrorType( tree.type );
    }
    result = tree;
  }
  else
  {
    result = tree;
  }
}
 
Example 6
Source File: ExtensionTransformer.java    From manifold with Apache License 2.0 4 votes vote down vote up
private JCTree replaceStructuralCall( JCTree.JCMethodInvocation theCall )
{
  JCExpression methodSelect = theCall.getMethodSelect();
  if( methodSelect instanceof JCTree.JCFieldAccess )
  {
    int pos = theCall.pos;

    Symtab symbols = _tp.getSymtab();
    Names names = Names.instance( _tp.getContext() );
    Symbol.ClassSymbol reflectMethodClassSym = IDynamicJdk.instance().getTypeElement( _tp.getContext(), _tp.getCompilationUnit(), RuntimeMethods.class.getName() );
    Symbol.MethodSymbol makeInterfaceProxyMethod = resolveMethod( theCall.pos(), names.fromString( "constructProxy" ), reflectMethodClassSym.type,
      List.from( new Type[]{symbols.objectType, symbols.classType} ) );

    JCTree.JCFieldAccess m = (JCTree.JCFieldAccess)methodSelect;
    TreeMaker make = _tp.getTreeMaker();
    JavacElements javacElems = _tp.getElementUtil();
    JCExpression thisArg = m.selected;

    ArrayList<JCExpression> newArgs = new ArrayList<>();
    newArgs.add( thisArg );
    JCTree.JCFieldAccess ifaceClassExpr = (JCTree.JCFieldAccess)memberAccess( make, javacElems, thisArg.type.tsym.getQualifiedName().toString() + ".class" );
    ifaceClassExpr.type = symbols.classType;
    ifaceClassExpr.sym = symbols.classType.tsym;
    ifaceClassExpr.pos = pos;
    assignTypes( ifaceClassExpr.selected, thisArg.type.tsym );
    ifaceClassExpr.selected.pos = pos;
    newArgs.add( ifaceClassExpr );

    JCTree.JCMethodInvocation makeProxyCall = make.Apply( List.nil(), memberAccess( make, javacElems, RuntimeMethods.class.getName() + ".constructProxy" ), List.from( newArgs ) );
    makeProxyCall.setPos( pos );
    makeProxyCall.type = thisArg.type;
    JCTree.JCFieldAccess newMethodSelect = (JCTree.JCFieldAccess)makeProxyCall.getMethodSelect();
    newMethodSelect.sym = makeInterfaceProxyMethod;
    newMethodSelect.type = makeInterfaceProxyMethod.type;
    newMethodSelect.pos = pos;
    assignTypes( newMethodSelect.selected, reflectMethodClassSym );
    newMethodSelect.selected.pos = pos;

    JCTypeCast cast = make.TypeCast( thisArg.type, makeProxyCall );
    cast.type = thisArg.type;
    cast.pos = pos;

    ((JCTree.JCFieldAccess)theCall.meth).selected = cast;

    theCall.pos = pos;

    return theCall;
  }
  return null;
}
 
Example 7
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 8
Source File: ExtensionTransformer.java    From manifold with Apache License 2.0 4 votes vote down vote up
private JCTree replaceWithReflection( JCTree.JCFieldAccess tree )
{
  TreeMaker make = _tp.getTreeMaker();
  JavacElements javacElems = _tp.getElementUtil();

  boolean isStatic = tree.sym.getModifiers().contains( javax.lang.model.element.Modifier.STATIC );
  if( tree.sym instanceof Symbol.MethodSymbol )
  {
    return tree;
  }

  Tree parent = _tp.getParent( tree );
  if( parent instanceof JCTree.JCAssign && ((JCTree.JCAssign)parent).lhs == tree ||
      parent instanceof JCTree.JCAssignOp && ((JCTree.JCAssignOp)parent).lhs == tree )
  {
    // handled in visitAssign() or visitAssignOp()
    return tree;
  }

  if( parent instanceof JCTree.JCUnary && ((JCTree.JCUnary)parent).arg == tree )
  {
    Tree.Kind kind = parent.getKind();

    if( kind != Tree.Kind.UNARY_MINUS && kind != Tree.Kind.UNARY_PLUS &&
        kind != Tree.Kind.LOGICAL_COMPLEMENT && kind != Tree.Kind.BITWISE_COMPLEMENT )
    {
      // supporting -, +, !, ~  not supporting --, ++
      _tp.report( (JCTree)parent, Diagnostic.Kind.ERROR, ExtIssueMsg.MSG_INCREMENT_OP_NOT_ALLOWED_REFLECTION.get() );
      return tree;
    }
  }

  Type type = tree.sym.type;
  if( type instanceof Type.ErrorType )
  {
    // No such field/method
    return tree;
  }

  Symbol.MethodSymbol reflectMethodSym = findFieldAccessReflectUtilMethod( tree, type, isStatic, false );

  ArrayList<JCExpression> newArgs = new ArrayList<>();
  newArgs.add( isStatic ? makeClassExpr( tree, tree.selected.type ) : tree.selected ); // receiver or class
  newArgs.add( make.Literal( tree.sym.flatName().toString() ) ); // field name

  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 = type;
  JCTree.JCFieldAccess newMethodSelect = (JCTree.JCFieldAccess)reflectCall.getMethodSelect();
  newMethodSelect.sym = reflectMethodSym;
  newMethodSelect.type = reflectMethodSym.type;
  assignTypes( newMethodSelect.selected, reflectMethodClassSym );

  return reflectCall;
}
 
Example 9
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;
}