private Expression findStaticFieldOrPropAccessorImportFromModule(String name) {
    ModuleNode module = currentClass.getModule();
    if (module == null) return null;
    Map<String, ImportNode> importNodes = module.getStaticImports();
    Expression expression;
    String accessorName = getAccessorName(name);
    // look for one of these:
    //   import static MyClass.setProp [as setOtherProp]
    //   import static MyClass.getProp [as getOtherProp]
    // when resolving prop reference
    if (importNodes.containsKey(accessorName)) {
      ImportNode importNode = importNodes.get(accessorName);
      expression =
          findStaticPropertyAccessorByFullName(importNode.getType(), importNode.getFieldName());
      if (expression != null) return expression;
      expression =
          findStaticPropertyAccessor(
              importNode.getType(), getPropNameForAccessor(importNode.getFieldName()));
      if (expression != null) return expression;
    }
    if (accessorName.startsWith("get")) {
      accessorName = "is" + accessorName.substring(3);
      if (importNodes.containsKey(accessorName)) {
        ImportNode importNode = importNodes.get(accessorName);
        expression =
            findStaticPropertyAccessorByFullName(importNode.getType(), importNode.getFieldName());
        if (expression != null) return expression;
        expression =
            findStaticPropertyAccessor(
                importNode.getType(), getPropNameForAccessor(importNode.getFieldName()));
        if (expression != null) return expression;
      }
    }

    // look for one of these:
    //   import static MyClass.prop [as otherProp]
    // when resolving prop or field reference
    if (importNodes.containsKey(name)) {
      ImportNode importNode = importNodes.get(name);
      expression = findStaticPropertyAccessor(importNode.getType(), importNode.getFieldName());
      if (expression != null) return expression;
      expression = findStaticField(importNode.getType(), importNode.getFieldName());
      if (expression != null) return expression;
    }
    // look for one of these:
    //   import static MyClass.*
    // when resolving prop or field reference
    for (ImportNode importNode : module.getStaticStarImports().values()) {
      ClassNode node = importNode.getType();
      expression = findStaticPropertyAccessor(node, name);
      if (expression != null) return expression;
      expression = findStaticField(node, name);
      if (expression != null) return expression;
    }
    return null;
  }
예제 #2
0
  /**
   * A loop driver for applying operations to all primary ClassNodes in our AST. Automatically skips
   * units that have already been processed through the current phase.
   */
  public void applyToPrimaryClassNodes(PrimaryClassNodeOperation body)
      throws CompilationFailedException {
    // GRECLIPSE: start
    /*old{
    Iterator classNodes = getPrimaryClassNodes(body.needSortedInput()).iterator();
    }*/
    // newcode
    List primaryClassNodes = getPrimaryClassNodes(body.needSortedInput());
    Iterator classNodes = primaryClassNodes.iterator();
    // end
    while (classNodes.hasNext()) {
      SourceUnit context = null;
      try {
        ClassNode classNode = (ClassNode) classNodes.next();
        context = classNode.getModule().getContext();
        // GRECLIPSE get to the bottom of this - why are operations running multiple times that
        // should only run once?
        if (context == null
            || context.phase < phase
            || (context.phase == phase && !context.phaseComplete)) {

          int offset = 1;
          Iterator<InnerClassNode> iterator = classNode.getInnerClasses();
          while (iterator.hasNext()) {
            iterator.next();
            offset++;
          }
          body.call(context, new GeneratorContext(this.ast, offset), classNode);
        }
      } catch (CompilationFailedException e) {
        // fall through, getErrorReporter().failIfErrors() will trigger
      } catch (NullPointerException npe) {
        throw npe;
      } catch (GroovyBugError e) {
        changeBugText(e, context);
        throw e;
      } catch (Exception e) {
        // check the exception for a nested compilation exception
        ErrorCollector nestedCollector = null;
        for (Throwable next = e.getCause(); next != e && next != null; next = next.getCause()) {
          if (!(next instanceof MultipleCompilationErrorsException)) continue;
          MultipleCompilationErrorsException mcee = (MultipleCompilationErrorsException) next;
          nestedCollector = mcee.collector;
          break;
        }

        if (nestedCollector != null) {
          getErrorCollector().addCollectorContents(nestedCollector);
        } else {
          getErrorCollector().addError(new ExceptionMessage(e, configuration.getDebug(), this));
        }
      }
    }

    getErrorCollector().failIfErrors();
  }
 private Expression transformMapEntryExpression(
     MapEntryExpression me, ClassNode constructorCallType) {
   Expression key = me.getKeyExpression();
   Expression value = me.getValueExpression();
   ModuleNode module = currentClass.getModule();
   if (module != null && key instanceof ConstantExpression) {
     Map<String, ImportNode> importNodes = module.getStaticImports();
     if (importNodes.containsKey(key.getText())) {
       ImportNode importNode = importNodes.get(key.getText());
       if (importNode.getType().equals(constructorCallType)) {
         String newKey = importNode.getFieldName();
         return new MapEntryExpression(
             new ConstantExpression(newKey), value.transformExpression(this));
       }
     }
   }
   return me;
 }
 private Expression findStaticMethodImportFromModule(Expression method, Expression args) {
   ModuleNode module = currentClass.getModule();
   if (module == null || !(method instanceof ConstantExpression)) return null;
   Map<String, ImportNode> importNodes = module.getStaticImports();
   ConstantExpression ce = (ConstantExpression) method;
   Expression expression;
   Object value = ce.getValue();
   // skip non-Strings, e.g. Integer
   if (!(value instanceof String)) return null;
   final String name = (String) value;
   // look for one of these:
   //   import static SomeClass.method [as otherName]
   // when resolving methodCall() or getProp() or setProp()
   if (importNodes.containsKey(name)) {
     ImportNode importNode = importNodes.get(name);
     expression = findStaticMethod(importNode.getType(), importNode.getFieldName(), args);
     if (expression != null) return expression;
     expression =
         findStaticPropertyAccessorGivenArgs(
             importNode.getType(), getPropNameForAccessor(importNode.getFieldName()), args);
     if (expression != null) {
       return new StaticMethodCallExpression(
           importNode.getType(), importNode.getFieldName(), args);
     }
   }
   // look for one of these:
   //   import static SomeClass.someProp [as otherName]
   // when resolving getProp() or setProp()
   if (validPropName(name)) {
     String propName = getPropNameForAccessor(name);
     if (importNodes.containsKey(propName)) {
       ImportNode importNode = importNodes.get(propName);
       expression =
           findStaticMethod(
               importNode.getType(), prefix(name) + capitalize(importNode.getFieldName()), args);
       if (expression != null) return expression;
       expression =
           findStaticPropertyAccessorGivenArgs(
               importNode.getType(), importNode.getFieldName(), args);
       if (expression != null) {
         return new StaticMethodCallExpression(
             importNode.getType(), prefix(name) + capitalize(importNode.getFieldName()), args);
       }
     }
   }
   Map<String, ImportNode> starImports = module.getStaticStarImports();
   ClassNode starImportType;
   if (currentClass.isEnum() && starImports.containsKey(currentClass.getName())) {
     ImportNode importNode = starImports.get(currentClass.getName());
     starImportType = importNode == null ? null : importNode.getType();
     expression = findStaticMethod(starImportType, name, args);
     if (expression != null) return expression;
   } else {
     for (ImportNode importNode : starImports.values()) {
       starImportType = importNode == null ? null : importNode.getType();
       expression = findStaticMethod(starImportType, name, args);
       if (expression != null) return expression;
       expression =
           findStaticPropertyAccessorGivenArgs(starImportType, getPropNameForAccessor(name), args);
       if (expression != null) {
         return new StaticMethodCallExpression(starImportType, name, args);
       }
     }
   }
   return null;
 }
예제 #5
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
        }
  private Expression findStaticFieldOrPropAccessorImportFromModule(String name) {
    ModuleNode module = currentClass.getModule();
    if (module == null) return null;
    Map<String, ImportNode> importNodes = module.getStaticImports();
    Expression expression = null;
    String accessorName = getAccessorName(name);
    // look for one of these:
    //   import static MyClass.setProp [as setOtherProp]
    //   import static MyClass.getProp [as getOtherProp]
    // when resolving prop reference
    if (importNodes.containsKey(accessorName)) {
      ImportNode importNode = importNodes.get(accessorName);
      expression =
          findStaticPropertyAccessorByFullName(importNode.getType(), importNode.getFieldName());
      if (expression != null) return expression;
      expression =
          findStaticPropertyAccessor(
              importNode.getType(), getPropNameForAccessor(importNode.getFieldName()));
      if (expression != null) return expression;
    }
    if (accessorName.startsWith("get")) {
      accessorName = "is" + accessorName.substring(3);
      if (importNodes.containsKey(accessorName)) {
        ImportNode importNode = importNodes.get(accessorName);
        expression =
            findStaticPropertyAccessorByFullName(importNode.getType(), importNode.getFieldName());
        if (expression != null) return expression;
        expression =
            findStaticPropertyAccessor(
                importNode.getType(), getPropNameForAccessor(importNode.getFieldName()));
        if (expression != null) return expression;
      }
    }

    // look for one of these:
    //   import static MyClass.prop [as otherProp]
    // when resolving prop or field reference
    if (importNodes.containsKey(name)) {
      ImportNode importNode = importNodes.get(name);
      // GRECLIPSE add
      try {
        if (!isReconcile) {
          // GRECLIPSE end
          expression = findStaticPropertyAccessor(importNode.getType(), importNode.getFieldName());
          if (expression != null) return expression;
          // GRECLIPSE add
        }
        // end
        expression = findStaticField(importNode.getType(), importNode.getFieldName());
        if (expression != null) return expression;
        // GRECLIPSE add
      } finally {
        // store the identifier to facilitate organizing static imports
        if (expression != null) expression.setNodeMetaData("static.import.alias", name);
      }
      // GRECLIPSE end
    }
    // look for one of these:
    //   import static MyClass.*
    // when resolving prop or field reference
    for (ImportNode importNode : module.getStaticStarImports().values()) {
      ClassNode node = importNode.getType();
      expression = findStaticPropertyAccessor(node, name);
      if (expression != null) return expression;
      expression = findStaticField(node, name);
      if (expression != null) return expression;
    }
    return null;
  }