Java Code Examples for com.sun.tools.javac.tree.JCTree#JCNewClass

The following examples show how to use com.sun.tools.javac.tree.JCTree#JCNewClass . 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: SourceAnalyzerFactory.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
@CheckForNull
public Void visitNewClass(@NonNull final NewClassTree node, @NonNull final Map<Pair<BinaryName,String>, UsagesData<String>> p) {
    final Symbol sym = ((JCTree.JCNewClass)node).constructor;
    if (sym != null) {
        final Symbol owner = sym.getEnclosingElement();
        if (owner != null && owner.getKind().isClass()) {
            addUsage(
                owner,
                activeClass.peek(),
                p,
                ClassIndexImpl.UsageType.METHOD_REFERENCE);
        }
    }
    return super.visitNewClass (node,p);
}
 
Example 2
Source File: ExtensionTransformer.java    From manifold with Apache License 2.0 6 votes vote down vote up
@Override
public void visitNewClass( JCTree.JCNewClass tree )
{
  super.visitNewClass( tree );

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

  if( isJailbreakReceiver( tree ) )
  {
    result = replaceWithReflection( tree );
  }
  else
  {
    result = tree;
  }
}
 
Example 3
Source File: ExtensionTransformer.java    From manifold with Apache License 2.0 5 votes vote down vote up
private static boolean isJailbreakReceiver( JCTree.JCNewClass newExpr )
{
  JCExpression classExpr = newExpr.clazz;
  if( classExpr instanceof JCTree.JCAnnotatedType )
  {
    return ((JCTree.JCAnnotatedType)classExpr).annotations.stream()
      .anyMatch( e -> Jailbreak.class.getTypeName().equals( e.attribute.type.toString() ) );
  }
  return false;
}
 
Example 4
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 5
Source File: ModelBuilder.java    From vertx-codetrans with Apache License 2.0 5 votes vote down vote up
@Override
public CodeModel visitNewClass(NewClassTree node, VisitContext context) {
  ClassModel identifier = (ClassModel) scan(node.getIdentifier(), context);
  List<ExpressionModel> arguments = node.getArguments().stream().map(arg -> scan(arg, context)).collect(Collectors.toList());
  JCTree.JCNewClass newClass = (JCTree.JCNewClass) node;
  return identifier.onNew(arguments);
}
 
Example 6
Source File: SingletonJavacHandler.java    From tutorials with MIT License 5 votes vote down vote up
private void addInstanceVar(JavacNode singletonClass, JavacTreeMaker singletonClassTM, JavacNode holderClass) {
    JCTree.JCModifiers fieldMod = singletonClassTM.Modifiers(Flags.PRIVATE | Flags.STATIC | Flags.FINAL);

    JCTree.JCClassDecl singletonClassDecl = (JCTree.JCClassDecl) singletonClass.get();
    JCTree.JCIdent singletonClassType = singletonClassTM.Ident(singletonClassDecl.name);

    JCTree.JCNewClass newKeyword = singletonClassTM.NewClass(null, List.nil(), singletonClassType, List.nil(), null);

    JCTree.JCVariableDecl instanceVar = singletonClassTM.VarDef(fieldMod, singletonClass.toName("INSTANCE"), singletonClassType, newKeyword);
    JavacHandlerUtil.injectField(holderClass, instanceVar);
}
 
Example 7
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 8
Source File: TreeAnalyzer.java    From meghanada-server with GNU General Public License v3.0 4 votes vote down vote up
private static void analyzeNewClass(
    SourceContext context, JCTree.JCNewClass newClass, int preferredPos, int endPos)
    throws IOException {
  Source src = context.source;
  EndPosTable endPosTable = context.endPosTable;
  boolean isParameter = context.parameter;
  boolean isArgument = context.argument;
  int argumentIndex = context.argumentIndex;

  List<JCTree.JCExpression> argumentExpressions = newClass.getArguments();
  java.util.List<String> arguments = getArgumentsType(context, argumentExpressions);

  context.parameter = isParameter;
  context.argument = isArgument;
  context.setArgumentIndex(argumentIndex);

  JCTree.JCExpression identifier = newClass.getIdentifier();
  String name = identifier.toString();

  int start = identifier.getStartPosition();
  int end = identifier.getEndPosition(endPosTable);
  Range nameRange = Range.create(src, start, end);

  Range range = Range.create(src, preferredPos + 4, endPos);
  MethodCall methodCall = new MethodCall(name, preferredPos, nameRange, range, true);

  Type type = identifier.type;
  getTypeString(src, type)
      .ifPresent(
          fqcn -> {
            methodCall.declaringClass = TreeAnalyzer.markFQCN(src, fqcn);
            methodCall.returnType = fqcn;
            methodCall.argumentIndex = argumentIndex;
            context.setArgumentFQCN(fqcn);
          });

  if (isNull(type) || type instanceof Type.ErrorType) {
    // add className to unknown
    ClassName className = new ClassName(name);
    String simpleName = className.getName();
    if (!src.getImportedClassMap().containsKey(simpleName)
        && !CachedASMReflector.getInstance().getGlobalClassIndex().containsKey(simpleName)) {
      src.addUnknown(simpleName);
    }
  }

  JCTree.JCClassDecl classBody = newClass.getClassBody();
  analyzeParsedTree(context, classBody);

  src.getCurrentScope()
      .ifPresent(
          scope -> {
            if (nonNull(arguments)) {
              methodCall.setArguments(arguments);
            }
            scope.addMethodCall(methodCall);
            addUsageIndex(src, methodCall);
          });
}