예제 #1
0
 public Env<AttrContext> getClassEnv(TypeSymbol sym) {
   Env<AttrContext> localEnv = getEnv(sym);
   Env<AttrContext> lintEnv = localEnv;
   while (lintEnv.info.lint == null) lintEnv = lintEnv.next;
   localEnv.info.lint = lintEnv.info.lint.augment(sym.attributes_field, sym.flags());
   return localEnv;
 }
예제 #2
0
  void createSymbols() throws IOException {
    Set<String> legacy = getLegacyPackages();
    Set<String> legacyProprietary = getLegacyPackages();
    Set<String> documented = new HashSet<String>();
    Set<PackageSymbol> packages =
        ((JavacProcessingEnvironment) processingEnv).getSpecifiedPackages();
    String jarName = processingEnv.getOptions().get("com.sun.tools.javac.sym.Jar");
    if (jarName == null)
      throw new RuntimeException("Must use -Acom.sun.tools.javac.sym.Jar=LOCATION_OF_JAR");
    String destName = processingEnv.getOptions().get("com.sun.tools.javac.sym.Dest");
    if (destName == null)
      throw new RuntimeException("Must use -Acom.sun.tools.javac.sym.Dest=LOCATION_OF_JAR");

    for (PackageSymbol psym : packages) {
      String name = psym.getQualifiedName().toString();
      legacyProprietary.remove(name);
      documented.add(name);
    }

    JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    Location jarLocation = StandardLocation.locationFor(jarName);
    File jarFile = new File(jarName);
    fm.setLocation(jarLocation, List.of(jarFile));
    fm.setLocation(StandardLocation.CLASS_PATH, List.<File>nil());
    fm.setLocation(StandardLocation.SOURCE_PATH, List.<File>nil());
    {
      ArrayList<File> bootClassPath = new ArrayList<File>();
      bootClassPath.add(jarFile);
      for (File path : fm.getLocation(StandardLocation.PLATFORM_CLASS_PATH)) {
        if (!new File(path.getName()).equals(new File("rt.jar"))) bootClassPath.add(path);
      }
      System.err.println("Using boot class path = " + bootClassPath);
      fm.setLocation(StandardLocation.PLATFORM_CLASS_PATH, bootClassPath);
    }
    // System.out.println(fm.getLocation(StandardLocation.PLATFORM_CLASS_PATH));
    File destDir = new File(destName);
    if (!destDir.exists())
      if (!destDir.mkdirs()) throw new RuntimeException("Could not create " + destDir);
    fm.setLocation(StandardLocation.CLASS_OUTPUT, List.of(destDir));
    Set<String> hiddenPackages = new HashSet<String>();
    Set<String> crisp = new HashSet<String>();
    List<String> options = List.of("-XDdev");
    // options = options.prepend("-doe");
    // options = options.prepend("-verbose");
    JavacTaskImpl task = (JavacTaskImpl) tool.getTask(null, fm, null, options, null, null);
    com.sun.tools.javac.main.JavaCompiler compiler =
        com.sun.tools.javac.main.JavaCompiler.instance(task.getContext());
    ClassReader reader = ClassReader.instance(task.getContext());
    ClassWriter writer = ClassWriter.instance(task.getContext());
    Symtab syms = Symtab.instance(task.getContext());
    Attribute.Compound proprietary =
        new Attribute.Compound(
            syms.proprietaryType, List.<Pair<Symbol.MethodSymbol, Attribute>>nil());

    Type.moreInfo = true;
    Pool pool = new Pool();
    for (JavaFileObject file : fm.list(jarLocation, "", EnumSet.of(CLASS), true)) {
      String className = fm.inferBinaryName(jarLocation, file);
      int index = className.lastIndexOf('.');
      String pckName = index == -1 ? "" : className.substring(0, index);
      boolean addLegacyAnnotation = false;
      if (documented.contains(pckName)) {
        if (!legacy.contains(pckName)) crisp.add(pckName);
        // System.out.println("Documented: " + className);
      } else if (legacyProprietary.contains(pckName)) {
        addLegacyAnnotation = true;
        // System.out.println("Legacy proprietary: " + className);
      } else {
        // System.out.println("Hidden " + className);
        hiddenPackages.add(pckName);
        continue;
      }
      TypeSymbol sym = (TypeSymbol) compiler.resolveIdent(className);
      if (sym.kind != Kinds.TYP) {
        if (className.indexOf('$') < 0) {
          System.err.println("Ignoring (other) " + className + " : " + sym);
          System.err.println("   " + sym.getClass().getSimpleName() + " " + sym.type);
        }
        continue;
      }
      sym.complete();
      if (sym.getEnclosingElement().getKind() != ElementKind.PACKAGE) {
        System.err.println("Ignoring (bad) " + sym.getQualifiedName());
        continue;
      }
      ClassSymbol cs = (ClassSymbol) sym;
      if (addLegacyAnnotation) {
        cs.attributes_field =
            (cs.attributes_field == null)
                ? List.of(proprietary)
                : cs.attributes_field.prepend(proprietary);
      }
      writeClass(pool, cs, writer);
    }

    if (false) {
      for (String pckName : crisp) System.out.println("Crisp: " + pckName);
      for (String pckName : hiddenPackages) System.out.println("Hidden: " + pckName);
      for (String pckName : legacyProprietary) System.out.println("Legacy proprietary: " + pckName);
      for (String pckName : documented) System.out.println("Documented: " + pckName);
    }
  }
예제 #3
0
  /** Constructor; enters all predefined identifiers and operators into symbol table. */
  private Symtab(Context context) throws CompletionFailure {
    super();
    context.put(symtabKey, this);
    names = Name.Table.instance(context);
    byteType = new Type(TypeTags.BYTE, null);
    charType = new Type(TypeTags.CHAR, null);
    shortType = new Type(TypeTags.SHORT, null);
    intType = new Type(TypeTags.INT, null);
    longType = new Type(TypeTags.LONG, null);
    floatType = new Type(TypeTags.FLOAT, null);
    doubleType = new Type(TypeTags.DOUBLE, null);
    booleanType = new Type(TypeTags.BOOLEAN, null);
    voidType = new Type(TypeTags.VOID, null);
    botType = new Type(TypeTags.BOT, null);
    unknownType =
        new Type(TypeTags.UNKNOWN, null) {

          public boolean isSameType(Type that) {
            return true;
          }

          public boolean isSubType(Type that) {
            return false;
          }

          public boolean isSuperType(Type that) {
            return true;
          }
        };
    rootPackage = new PackageSymbol(names.empty, null);
    emptyPackage = new PackageSymbol(names.emptyPackage, rootPackage);
    noSymbol = new TypeSymbol(0, names.empty, Type.noType, rootPackage);
    noSymbol.kind = Kinds.NIL;
    errSymbol = new ClassSymbol(PUBLIC | STATIC, names.any, null, rootPackage);
    errType = new ErrorType(errSymbol);
    initType(byteType, "byte", "Byte");
    initType(shortType, "short", "Short");
    initType(charType, "char", "Character");
    initType(intType, "int", "Integer");
    initType(longType, "long", "Long");
    initType(floatType, "float", "Float");
    initType(doubleType, "double", "Double");
    initType(booleanType, "boolean", "Boolean");
    initType(voidType, "void", "Void");
    initType(botType, "<nulltype>");
    initType(errType, errSymbol);
    initType(unknownType, "<any?>");
    arrayClass = new ClassSymbol(PUBLIC, names.Array, noSymbol);
    methodClass = new ClassSymbol(PUBLIC, names.Method, noSymbol);
    predefClass = new ClassSymbol(PUBLIC, names.empty, rootPackage);
    Scope scope = new Scope(predefClass);
    predefClass.members_field = scope;
    scope.enter(byteType.tsym);
    scope.enter(shortType.tsym);
    scope.enter(charType.tsym);
    scope.enter(intType.tsym);
    scope.enter(longType.tsym);
    scope.enter(floatType.tsym);
    scope.enter(doubleType.tsym);
    scope.enter(booleanType.tsym);
    scope.enter(errType.tsym);
    classes.put(predefClass.fullname, predefClass);
    reader = ClassReader.instance(context);
    reader.init(this);
    objectType = enterClass("java.lang.Object");
    classType = enterClass("java.lang.Class");
    stringType = enterClass("java.lang.String");
    stringBufferType = enterClass("java.lang.StringBuffer");
    cloneableType = enterClass("java.lang.Cloneable");
    throwableType = enterClass("java.lang.Throwable");
    serializableType = enterClass("java.io.Serializable");
    errorType = enterClass("java.lang.Error");
    exceptionType = enterClass("java.lang.Exception");
    runtimeExceptionType = enterClass("java.lang.RuntimeException");
    classNotFoundExceptionType = enterClass("java.lang.ClassNotFoundException");
    noClassDefFoundErrorType = enterClass("java.lang.NoClassDefFoundError");
    assertionErrorType = enterClass("java.lang.AssertionError");
    classLoaderType = enterClass("java.lang.ClassLoader");
    ClassType arrayClassType = (ClassType) arrayClass.type;
    arrayClassType.supertype_field = objectType;
    arrayClassType.interfaces_field = List.make(cloneableType, serializableType);
    arrayClass.members_field = new Scope(arrayClass);
    lengthVar = new VarSymbol(PUBLIC | FINAL, names.length, intType, arrayClass);
    arrayClass.members().enter(lengthVar);
    Symbol cloneMethod =
        new MethodSymbol(
            PUBLIC,
            names.clone,
            new MethodType(Type.emptyList, objectType, Type.emptyList, methodClass),
            arrayClass);
    arrayClass.members().enter(cloneMethod);
    nullConst = enterConstant("null", botType);
    trueConst = enterConstant("true", booleanType.constType(new Integer(1)));
    falseConst = enterConstant("false", booleanType.constType(new Integer(0)));
    enterUnop("+", intType, intType, nop);
    enterUnop("+", longType, longType, nop);
    enterUnop("+", floatType, floatType, nop);
    enterUnop("+", doubleType, doubleType, nop);
    enterUnop("-", intType, intType, ineg);
    enterUnop("-", longType, longType, lneg);
    enterUnop("-", floatType, floatType, fneg);
    enterUnop("-", doubleType, doubleType, dneg);
    enterUnop("~", intType, intType, ixor);
    enterUnop("~", longType, longType, lxor);
    enterUnop("++", byteType, byteType, iadd);
    enterUnop("++", shortType, shortType, iadd);
    enterUnop("++", charType, charType, iadd);
    enterUnop("++", intType, intType, iadd);
    enterUnop("++", longType, longType, ladd);
    enterUnop("++", floatType, floatType, fadd);
    enterUnop("++", doubleType, doubleType, dadd);
    enterUnop("--", byteType, byteType, isub);
    enterUnop("--", shortType, shortType, isub);
    enterUnop("--", charType, charType, isub);
    enterUnop("--", intType, intType, isub);
    enterUnop("--", longType, longType, lsub);
    enterUnop("--", floatType, floatType, fsub);
    enterUnop("--", doubleType, doubleType, dsub);
    enterUnop("!", booleanType, booleanType, bool_not);
    nullcheck = enterUnop("<*nullchk*>", objectType, objectType, nullchk);
    enterBinop("+", stringType, stringType, stringType, string_add);
    enterBinop("+", stringType, intType, stringType, string_add);
    enterBinop("+", stringType, longType, stringType, string_add);
    enterBinop("+", stringType, floatType, stringType, string_add);
    enterBinop("+", stringType, doubleType, stringType, string_add);
    enterBinop("+", stringType, booleanType, stringType, string_add);
    enterBinop("+", stringType, objectType, stringType, string_add);
    enterBinop("+", stringType, botType, stringType, string_add);
    enterBinop("+", intType, stringType, stringType, string_add);
    enterBinop("+", longType, stringType, stringType, string_add);
    enterBinop("+", floatType, stringType, stringType, string_add);
    enterBinop("+", doubleType, stringType, stringType, string_add);
    enterBinop("+", booleanType, stringType, stringType, string_add);
    enterBinop("+", objectType, stringType, stringType, string_add);
    enterBinop("+", botType, stringType, stringType, string_add);
    enterBinop("+", intType, intType, intType, iadd);
    enterBinop("+", longType, longType, longType, ladd);
    enterBinop("+", floatType, floatType, floatType, fadd);
    enterBinop("+", doubleType, doubleType, doubleType, dadd);
    enterBinop("+", botType, botType, botType, error);
    enterBinop("+", botType, intType, botType, error);
    enterBinop("+", botType, longType, botType, error);
    enterBinop("+", botType, floatType, botType, error);
    enterBinop("+", botType, doubleType, botType, error);
    enterBinop("+", botType, booleanType, botType, error);
    enterBinop("+", botType, objectType, botType, error);
    enterBinop("+", intType, botType, botType, error);
    enterBinop("+", longType, botType, botType, error);
    enterBinop("+", floatType, botType, botType, error);
    enterBinop("+", doubleType, botType, botType, error);
    enterBinop("+", booleanType, botType, botType, error);
    enterBinop("+", objectType, botType, botType, error);
    enterBinop("-", intType, intType, intType, isub);
    enterBinop("-", longType, longType, longType, lsub);
    enterBinop("-", floatType, floatType, floatType, fsub);
    enterBinop("-", doubleType, doubleType, doubleType, dsub);
    enterBinop("*", intType, intType, intType, imul);
    enterBinop("*", longType, longType, longType, lmul);
    enterBinop("*", floatType, floatType, floatType, fmul);
    enterBinop("*", doubleType, doubleType, doubleType, dmul);
    enterBinop("/", intType, intType, intType, idiv);
    enterBinop("/", longType, longType, longType, ldiv);
    enterBinop("/", floatType, floatType, floatType, fdiv);
    enterBinop("/", doubleType, doubleType, doubleType, ddiv);
    enterBinop("%", intType, intType, intType, imod);
    enterBinop("%", longType, longType, longType, lmod);
    enterBinop("%", floatType, floatType, floatType, fmod);
    enterBinop("%", doubleType, doubleType, doubleType, dmod);
    enterBinop("&", booleanType, booleanType, booleanType, iand);
    enterBinop("&", intType, intType, intType, iand);
    enterBinop("&", longType, longType, longType, land);
    enterBinop("|", booleanType, booleanType, booleanType, ior);
    enterBinop("|", intType, intType, intType, ior);
    enterBinop("|", longType, longType, longType, lor);
    enterBinop("^", booleanType, booleanType, booleanType, ixor);
    enterBinop("^", intType, intType, intType, ixor);
    enterBinop("^", longType, longType, longType, lxor);
    enterBinop("<<", intType, intType, intType, ishl);
    enterBinop("<<", longType, intType, longType, lshl);
    enterBinop("<<", intType, longType, intType, ishll);
    enterBinop("<<", longType, longType, longType, lshll);
    enterBinop(">>", intType, intType, intType, ishr);
    enterBinop(">>", longType, intType, longType, lshr);
    enterBinop(">>", intType, longType, intType, ishrl);
    enterBinop(">>", longType, longType, longType, lshrl);
    enterBinop(">>>", intType, intType, intType, iushr);
    enterBinop(">>>", longType, intType, longType, lushr);
    enterBinop(">>>", intType, longType, intType, iushrl);
    enterBinop(">>>", longType, longType, longType, lushrl);
    enterBinop("<", intType, intType, booleanType, if_icmplt);
    enterBinop("<", longType, longType, booleanType, lcmp, iflt);
    enterBinop("<", floatType, floatType, booleanType, fcmpg, iflt);
    enterBinop("<", doubleType, doubleType, booleanType, dcmpg, iflt);
    enterBinop(">", intType, intType, booleanType, if_icmpgt);
    enterBinop(">", longType, longType, booleanType, lcmp, ifgt);
    enterBinop(">", floatType, floatType, booleanType, fcmpl, ifgt);
    enterBinop(">", doubleType, doubleType, booleanType, dcmpl, ifgt);
    enterBinop("<=", intType, intType, booleanType, if_icmple);
    enterBinop("<=", longType, longType, booleanType, lcmp, ifle);
    enterBinop("<=", floatType, floatType, booleanType, fcmpg, ifle);
    enterBinop("<=", doubleType, doubleType, booleanType, dcmpg, ifle);
    enterBinop(">=", intType, intType, booleanType, if_icmpge);
    enterBinop(">=", longType, longType, booleanType, lcmp, ifge);
    enterBinop(">=", floatType, floatType, booleanType, fcmpl, ifge);
    enterBinop(">=", doubleType, doubleType, booleanType, dcmpl, ifge);
    enterBinop("==", intType, intType, booleanType, if_icmpeq);
    enterBinop("==", longType, longType, booleanType, lcmp, ifeq);
    enterBinop("==", floatType, floatType, booleanType, fcmpl, ifeq);
    enterBinop("==", doubleType, doubleType, booleanType, dcmpl, ifeq);
    enterBinop("==", booleanType, booleanType, booleanType, if_icmpeq);
    enterBinop("==", objectType, objectType, booleanType, if_acmpeq);
    enterBinop("!=", intType, intType, booleanType, if_icmpne);
    enterBinop("!=", longType, longType, booleanType, lcmp, ifne);
    enterBinop("!=", floatType, floatType, booleanType, fcmpl, ifne);
    enterBinop("!=", doubleType, doubleType, booleanType, dcmpl, ifne);
    enterBinop("!=", booleanType, booleanType, booleanType, if_icmpne);
    enterBinop("!=", objectType, objectType, booleanType, if_acmpne);
    enterBinop("&&", booleanType, booleanType, booleanType, bool_and);
    enterBinop("||", booleanType, booleanType, booleanType, bool_or);
  }