コード例 #1
0
        public void call(SourceUnit source) throws CompilationFailedException {
          List<ClassNode> classes = source.ast.getClasses();
          for (ClassNode node : classes) {
            CompileUnit cu = node.getCompileUnit();
            for (Iterator iter = cu.iterateClassNodeToCompile(); iter.hasNext(); ) {
              String name = (String) iter.next();
              SourceUnit su = ast.getScriptSourceLocation(name);
              List<ClassNode> classesInSourceUnit = su.ast.getClasses();
              StringBuilder message = new StringBuilder();
              message
                  .append("Compilation incomplete: expected to find the class ")
                  .append(name)
                  .append(" in ")
                  .append(su.getName());
              if (classesInSourceUnit.isEmpty()) {
                message.append(", but the file seems not to contain any classes");
              } else {
                message.append(", but the file contains the classes: ");
                boolean first = true;
                for (ClassNode cn : classesInSourceUnit) {
                  if (!first) {
                    message.append(", ");
                  } else {
                    first = false;
                  }
                  message.append(cn.getName());
                }
              }

              getErrorCollector()
                  .addErrorAndContinue(new SimpleMessage(message.toString(), CompilationUnit.this));
              iter.remove();
            }
          }
        }
コード例 #2
0
        public void call(SourceUnit source) throws CompilationFailedException {
          source.convert();
          CompilationUnit.this.ast.addModule(source.getAST());

          if (CompilationUnit.this.progressCallback != null) {
            CompilationUnit.this.progressCallback.call(source, CompilationUnit.this.phase);
          }
        }
コード例 #3
0
        public void call(SourceUnit source) throws CompilationFailedException {
          if (source.phase < phase) {
            source.gotoPhase(phase);
          }

          if (source.phase == phase && phaseComplete && !source.phaseComplete) {
            source.completePhase();
          }
        }
コード例 #4
0
 private void changeBugText(GroovyBugError e, SourceUnit context) {
   e.setBugText(
       "exception in phase '"
           + getPhaseDescription()
           + "' in source unit '"
           + ((context != null) ? context.getName() : "?")
           + "' "
           + e.getBugText());
 }
コード例 #5
0
 /** Adds a SourceUnit to the unit. */
 public SourceUnit addSource(SourceUnit source) {
   String name = source.getName();
   source.setClassLoader(this.classLoader);
   for (SourceUnit su : queuedSources) {
     if (name.equals(su.getName())) return su;
   }
   // GRECLIPSE: start
   if (iterating) {
     GroovyBugError gbe =
         new GroovyBugError(
             "Queuing new source whilst already iterating.  Queued source is '"
                 + source.getName()
                 + "'");
     gbe.printStackTrace();
     throw gbe;
   }
   // end
   queuedSources.add(source);
   return source;
 }
コード例 #6
0
 /**
  * Dequeues any source units add through addSource and resets the compiler phase to
  * initialization.
  *
  * <p>Note: this does not mean a file is recompiled. If a SourceUnit has already passed a phase it
  * is skipped until a higher phase is reached.
  *
  * @return true if there was a queued source
  * @throws CompilationFailedException
  */
 protected boolean dequeued() throws CompilationFailedException {
   boolean dequeue = !queuedSources.isEmpty();
   while (!queuedSources.isEmpty()) {
     SourceUnit su = queuedSources.removeFirst();
     String name = su.getName();
     // GRECLIPSE: start
     if (iterating) {
       GroovyBugError gbe =
           new GroovyBugError(
               "Damaging 'names' whilst already iterating.  Name getting added is '"
                   + su.getName()
                   + "'");
       gbe.printStackTrace();
       throw gbe;
     }
     // end
     names.add(name);
     sources.put(name, su);
   }
   if (dequeue) {
     gotoPhase(Phases.INITIALIZATION);
   }
   return dequeue;
 }
コード例 #7
0
        public void call(SourceUnit source, GeneratorContext context, ClassNode classNode)
            throws CompilationFailedException {

          optimizer.visitClass(
              classNode, source); // GROOVY-4272: repositioned it here from staticImport

          if (!classNode.isSynthetic()) {
            GenericsVisitor genericsVisitor = new GenericsVisitor(source);
            genericsVisitor.visitClass(classNode);
          }
          //
          // Run the Verifier on the outer class
          //
          try {
            verifier.visitClass(classNode);
          } catch (GroovyRuntimeException rpe) {
            ASTNode node = rpe.getNode();
            getErrorCollector()
                .addError(
                    new SyntaxException(
                        rpe.getMessage(),
                        node.getLineNumber(),
                        node.getColumnNumber(),
                        node.getLastLineNumber(),
                        node.getLastColumnNumber()),
                    source);
          }

          LabelVerifier lv = new LabelVerifier(source);
          lv.visitClass(classNode);

          ClassCompletionVerifier completionVerifier = new ClassCompletionVerifier(source);
          completionVerifier.visitClass(classNode);

          ExtendedVerifier xverifier = new ExtendedVerifier(source);
          xverifier.visitClass(classNode);

          // because the class may be generated even if a error was found
          // and that class may have an invalid format we fail here if needed
          getErrorCollector().failIfErrors();

          //
          // Prep the generator machinery
          //
          ClassVisitor visitor = createClassVisitor();

          String sourceName =
              (source == null ? classNode.getModule().getDescription() : source.getName());
          // only show the file name and its extension like javac does in its stacktraces rather
          // than the full path
          // also takes care of both \ and / depending on the host compiling environment
          if (sourceName != null)
            sourceName =
                sourceName.substring(
                    Math.max(sourceName.lastIndexOf('\\'), sourceName.lastIndexOf('/')) + 1);
          AsmClassGenerator generator = new AsmClassGenerator(source, context, visitor, sourceName);

          //
          // Run the generation and create the class (if required)
          //
          // GRECLIPSE: if there are errors, don't generate code.
          // code gen can fail unexpectedly if there was an earlier error.
          if (!source.getErrorCollector().hasErrors()) {
            // end
            generator.visitClass(classNode);

            byte[] bytes = ((ClassWriter) visitor).toByteArray();
            /// GRECLIPSE: start: added classNode, sourceUnit
            /*old{
            generatedClasses.add(new GroovyClass(classNode.getName(), bytes));
            }*/
            // newcode
            generatedClasses.add(new GroovyClass(classNode.getName(), bytes, classNode, source));
            // end

            //
            // Handle any callback that's been set
            //
            if (CompilationUnit.this.classgenCallback != null) {
              classgenCallback.call(visitor, classNode);
            }

            //
            // Recurse for inner classes
            //
            LinkedList innerClasses = generator.getInnerClasses();
            while (!innerClasses.isEmpty()) {
              classgen.call(source, context, (ClassNode) innerClasses.removeFirst());
            }
            // GRECLIPSE: if there are errors, don't generate code
          }
          // end
        }