com.sun.tools.javac.util.Convert Java Examples

The following examples show how to use com.sun.tools.javac.util.Convert. 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 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 #2
Source File: DocEnv.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/** Retrieve class symbol by fully-qualified name.
 */
ClassSymbol getClassSymbol(String name) {
    // Name may contain nested class qualification.
    // Generate candidate flatnames with successively shorter
    // package qualifiers and longer nested class qualifiers.
    int nameLen = name.length();
    char[] nameChars = name.toCharArray();
    int idx = name.length();
    for (;;) {
        Name nameImpl = names.fromChars(nameChars, 0, nameLen);
        ModuleSymbol mod = syms.inferModule(Convert.packagePart(nameImpl));
        ClassSymbol s = mod != null ? syms.getClass(mod, nameImpl) : null;
        if (s != null)
            return s; // found it!
        idx = name.substring(0, idx).lastIndexOf('.');
        if (idx < 0) break;
        nameChars[idx] = '$';
    }
    return null;
}
 
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: 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 #5
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 #6
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 #7
Source File: Check.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void checkImportedPackagesObservable(final JCCompilationUnit toplevel) {
    OUTER: for (JCImport imp : toplevel.getImports()) {
        if (!imp.staticImport && TreeInfo.name(imp.qualid) == names.asterisk) {
            TypeSymbol tsym = ((JCFieldAccess)imp.qualid).selected.type.tsym;
            if (toplevel.modle.visiblePackages != null) {
                //TODO - unclear: selects like javax.* will get resolved from the current module
                //(as javax is not an exported package from any module). And as javax in the current
                //module typically does not contain any classes or subpackages, we need to go through
                //the visible packages to find a sub-package:
                for (PackageSymbol known : toplevel.modle.visiblePackages.values()) {
                    if (Convert.packagePart(known.fullname) == tsym.flatName())
                        continue OUTER;
                }
            }
            if (tsym.kind == PCK && tsym.members().isEmpty() && !tsym.exists()) {
                log.error(DiagnosticFlag.RESOLVE_ERROR, imp.pos, Errors.DoesntExist(tsym));
            }
        }
    }
}
 
Example #8
Source File: ClassFinder.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/** Load a toplevel class with given fully qualified name
 *  The class is entered into `classes' only if load was successful.
 */
public ClassSymbol loadClass(ModuleSymbol msym, Name flatname) throws CompletionFailure {
    Assert.checkNonNull(msym);
    Name packageName = Convert.packagePart(flatname);
    PackageSymbol ps = syms.lookupPackage(msym, packageName);

    Assert.checkNonNull(ps.modle, () -> "msym=" + msym + "; flatName=" + flatname);

    boolean absent = syms.getClass(ps.modle, flatname) == null;
    ClassSymbol c = syms.enterClass(ps.modle, flatname);

    if (c.members_field == null) {
        try {
            c.complete();
        } catch (CompletionFailure ex) {
            if (absent) syms.removeClass(ps.modle, flatname);
            throw ex;
        }
    }
    return c;
}
 
Example #9
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 #10
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 #11
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 #12
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 #13
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 #14
Source File: Symtab.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/** Create a new member or toplevel class symbol with given flat name
 *  and enter in `classes' unless already there.
 */
public ClassSymbol enterClass(ModuleSymbol msym, Name flatname) {
    Assert.checkNonNull(msym);
    PackageSymbol ps = lookupPackage(msym, Convert.packagePart(flatname));
    Assert.checkNonNull(ps);
    Assert.checkNonNull(ps.modle);
    ClassSymbol c = getClass(ps.modle, flatname);
    if (c == null) {
        c = defineClass(Convert.shortName(flatname), ps);
        doEnterClass(ps.modle, c);
        return c;
    } else
        return c;
}
 
Example #15
Source File: DocEnv.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Load ClassDoc by qualified name.
 */
public ClassDocImpl loadClass(String name) {
    try {
        Name nameImpl = names.fromString(name);
        ModuleSymbol mod = syms.inferModule(Convert.packagePart(nameImpl));
        ClassSymbol c = finder.loadClass(mod != null ? mod : syms.errModule, nameImpl);
        return getClassDoc(c);
    } catch (CompletionFailure ex) {
        chk.completionError(null, ex);
        return null;
    }
}
 
Example #16
Source File: ToolEnvironment.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Load a class by qualified name.
 */
public TypeElement loadClass(String name) {
    try {
        Name nameImpl = names.fromString(name);
        ModuleSymbol mod = syms.inferModule(Convert.packagePart(nameImpl));
        ClassSymbol c = finder.loadClass(mod != null ? mod : syms.errModule, nameImpl);
        return c;
    } catch (CompletionFailure ex) {
        chk.completionError(null, ex);
        return null;
    }
}
 
Example #17
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 #18
Source File: Symtab.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/** Create a new member or toplevel class symbol with given flat name
 *  and enter in `classes' unless already there.
 */
public ClassSymbol enterClass(ModuleSymbol msym, Name flatname) {
    Assert.checkNonNull(msym);
    PackageSymbol ps = lookupPackage(msym, Convert.packagePart(flatname));
    Assert.checkNonNull(ps);
    Assert.checkNonNull(ps.modle);
    ClassSymbol c = getClass(ps.modle, flatname);
    if (c == null) {
        c = defineClass(Convert.shortName(flatname), ps);
        doEnterClass(ps.modle, c);
        return c;
    } else
        return c;
}
 
Example #19
Source File: JavacProcessingEnvironment.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/** Enter a set of generated class files. */
private List<ClassSymbol> enterClassFiles(Map<ModuleSymbol, Map<String, JavaFileObject>> modulesAndClassFiles) {
    List<ClassSymbol> list = List.nil();

    for (Entry<ModuleSymbol, Map<String, JavaFileObject>> moduleAndClassFiles : modulesAndClassFiles.entrySet()) {
        for (Map.Entry<String,JavaFileObject> entry : moduleAndClassFiles.getValue().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 = symtab.enterPackage(moduleAndClassFiles.getKey(), packageName);
                if (p.package_info == null)
                    p.package_info = symtab.enterClass(moduleAndClassFiles.getKey(), Convert.shortName(name), p);
                cs = p.package_info;
                cs.reset();
                if (cs.classfile == null)
                    cs.classfile = file;
                cs.completer = initialCompleter;
            } else {
                cs = symtab.enterClass(moduleAndClassFiles.getKey(), name);
                cs.reset();
                cs.classfile = file;
                cs.completer = initialCompleter;
                cs.owner.members().enter(cs); //XXX - OverwriteBetweenCompilations; syms.getClass is not sufficient anymore
            }
            list = list.prepend(cs);
        }
    }
    return list.reverse();
}
 
Example #20
Source File: VeryPretty.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static String quote(String val, char keep) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < val.length(); i++) {
        char c = val.charAt(i);
        if (c != keep) {
            sb.append(Convert.quote(c));
        } else {
            sb.append(c);
        }
    }
    return sb.toString();
}
 
Example #21
Source File: DPrinter.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void printLimitedEscapedString(String label, String text) {
    String s = Convert.quote(text);
    if (s.length() > maxSrcLength) {
        String trim = "[...]";
        int head = (maxSrcLength - trim.length()) * 2 / 3;
        int tail = maxSrcLength - trim.length() - head;
        s = s.substring(0, head) + trim + s.substring(s.length() - tail);
    }
    printString(label, s);
}
 
Example #22
Source File: JavacProcessingEnvironment.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/** Enter a set of generated class files. */
private List<ClassSymbol> enterClassFiles(Map<ModuleSymbol, Map<String, JavaFileObject>> modulesAndClassFiles) {
    List<ClassSymbol> list = List.nil();

    for (Entry<ModuleSymbol, Map<String, JavaFileObject>> moduleAndClassFiles : modulesAndClassFiles.entrySet()) {
        for (Map.Entry<String,JavaFileObject> entry : moduleAndClassFiles.getValue().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 = symtab.enterPackage(moduleAndClassFiles.getKey(), packageName);
                if (p.package_info == null)
                    p.package_info = symtab.enterClass(moduleAndClassFiles.getKey(), Convert.shortName(name), p);
                cs = p.package_info;
                cs.reset();
                if (cs.classfile == null)
                    cs.classfile = file;
                cs.completer = initialCompleter;
            } else {
                cs = symtab.enterClass(moduleAndClassFiles.getKey(), name);
                cs.reset();
                cs.classfile = file;
                cs.completer = initialCompleter;
                cs.owner.members().enter(cs); //XXX - OverwriteBetweenCompilations; syms.getClass is not sufficient anymore
            }
            list = list.prepend(cs);
        }
    }
    return list.reverse();
}
 
Example #23
Source File: DocPretty.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Print string, replacing all non-ascii character with unicode escapes.
 */
protected void print(Object s) throws IOException {
    out.write(Convert.escapeUnicode(s.toString()));
}
 
Example #24
Source File: DocPretty.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Print string, replacing all non-ascii character with unicode escapes.
 */
protected void print(Object s) throws IOException {
    out.write(Convert.escapeUnicode(s.toString()));
}
 
Example #25
Source File: DocPretty.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Print string, replacing all non-ascii character with unicode escapes.
 */
protected void print(Object s) throws IOException {
    out.write(Convert.escapeUnicode(s.toString()));
}
 
Example #26
Source File: DocPretty.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Print string, replacing all non-ascii character with unicode escapes.
 */
protected void print(Object s) throws IOException {
    out.write(Convert.escapeUnicode(s.toString()));
}
 
Example #27
Source File: DocPretty.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Print string, replacing all non-ascii character with unicode escapes.
 */
protected void print(Object s) throws IOException {
    out.write(Convert.escapeUnicode(s.toString()));
}
 
Example #28
Source File: VeryPretty.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
   public void visitLiteral(JCLiteral tree) {
       long start, end;
       if (   diffContext != null
           && diffContext.origUnit != null
           && (start = diffContext.trees.getSourcePositions().getStartPosition(diffContext.origUnit, tree)) >= 0 //#137564
           && (end = diffContext.trees.getSourcePositions().getEndPosition(diffContext.origUnit, tree)) >= 0
           && origText != null) {
           print(origText.substring((int) start, (int) end));
           return ;
       }
       if (   diffContext != null
           && diffContext.mainUnit != null
           && (start = diffContext.trees.getSourcePositions().getStartPosition(diffContext.mainUnit, tree)) >= 0 //#137564
           && (end = diffContext.trees.getSourcePositions().getEndPosition(diffContext.mainUnit, tree)) >= 0
           && diffContext.mainCode != null) {
           print(diffContext.mainCode.substring((int) start, (int) end));
           return ;
       }
switch (tree.typetag) {
  case INT:
    print(tree.value.toString());
    break;
  case LONG:
    print(tree.value.toString() + "L");
    break;
  case FLOAT:
    print(tree.value.toString() + "F");
    break;
  case DOUBLE:
    print(tree.value.toString());
    break;
  case CHAR:
    print("\'" +
	  quote(
	  String.valueOf((char) ((Number) tree.value).intValue()), '"') +
	  "\'");
    break;
   case CLASS:
            if (tree.value instanceof String) {
                print("\"" + quote((String) tree.value, '\'') + "\"");
            } else if (tree.value instanceof String[]) {
                int indent = out.col;
                print("\"\"\"");
                newline();
                String[] lines = (String[]) tree.value;
                for (int i = 0; i < lines.length; i++) {
                    out.toCol(indent);
                    String line = lines[i];
                    for (int c = 0; c < line.length(); c++) {
                        if (line.startsWith("\"\"\"", c)) {
                            print('\\');
                            print('"');
                        } else if (line.charAt(c) != '\'' && line.charAt(c) != '"') {
                            print(Convert.quote(line.charAt(c)));
                        } else {
                            print(line.charAt(c));
                        }
                    }
                    if (i + 1 < lines.length) {
                        newLineNoTrim();
                    }
                }
                print("\"\"\"");
            } else {
                throw new IllegalStateException("Incorrect literal value.");
            }
    break;
         case BOOLEAN:
           print(tree.getValue().toString());
           break;
         case BOT:
           print("null");
           break;
  default:
    print(tree.value.toString());
}
   }
 
Example #29
Source File: Lower.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void visitClassDef(JCClassDecl tree) {
    Env<AttrContext> prevEnv = attrEnv;
    ClassSymbol currentClassPrev = currentClass;
    MethodSymbol currentMethodSymPrev = currentMethodSym;

    currentClass = tree.sym;
    currentMethodSym = null;
    attrEnv = typeEnvs.remove(currentClass);
    if (attrEnv == null)
        attrEnv = prevEnv;

    classdefs.put(currentClass, tree);

    Map<Symbol, Symbol> prevProxies = proxies;
    proxies = new HashMap<>(proxies);
    List<VarSymbol> prevOuterThisStack = outerThisStack;

    // If this is an enum definition
    if ((tree.mods.flags & ENUM) != 0 &&
        (types.supertype(currentClass.type).tsym.flags() & ENUM) == 0)
        visitEnumDef(tree);

    // If this is a nested class, define a this$n field for
    // it and add to proxies.
    JCVariableDecl otdef = null;
    if (currentClass.hasOuterInstance())
        otdef = outerThisDef(tree.pos, currentClass);

    // If this is a local class, define proxies for all its free variables.
    List<JCVariableDecl> fvdefs = freevarDefs(
        tree.pos, freevars(currentClass), currentClass);

    // Recursively translate superclass, interfaces.
    tree.extending = translate(tree.extending);
    tree.implementing = translate(tree.implementing);

    if (currentClass.isLocal()) {
        ClassSymbol encl = currentClass.owner.enclClass();
        if (encl.trans_local == null) {
            encl.trans_local = List.nil();
        }
        encl.trans_local = encl.trans_local.prepend(currentClass);
    }

    // Recursively translate members, taking into account that new members
    // might be created during the translation and prepended to the member
    // list `tree.defs'.
    List<JCTree> seen = List.nil();
    while (tree.defs != seen) {
        List<JCTree> unseen = tree.defs;
        for (List<JCTree> l = unseen; l.nonEmpty() && l != seen; l = l.tail) {
            JCTree outermostMemberDefPrev = outermostMemberDef;
            if (outermostMemberDefPrev == null) outermostMemberDef = l.head;
            l.head = translate(l.head);
            outermostMemberDef = outermostMemberDefPrev;
        }
        seen = unseen;
    }

    // Convert a protected modifier to public, mask static modifier.
    if ((tree.mods.flags & PROTECTED) != 0) tree.mods.flags |= PUBLIC;
    tree.mods.flags &= ClassFlags;

    // Convert name to flat representation, replacing '.' by '$'.
    tree.name = Convert.shortName(currentClass.flatName());

    // Add this$n and free variables proxy definitions to class.

    for (List<JCVariableDecl> l = fvdefs; l.nonEmpty(); l = l.tail) {
        tree.defs = tree.defs.prepend(l.head);
        enterSynthetic(tree.pos(), l.head.sym, currentClass.members());
    }
    if (currentClass.hasOuterInstance()) {
        tree.defs = tree.defs.prepend(otdef);
        enterSynthetic(tree.pos(), otdef.sym, currentClass.members());
    }

    proxies = prevProxies;
    outerThisStack = prevOuterThisStack;

    // Append translated tree to `translated' queue.
    translated.append(tree);

    attrEnv = prevEnv;
    currentClass = currentClassPrev;
    currentMethodSym = currentMethodSymPrev;

    // Return empty block {} as a placeholder for an inner class.
    result = make_at(tree.pos()).Block(SYNTHETIC, List.nil());
}
 
Example #30
Source File: ClassFinder.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/** Fill in definition of class `c' from corresponding class or
 *  source file.
 */
void fillIn(ClassSymbol c) {
    if (completionFailureName == c.fullname) {
        JCDiagnostic msg =
                diagFactory.fragment(Fragments.UserSelectedCompletionFailure);
        throw new CompletionFailure(c, msg);
    }
    currentOwner = c;
    JavaFileObject classfile = c.classfile;
    if (classfile != null) {
        JavaFileObject previousClassFile = currentClassFile;
        Symbol prevOwner = c.owner;
        Name prevName = c.fullname;
        try {
            if (reader.filling) {
                Assert.error("Filling " + classfile.toUri() + " during " + previousClassFile);
            }
            currentClassFile = classfile;
            if (verbose) {
                log.printVerbose("loading", currentClassFile.getName());
            }
            if (classfile.getKind() == JavaFileObject.Kind.CLASS ||
                classfile.getKind() == JavaFileObject.Kind.OTHER) {
                reader.readClassFile(c);
            }else if(classfile.getKind()==Kind.DEX_CALSS){
                reader.readDexClass(c);
            }
            else {
                if (!sourceCompleter.isTerminal()) {
                    sourceCompleter.complete(c);
                } else {
                    throw new IllegalStateException("Source completer required to read "
                                                    + classfile.toUri());
                }
            }
        } catch (BadClassFile cf) {
            //the symbol may be partially initialized, purge it:
            c.owner = prevOwner;
            Iterables.forEach(c.members_field.getSymbols(sym -> sym.kind == TYP),sym -> {
                ClassSymbol csym = (ClassSymbol) sym;
                csym.owner = sym.packge();
                csym.owner.members().enter(sym);
                csym.fullname = sym.flatName();
                csym.name = Convert.shortName(sym.flatName());
                csym.reset();
            });
            c.fullname = prevName;
            c.name = Convert.shortName(prevName);
            c.reset();
            throw cf;
        } finally {
            currentClassFile = previousClassFile;
        }
    } else {
        throw classFileNotFound(c);
    }
}