Java Code Examples for com.sun.tools.javac.util.Names#instance()

The following examples show how to use com.sun.tools.javac.util.Names#instance() . 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 findFieldAccessReflectUtilMethod( JCTree tree, Type type, boolean isStatic, boolean setter )
{
  String name = (setter ? "set" : "get") + "Field" + (isStatic ? "Static" : "") + '_' + typeForReflect( type );

  Symtab symtab = _tp.getSymtab();
  List<Type> paramTypes;
  if( setter )
  {
    paramTypes = List.of( isStatic ? symtab.classType : symtab.objectType, symtab.stringType, type );
  }
  else
  {
    paramTypes = List.of( isStatic ? symtab.classType : symtab.objectType, symtab.stringType );
  }

  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: JavacProcessingEnvironment.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
/** Enter a set of generated class files. */
private List<ClassSymbol> enterClassFiles(Map<String, JavaFileObject> classFiles) {
    ClassReader reader = ClassReader.instance(context);
    Names names = Names.instance(context);
    List<ClassSymbol> list = List.nil();

    for (Map.Entry<String,JavaFileObject> entry : classFiles.entrySet()) {
        Name name = names.fromString(entry.getKey());
        JavaFileObject file = entry.getValue();
        if (file.getKind() != JavaFileObject.Kind.CLASS)
            throw new AssertionError(file);
        ClassSymbol cs;
        if (isPkgInfo(file, JavaFileObject.Kind.CLASS)) {
            Name packageName = Convert.packagePart(name);
            PackageSymbol p = reader.enterPackage(packageName);
            if (p.package_info == null)
                p.package_info = reader.enterClass(Convert.shortName(name), p);
            cs = p.package_info;
            if (cs.classfile == null)
                cs.classfile = file;
        } else
            cs = reader.enterClass(name, file);
        list = list.prepend(cs);
    }
    return list.reverse();
}
 
Example 3
Source File: ConvenientAccessErrorsTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testConvertNameCandidates(Path base) throws Exception {
    Context ctx = new Context();
    Names names = Names.instance(ctx);
    Name name = names.fromString("com.sun.tools.javac.Attr.BreakAttr");

    com.sun.tools.javac.util.List<String> actual =
            Convert.classCandidates(name).map(n -> n.toString());
    List<String> expected = Arrays.asList(
            "com.sun$tools$javac$Attr$BreakAttr",
            "com.sun.tools$javac$Attr$BreakAttr",
            "com.sun.tools.javac$Attr$BreakAttr",
            "com.sun.tools.javac.Attr$BreakAttr",
            "com.sun.tools.javac.Attr.BreakAttr"
    );

    if (!expected.equals(actual)) {
        throw new Exception("Expected names not generated: " + actual);
    }
}
 
Example 4
Source File: TestInvokeDynamic.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public void run() {
    int id = checkCount.incrementAndGet();
    JavaSource source = new JavaSource(id);
    JavacTaskImpl ct = (JavacTaskImpl)comp.getTask(null, fm.get(), dc,
            Arrays.asList("-g"), null, Arrays.asList(source));
    Context context = ct.getContext();
    Symtab syms = Symtab.instance(context);
    Names names = Names.instance(context);
    Types types = Types.instance(context);
    ct.addTaskListener(new Indifier(syms, names, types));
    try {
        ct.generate();
    } catch (Throwable t) {
        t.printStackTrace();
        throw new AssertionError(
                String.format("Error thrown when compiling following code\n%s",
                source.source));
    }
    if (dc.diagFound) {
        throw new AssertionError(
                String.format("Diags found when compiling following code\n%s\n\n%s",
                source.source, dc.printDiags()));
    }
    verifyBytecode(id);
}
 
Example 5
Source File: Enter.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
protected Enter(Context context) {
    context.put(enterKey, this);

    log = Log.instance(context);
    reader = ClassReader.instance(context);
    make = TreeMaker.instance(context);
    syms = Symtab.instance(context);
    chk = Check.instance(context);
    memberEnter = MemberEnter.instance(context);
    types = Types.instance(context);
    annotate = Annotate.instance(context);
    lint = Lint.instance(context);
    names = Names.instance(context);

    predefClassDef = make.ClassDef(
        make.Modifiers(PUBLIC),
        syms.predefClass.name, null, null, null, null);
    predefClassDef.sym = syms.predefClass;
    todo = Todo.instance(context);
    fileManager = context.get(JavaFileManager.class);

    Options options = Options.instance(context);
    pkginfoOpt = PkgInfo.get(options);
}
 
Example 6
Source File: NameReplacer.java    From manifold with Apache License 2.0 5 votes vote down vote up
@Override
public void visitSelect( JCTree.JCFieldAccess jcFieldAccess )
{
  super.visitSelect( jcFieldAccess );
  Names names = Names.instance( _javacTask.getContext() );
  jcFieldAccess.name = names.fromString( jcFieldAccess.name.toString() );
  jcFieldAccess.pos = _offset;
}
 
Example 7
Source File: JavacTrees.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private void init(Context context) {
    attr = Attr.instance(context);
    enter = Enter.instance(context);
    elements = JavacElements.instance(context);
    log = Log.instance(context);
    resolve = Resolve.instance(context);
    treeMaker = TreeMaker.instance(context);
    memberEnter = MemberEnter.instance(context);
    names = Names.instance(context);
    types = Types.instance(context);

    JavacTask t = context.get(JavacTask.class);
    if (t instanceof JavacTaskImpl)
        javacTaskImpl = (JavacTaskImpl) t;
}
 
Example 8
Source File: ParserFactory.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
protected ParserFactory(Context context) {
    super();
    context.put(parserFactoryKey, this);
    this.F = TreeMaker.instance(context);
    this.log = Log.instance(context);
    this.names = Names.instance(context);
    this.keywords = Keywords.instance(context);
    this.source = Source.instance(context);
    this.options = Options.instance(context);
    this.scannerFactory = ScannerFactory.instance(context);
}
 
Example 9
Source File: Corraller.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private Corraller(StringWriter out, int keyIndex, Context context) {
    super(out, false);
    this.out = out;
    this.keyIndex = keyIndex;
    this.make = TreeMaker.instance(context);
    this.names = Names.instance(context);
}
 
Example 10
Source File: ParserFactory.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
protected ParserFactory(Context context) {
    super();
    context.put(parserFactoryKey, this);
    this.F = TreeMaker.instance(context);
    this.docTreeMaker = DocTreeMaker.instance(context);
    this.log = Log.instance(context);
    this.names = Names.instance(context);
    this.tokens = Tokens.instance(context);
    this.source = Source.instance(context);
    this.options = Options.instance(context);
    this.scannerFactory = ScannerFactory.instance(context);
    this.locale = context.get(Locale.class);
}
 
Example 11
Source File: ScannerFactory.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/** Create a new scanner factory. */
protected ScannerFactory(Context context) {
    context.put(scannerFactoryKey, this);
    this.log = Log.instance(context);
    this.names = Names.instance(context);
    this.source = Source.instance(context);
    this.tokens = Tokens.instance(context);
}
 
Example 12
Source File: Types.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
protected Types(Context context) {
    context.put(typesKey, this);
    syms = Symtab.instance(context);
    names = Names.instance(context);
    Source source = Source.instance(context);
    allowBoxing = source.allowBoxing();
    allowCovariantReturns = source.allowCovariantReturns();
    allowObjectToPrimitiveCast = source.allowObjectToPrimitiveCast();
    reader = ClassReader.instance(context);
    chk = Check.instance(context);
    capturedName = names.fromString("<captured wildcard>");
    messages = JavacMessages.instance(context);
}
 
Example 13
Source File: JavacTrees.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private void init(Context context) {
    attr = Attr.instance(context);
    enter = Enter.instance(context);
    elements = JavacElements.instance(context);
    log = Log.instance(context);
    resolve = Resolve.instance(context);
    treeMaker = TreeMaker.instance(context);
    memberEnter = MemberEnter.instance(context);
    names = Names.instance(context);
    types = Types.instance(context);

    JavacTask t = context.get(JavacTask.class);
    if (t instanceof JavacTaskImpl)
        javacTaskImpl = (JavacTaskImpl) t;
}
 
Example 14
Source File: Enter.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected Enter(Context context) {
    context.put(enterKey, this);

    log = Log.instance(context);
    make = TreeMaker.instance(context);
    syms = Symtab.instance(context);
    chk = Check.instance(context);
    typeEnter = TypeEnter.instance(context);
    types = Types.instance(context);
    annotate = Annotate.instance(context);
    lint = Lint.instance(context);
    names = Names.instance(context);
    modules = Modules.instance(context);
    diags = JCDiagnostic.Factory.instance(context);

    predefClassDef = make.ClassDef(
        make.Modifiers(PUBLIC),
        syms.predefClass.name,
        List.nil(),
        null,
        List.nil(),
        List.nil());
    predefClassDef.sym = syms.predefClass;
    todo = Todo.instance(context);
    fileManager = context.get(JavaFileManager.class);

    Options options = Options.instance(context);
    pkginfoOpt = PkgInfo.get(options);
    typeEnvs = TypeEnvs.instance(context);
}
 
Example 15
Source File: JavacTrees.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private void init(Context context) {
    attr = Attr.instance(context);
    enter = Enter.instance(context);
    elements = JavacElements.instance(context);
    log = Log.instance(context);
    resolve = Resolve.instance(context);
    treeMaker = TreeMaker.instance(context);
    memberEnter = MemberEnter.instance(context);
    names = Names.instance(context);
    types = Types.instance(context);

    JavacTask t = context.get(JavacTask.class);
    if (t instanceof JavacTaskImpl)
        javacTaskImpl = (JavacTaskImpl) t;
}
 
Example 16
Source File: StringLiteralTemplateProcessor.java    From manifold with Apache License 2.0 4 votes vote down vote up
public List<JCTree.JCExpression> parse( String stringValue, int literalOffset )
{
  List<StringLiteralTemplateParser.Expr> comps =
    StringLiteralTemplateParser.parse( new EscapeMatcher( _manDiagnosticHandler, literalOffset+1 ), stringValue );
  if( comps.isEmpty() )
  {
    return Collections.emptyList();
  }

  TreeMaker maker = TreeMaker.instance( _javacTask.getContext() );
  Names names = Names.instance( _javacTask.getContext() );

  List<JCTree.JCExpression> exprs = new ArrayList<>();
  StringLiteralTemplateParser.Expr prev = null;
  for( StringLiteralTemplateParser.Expr comp : comps )
  {
    JCTree.JCExpression expr;
    if( comp.isVerbatim() )
    {
      expr = maker.Literal( comp.getExpr() );
    }
    else
    {
      if( prev != null && !prev.isVerbatim() )
      {
        // enforce concatenation
        exprs.add( maker.Literal( "" ) );
      }

      int exprPos = literalOffset + 1 + comp.getOffset();

      if( comp.isIdentifier() )
      {
        JCTree.JCIdent ident = maker.Ident( names.fromString( comp.getExpr() ) );
        ident.pos = exprPos;
        expr = ident;
      }
      else
      {
        DiagnosticCollector<JavaFileObject> errorHandler = new DiagnosticCollector<>();
        expr = _tp.getHost().getJavaParser().parseExpr( comp.getExpr(), errorHandler );
        if( transferParseErrors( literalOffset, comp, expr, errorHandler ) )
        {
          return Collections.emptyList();
        }
        replaceNames( expr, exprPos );
      }
    }
    prev = comp;
    exprs.add( expr );
  }

  if( exprs.size() == 1 )
  {
    // insert an empty string so concat will make the expr a string
    exprs.add( 0, maker.Literal( "" ) );
  }

  return exprs;
}
 
Example 17
Source File: Inliner.java    From Refaster with Apache License 2.0 4 votes vote down vote up
public Names names() {
  return Names.instance(context);
}
 
Example 18
Source File: T6889255.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
void test(String testName, boolean expectNames, String... opts) throws Exception {
    System.err.println("Test " + testName
            + ": expectNames:" + expectNames
            + " javacOpts:" + Arrays.asList(opts));

    File outDir = new File(testName);
    outDir.mkdirs();
    compile(outDir, opts);

    Context ctx = new Context();
    JavacFileManager fm = new JavacFileManager(ctx, true, null);
    fm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(outDir));
    Symtab syms = Symtab.instance(ctx);
    ClassReader cr = ClassReader.instance(ctx);
    cr.saveParameterNames = true;
    Names names = Names.instance(ctx);

    Set<String> classes = getTopLevelClasses(outDir);
    Deque<String> work = new LinkedList<String>(classes);
    String classname;
    while ((classname = work.poll()) != null) {
        System.err.println("Checking class " + classname);
        ClassSymbol sym = syms.enterClass(syms.noModule, names.table.fromString(classname));
        sym.complete();

        if ((sym.flags() & Flags.INTERFACE) != 0 && !testInterfaces)
            continue;

        for (Symbol s : sym.members_field.getSymbols(NON_RECURSIVE)) {
            System.err.println("Checking member " + s);
            switch (s.kind) {
                case TYP: {
                    String name = s.flatName().toString();
                    if (!classes.contains(name)) {
                        classes.add(name);
                        work.add(name);
                    }
                    break;
                }
                case MTH:
                    verify((MethodSymbol) s, expectNames);
                    break;
            }

        }
    }
}
 
Example 19
Source File: T6889255.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
void test(String testName, boolean expectNames, String... opts) throws Exception {
    System.err.println("Test " + testName
            + ": expectNames:" + expectNames
            + " javacOpts:" + Arrays.asList(opts));

    File outDir = new File(testName);
    outDir.mkdirs();
    compile(outDir, opts);

    Context ctx = new Context();
    JavacFileManager fm = new JavacFileManager(ctx, true, null);
    fm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(outDir));
    ClassReader cr = ClassReader.instance(ctx);
    cr.saveParameterNames = true;
    Names names = Names.instance(ctx);

    Set<String> classes = getTopLevelClasses(outDir);
    Deque<String> work = new LinkedList<String>(classes);
    String classname;
    while ((classname = work.poll()) != null) {
        System.err.println("Checking class " + classname);
        ClassSymbol sym = cr.enterClass(names.table.fromString(classname));
        sym.complete();

        if ((sym.flags() & Flags.INTERFACE) != 0 && !testInterfaces)
            continue;

        for (Scope.Entry e = sym.members_field.elems; e != null; e = e.sibling) {
            System.err.println("Checking member " + e.sym);
            switch (e.sym.kind) {
                case Kinds.TYP: {
                    String name = e.sym.flatName().toString();
                    if (!classes.contains(name)) {
                        classes.add(name);
                        work.add(name);
                    }
                    break;
                }
                case Kinds.MTH:
                    verify((MethodSymbol) e.sym, expectNames);
                    break;
            }

        }
    }
}
 
Example 20
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;
}