@Override
 public String visitCapturedType(CapturedType t, Locale locale) {
   if (!allCaptured.contains(t)) {
     allCaptured = allCaptured.append(t);
   }
   return super.visitCapturedType(t, locale);
 }
 /** Add a name usage to the simplifier's internal cache */
 protected void addUsage(Symbol sym) {
   Name n = sym.getSimpleName();
   List<Symbol> conflicts = nameClashes.get(n);
   if (conflicts == null) {
     conflicts = List.nil();
   }
   if (!conflicts.contains(sym)) nameClashes.put(n, conflicts.append(sym));
 }
 @Override
 public List<JCCompilationUnit> parseFiles(List<JavaFileObject> fileObjects) throws IOException {
   List<JCCompilationUnit> trees = super.parseFiles(fileObjects);
   LinkedList<JCCompilationUnit> moduleTrees = new LinkedList<JCCompilationUnit>();
   loadCompiledModules(trees, moduleTrees);
   for (JCCompilationUnit moduleTree : moduleTrees) {
     trees = trees.append(moduleTree);
   }
   return trees;
 }
Exemple #4
0
 private com.sun.tools.javac.util.List<Entry> getEntries() {
   if (!zipFileEntriesInited) {
     initEntries();
     zipFileEntries = com.sun.tools.javac.util.List.nil();
     for (Entry zfie : entries) {
       zipFileEntries = zipFileEntries.append(zfie);
     }
     zipFileEntriesInited = true;
   }
   return zipFileEntries;
 }
Exemple #5
0
 private com.sun.tools.javac.util.List<String> getDirectories() {
   if (!directoriesInited) {
     initEntries();
     for (Entry e : entries) {
       if (e.isDir) {
         zipFileEntriesDirectories = zipFileEntriesDirectories.append(e.name);
       }
     }
     directoriesInited = true;
   }
   return zipFileEntriesDirectories;
 }
 @Override
 public void compile(
     List<JavaFileObject> fileObjects,
     List<String> classnames,
     Iterable<? extends Processor> processors) {
   // Now we first split the files into sources/modules and resources
   List<JavaFileObject> sourceFiles = List.nil();
   List<JavaFileObject> resourceFiles = List.nil();
   for (JavaFileObject fo : fileObjects) {
     if (isResource(fo)) {
       resourceFiles = resourceFiles.append(fo);
     } else {
       sourceFiles = sourceFiles.append(fo);
     }
   }
   this.resourceFileObjects = resourceFiles;
   // Add any module files for the resources (if needed)
   sourceFiles = addModuleDescriptors(sourceFiles, resourceFiles);
   // And then continue to the compilation of the source files
   super.compile(sourceFiles, classnames, processors);
 }
Exemple #7
0
 public Annotations appendUniqueTypes(List<Attribute.TypeCompound> l) {
   if (l.isEmpty()) {; // no-op
   } else if (type_attributes.isEmpty()) {
     type_attributes = l;
   } else {
     // TODO: in case we expect a large number of annotations, this
     // might be inefficient.
     for (Attribute.TypeCompound tc : l) {
       if (!type_attributes.contains(tc)) type_attributes = type_attributes.append(tc);
     }
   }
   return this;
 }
 // This is a bit of a hack, but if we got passed a list of resources
 // without any accompaning source files we'll not be able to determine
 // the module to which the resource files belong. So to try to fix that
 // we see if a module file exists in the source folders and add it to
 // the list of source files
 private List<JavaFileObject> addModuleDescriptors(
     List<JavaFileObject> sourceFiles, List<JavaFileObject> resourceFiles) {
   List<JavaFileObject> result = sourceFiles;
   JavacFileManager dfm = (JavacFileManager) fileManager;
   for (JavaFileObject fo : resourceFiles) {
     String resName =
         JarUtils.toPlatformIndependentPath(
             dfm.getLocation(CeylonLocation.RESOURCE_PATH), fo.getName());
     JavaFileObject moduleFile = findModuleDescriptorForFile(new File(resName));
     if (moduleFile != null && !result.contains(moduleFile)) {
       result = result.append(moduleFile);
     }
   }
   return result;
 }
 /**
  * Format all the subdiagnostics attached to a given diagnostic.
  *
  * @param d diagnostic whose subdiagnostics are to be formatted
  * @param l locale object to be used for i18n
  * @return list of all string representations of the subdiagnostics
  */
 protected List<String> formatSubdiagnostics(JCDiagnostic d, Locale l) {
   List<String> subdiagnostics = List.nil();
   int maxDepth = config.getMultilineLimit(MultilineLimit.DEPTH);
   if (maxDepth == -1 || depth < maxDepth) {
     depth++;
     try {
       int maxCount = config.getMultilineLimit(MultilineLimit.LENGTH);
       int count = 0;
       for (JCDiagnostic d2 : d.getSubdiagnostics()) {
         if (maxCount == -1 || count < maxCount) {
           subdiagnostics = subdiagnostics.append(formatSubdiagnostic(d, d2, l));
           count++;
         } else break;
       }
     } finally {
       depth--;
     }
   }
   return subdiagnostics;
 }
 @Override
 public List<JCCompilationUnit> parseFiles(Iterable<JavaFileObject> fileObjects) {
   timer.startTask("parse");
   /*
    * Stef: see javadoc for fixDefaultPackage() for why this is here.
    */
   modelLoader.fixDefaultPackage();
   List<JCCompilationUnit> trees = super.parseFiles(fileObjects);
   timer.startTask("loadCompiledModules");
   LinkedList<JCCompilationUnit> moduleTrees = new LinkedList<JCCompilationUnit>();
   // now load modules and associate their moduleless packages with the corresponding modules
   loadCompiledModules(trees, moduleTrees);
   for (JCCompilationUnit moduleTree : moduleTrees) {
     trees = trees.append(moduleTree);
   }
   /*
    * Stef: see javadoc for cacheModulelessPackages() for why this is here.
    */
   modelLoader.cacheModulelessPackages();
   timer.endTask();
   return trees;
 }
Exemple #11
0
  private static void openAnnotatedJava() {
    // Create list with source code names
    @SuppressWarnings("unchecked")
    Vector<String> myfilelist = new Vector<String>(cmd.getArgList());

    // Table that stores parsed compilation units
    Hashtable<String, CompilationUnit> myjpunitlist = new Hashtable<String, CompilationUnit>();

    // Declare outside, so filename can be used in case of exception.

    // Generate AST with JavaParser
    for (Enumeration<String> e = myfilelist.elements(); e.hasMoreElements(); ) {
      String mysource = "";
      CompilationUnit mycunit = null;

      try {
        // read filename
        mysource = e.nextElement();

        // Compile it with java parser.
        mycunit = JavaParser.parse(new FileInputStream(mysource));

        // Then fix the AST following the JC requirements
        ComplyToJCVisitor myfixervisitor = new ComplyToJCVisitor();
        mycunit = (CompilationUnit) myfixervisitor.visit(mycunit, new Integer(0));

        // creates an input stream and parse it using Java Parser
        myjpunitlist.put(mysource, mycunit);

        if (cmd.hasOption("d")) {
          // ASTDumpVisitor myjpvisitor = new ASTDumpVisitor();
          DumpVisitor myjpvisitor = new DumpVisitor();
          myjpvisitor.visit(myjpunitlist.get(mysource), null);
          System.out.print(myjpvisitor.getSource());
        }
      } catch (com.github.javaparser.ParseException ex) {
        System.out.println("Error: Parsing of Java file failed: " + mysource + ". Exiting...");
        System.exit(1);
      } catch (FileNotFoundException ex) {
        System.out.println("Error: File not found: " + mysource + ". Exiting...");
        System.exit(1);
      }
    }

    // Building internals from Java Compiler
    Context mycontext = new Context();
    JavaCompiler myjcompiler = new JavaCompiler(mycontext);
    JavaFileManager myfilemanager = mycontext.get(JavaFileManager.class);
    // Phase that Javac may go to: Setting code generation
    myjcompiler.shouldStopPolicyIfNoError = CompileState.GENERATE;

    // Table that stores the Java Compiler's ASTs
    List<JCCompilationUnit> ljctreelist = List.<JCCompilationUnit>nil();

    // Convert to Java Parser AST to JCTree AST's
    for (Enumeration<String> e = myjpunitlist.keys(); e.hasMoreElements(); ) {

      // read filename
      String mysource = e.nextElement();

      CompilationUnit myjpunit = myjpunitlist.get(mysource);

      JavaParser2JCTree translator = new JavaParser2JCTree(mycontext);
      AJCCompilationUnit myjctreeunit = (AJCCompilationUnit) translator.visit(myjpunit, myjpunit);

      // Setting additional information for Javac:
      // - Source file. Otherwise it throws a NullPointerException
      myjctreeunit.sourcefile = ((JavacFileManager) myfilemanager).getFileForInput(mysource);

      // Storing in the list
      ljctreelist = ljctreelist.append(myjctreeunit);

      // Debug: Shows how the JCTree AST was generated. Output node types.
      if (cmd.hasOption("d")) {
        try {
          Writer mystdout = new OutputStreamWriter(System.out);
          (new PrintAstVisitor(mystdout, true)).visitTopLevel(myjctreeunit);
          mystdout.flush();
        } catch (Exception z) {
        }
      }
    }

    // Enter (phase I): starting to build symtable
    Enter myenter = Enter.instance(mycontext);
    myenter.main(ljctreelist);
    // Enter (phase II): Finishing to build symtable
    /* MemberEnter mymemberenter = MemberEnter.instance(mycontext);
    mymemberenter.visitTopLevel(myjctreeunit); */

    // Get the todo list generated by Enter phase
    // From now on, the output of a phase is the input of the other.
    Todo mytodo = Todo.instance(mycontext);

    // atrribute: type-checking, name resolution, constant folding
    // flow: deadcode elimination
    // desugar: removes synctactic sugar: inner classes, class literals, assertions, foreachs
    myjcompiler.desugar(myjcompiler.flow(myjcompiler.attribute(mytodo)));

    // generate: produce bytecode or source code. Erases the whole AST
    // myjcompiler.generate(myjcompiler.desugar(myjcompiler.flow(myjcompiler.attribute( mytodo))));

    // Prints the Java program to output files and leave
    for (ListIterator<JCCompilationUnit> i = ljctreelist.listIterator(); i.hasNext(); ) {
      JCCompilationUnit myjctreeunit = i.next();
      try {
        Writer myoutputfile;

        if (cmd.getOptionValue("o") == null) {
          myoutputfile = new FileWriter(FileDescriptor.out);
        } else {
          myoutputfile = new FileWriter(myjctreeunit.sourcefile + ".new");
        }

        (new APretty(myoutputfile, true)).visitTopLevel(myjctreeunit);
        myoutputfile.flush();

      } catch (Exception e) {
        // TODO - Check what to report in case of error.
      }
    }
  }
 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
     errDiags = errDiags.append(diagnostic.getMessage(Locale.getDefault()));
     errorFound = true;
   }
 }