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

The following examples show how to use com.sun.tools.javac.util.Names#fromString() . 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: JavacProcessingEnvironment.java    From jdk8u60 with GNU General Public License v2.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 2
Source File: SampleJavacPlugin.java    From tutorials with MIT License 6 votes vote down vote up
private static JCTree.JCBlock createIfBlock(TreeMaker factory, Names symbolsTable, VariableTree parameter) {
    String parameterName = parameter.getName().toString();
    Name parameterId = symbolsTable.fromString(parameterName);
    
    String errorMessagePrefix = String.format("Argument '%s' of type %s is marked by @%s but got '", 
      parameterName, parameter.getType(), Positive.class.getSimpleName());
    String errorMessageSuffix = "' for it";
    
    return factory.Block(0, com.sun.tools.javac.util.List.of(
      factory.Throw(
        factory.NewClass(null, nil(), 
          factory.Ident(symbolsTable.fromString(IllegalArgumentException.class.getSimpleName())),
            com.sun.tools.javac.util.List.of(factory.Binary(JCTree.Tag.PLUS, 
              factory.Binary(JCTree.Tag.PLUS, factory.Literal(TypeTag.CLASS, errorMessagePrefix), 
                factory.Ident(parameterId)), 
                factory.Literal(TypeTag.CLASS, errorMessageSuffix))), null))));
}
 
Example 3
Source File: JavacProcessingEnvironment.java    From openjdk-8 with GNU General Public License v2.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 4
Source File: JavacProcessingEnvironment.java    From openjdk-8-source with GNU General Public License v2.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 5
Source File: JavacProcessingEnvironment.java    From hottub with GNU General Public License v2.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 6
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 7
Source File: JavacProcessingEnvironment.java    From javaide with GNU General Public License v3.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 8
Source File: JavacProcessingEnvironment.java    From openjdk-jdk8u-backup with GNU General Public License v2.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 9
Source File: ElementUtils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static TypeElement getTypeElementByBinaryName(JavacTask task, ModuleElement mod, String name) {
    Context ctx = ((JavacTaskImpl) task).getContext();
    Names names = Names.instance(ctx);
    Symtab syms = Symtab.instance(ctx);
    Check chk = Check.instance(ctx);
    final Name wrappedName = names.fromString(name);
    ClassSymbol clazz = chk.getCompiled((ModuleSymbol) mod, wrappedName);
    if (clazz != null) {
        return clazz;
    }
    clazz = syms.enterClass((ModuleSymbol) mod, wrappedName);
    
    try {
        clazz.complete();
        
        if (clazz.kind == Kind.TYP &&
            clazz.flatName() == wrappedName) {
            return clazz;
        }
    } catch (CompletionFailure cf) {
    }

    return null;
}
 
Example 10
Source File: JavacProcessingEnvironment.java    From openjdk-jdk8u with GNU General Public License v2.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 11
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 12
Source File: JavacProcessingEnvironment.java    From TencentKona-8 with GNU General Public License v2.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 13
Source File: LibraryModelsHandler.java    From NullAway with MIT License 5 votes vote down vote up
private <T> NameIndexedMap<T> makeOptimizedLookup(
    Names names, Set<MethodRef> refs, Function<MethodRef, T> getValForRef) {
  Map<Name, Map<MethodRef, T>> nameMapping = new LinkedHashMap<>();
  for (MethodRef ref : refs) {
    Name methodName = names.fromString(ref.methodName);
    Map<MethodRef, T> mapForName = nameMapping.get(methodName);
    if (mapForName == null) {
      mapForName = new LinkedHashMap<>();
      nameMapping.put(methodName, mapForName);
    }
    mapForName.put(ref, getValForRef.apply(ref));
  }
  return new NameIndexedMap<>(nameMapping);
}
 
Example 14
Source File: NBNames.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected NBNames(Context context) {
    Names n = Names.instance(context);

    _org_netbeans_EnclosingMethod = n.fromString("org.netbeans.EnclosingMethod");
    _org_netbeans_TypeSignature = n.fromString("org.netbeans.TypeSignature");
    _org_netbeans_ParameterNames = n.fromString("org.netbeans.ParameterNames");
    _org_netbeans_SourceLevelAnnotations = n.fromString("org.netbeans.SourceLevelAnnotations");
    _org_netbeans_SourceLevelParameterAnnotations = n.fromString("org.netbeans.SourceLevelParameterAnnotations");
    _org_netbeans_SourceLevelTypeAnnotations = n.fromString("org.netbeans.SourceLevelTypeAnnotations");
}
 
Example 15
Source File: PubApiExtractor.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public PubApi getPubApi(String fullyQualifiedClassName) {
    Symtab syms = Symtab.instance(context);
    ClassFinder cr = ClassFinder.instance(context);
    Names ns = Names.instance(context);
    Name n = ns.fromString(fullyQualifiedClassName);
    ClassSymbol cs = cr.loadClass(syms.inferModule(Convert.packagePart(n)), n);
    PubapiVisitor v = new PubapiVisitor();
    v.visit(cs);
    return v.getCollectedPubApi();
}
 
Example 16
Source File: Utilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public JackpotJavacParser(Context ctx, NBParserFactory fac,
                 Lexer S,
                 boolean keepDocComments,
                 boolean keepLineMap,
                 CancelService cancelService,
                 Names names) {
    super(fac, S, keepDocComments, keepLineMap, true, false, cancelService);
    this.ctx = ctx;
    this.make = com.sun.tools.javac.tree.TreeMaker.instance(ctx);
    this.dollar = names.fromString("$");
}
 
Example 17
Source File: NameReplacer.java    From manifold with Apache License 2.0 5 votes vote down vote up
@Override
public void visitIdent( JCTree.JCIdent jcIdent )
{
  super.visitIdent( jcIdent );
  Names names = Names.instance( _javacTask.getContext() );
  jcIdent.name = names.fromString( jcIdent.name.toString() );
  jcIdent.pos = _offset;
}
 
Example 18
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 19
Source File: IterateAndRemove.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
void run() {
    Context ctx = new Context();
    Names names = Names.instance(ctx);
    Symbol root = new PackageSymbol(names.empty, null);
    Name one = names.fromString("1");
    PackageSymbol sym1 = new PackageSymbol(one, new PackageSymbol(names.fromString("a"), root));
    PackageSymbol sym2 = new PackageSymbol(one, new PackageSymbol(names.fromString("b"), root));
    PackageSymbol sym3 = new PackageSymbol(one, new PackageSymbol(names.fromString("c"), root));
    List<Symbol> symbols = Arrays.asList(sym1, sym2, sym3);

    List<Function<Scope, Iterable<Symbol>>> getters = Arrays.asList(
            scope -> scope.getSymbols(),
            scope -> scope.getSymbolsByName(one)
    );
    for (Function<Scope, Iterable<Symbol>> scope2Content : getters) {
        for (int removeAt : new int[] {0, 1, 2, 3}) {
            for (Symbol removeWhat : new Symbol[] {sym1, sym2, sym3}) {
                WriteableScope s = WriteableScope.create(root);

                symbols.forEach(s :: enter);

                Iterator<Symbol> it = scope2Content.apply(s).iterator();
                List<PackageSymbol> actual = new ArrayList<>();
                int count = 0;

                while (true) {
                    if (count++ == removeAt)
                        s.remove(removeWhat);
                    if (!it.hasNext())
                        break;
                    actual.add((PackageSymbol) it.next());
                }

                List<Symbol> copy = new ArrayList<>(symbols);

                Collections.reverse(copy);

                count = 0;

                while (true) {
                    if (count == removeAt && copy.indexOf(removeWhat) >= count)
                        copy.remove(removeWhat);
                    count++;
                    if (count >= copy.size())
                        break;
                }

                if (!copy.equals(actual)) {
                    throw new AssertionError("differs: actual: " + actual + "; expected: " + copy);
                }
            }
        }
    }
}
 
Example 20
Source File: SampleJavacPlugin.java    From tutorials with MIT License 4 votes vote down vote up
private static JCTree.JCBinary createIfCondition(TreeMaker factory, Names symbolsTable, VariableTree parameter) {
    Name parameterId = symbolsTable.fromString(parameter.getName().toString());
    return factory.Binary(JCTree.Tag.LE, 
      factory.Ident(parameterId), 
      factory.Literal(TypeTag.INT, 0));
}