private void writeMapDotProperty(
      final Expression receiver,
      final String methodName,
      final MethodVisitor mv,
      final boolean safe) {
    receiver.visit(controller.getAcg()); // load receiver

    Label exit = new Label();
    if (safe) {
      Label doGet = new Label();
      mv.visitJumpInsn(IFNONNULL, doGet);
      controller.getOperandStack().remove(1);
      mv.visitInsn(ACONST_NULL);
      mv.visitJumpInsn(GOTO, exit);
      mv.visitLabel(doGet);
      receiver.visit(controller.getAcg());
    }

    mv.visitLdcInsn(methodName); // load property name
    mv.visitMethodInsn(
        INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;", true);
    if (safe) {
      mv.visitLabel(exit);
    }
    controller.getOperandStack().replace(OBJECT_TYPE);
  }
  private VariableSlotLoader loadWithSubscript(Expression expression) {
    final OperandStack operandStack = controller.getOperandStack();
    // if we have a BinaryExpression, let us check if it is with
    // subscription
    if (expression instanceof BinaryExpression) {
      BinaryExpression be = (BinaryExpression) expression;
      if (be.getOperation().getType() == Types.LEFT_SQUARE_BRACKET) {
        // right expression is the subscript expression
        // we store the result of the subscription on the stack
        Expression subscript = be.getRightExpression();
        subscript.visit(controller.getAcg());
        ClassNode subscriptType = operandStack.getTopOperand();
        int id =
            controller.getCompileStack().defineTemporaryVariable("$subscript", subscriptType, true);
        VariableSlotLoader subscriptExpression =
            new VariableSlotLoader(subscriptType, id, operandStack);
        // do modified visit
        BinaryExpression newBe =
            new BinaryExpression(be.getLeftExpression(), be.getOperation(), subscriptExpression);
        newBe.copyNodeMetaData(be);
        newBe.setSourcePosition(be);
        newBe.visit(controller.getAcg());
        return subscriptExpression;
      }
    }

    // normal loading of expression
    expression.visit(controller.getAcg());
    return null;
  }
  protected void evaluateCompareExpression(
      MethodCaller compareMethod, BinaryExpression expression) {
    Expression leftExp = expression.getLeftExpression();
    TypeChooser typeChooser = controller.getTypeChooser();
    ClassNode cn = controller.getClassNode();
    ClassNode leftType = typeChooser.resolveType(leftExp, cn);
    Expression rightExp = expression.getRightExpression();
    ClassNode rightType = typeChooser.resolveType(rightExp, cn);

    boolean done = false;
    if (ClassHelper.isPrimitiveType(leftType) && ClassHelper.isPrimitiveType(rightType)) {
      BinaryExpressionMultiTypeDispatcher helper =
          new BinaryExpressionMultiTypeDispatcher(getController());
      done = helper.doPrimitiveCompare(leftType, rightType, expression);
    }

    if (!done) {
      AsmClassGenerator acg = controller.getAcg();
      OperandStack operandStack = controller.getOperandStack();

      leftExp.visit(acg);
      operandStack.box();
      rightExp.visit(acg);
      operandStack.box();

      compareMethod.call(controller.getMethodVisitor());
      ClassNode resType = ClassHelper.boolean_TYPE;
      if (compareMethod == findRegexMethod) {
        resType = ClassHelper.OBJECT_TYPE;
      }
      operandStack.replace(resType, 2);
    }
  }
  @Override
  protected void performInjectionInternal(
      String apiInstanceProperty, SourceUnit source, ClassNode classNode) {
    List<PropertyNode> tags = findTags(classNode);

    PropertyNode namespaceProperty = classNode.getProperty(NAMESPACE_PROPERTY);
    String namespace = GroovyPage.DEFAULT_NAMESPACE;
    if (namespaceProperty != null && namespaceProperty.isStatic()) {
      Expression initialExpression = namespaceProperty.getInitialExpression();
      if (initialExpression instanceof ConstantExpression) {
        namespace = initialExpression.getText();
      }
    }

    addGetTagLibNamespaceMethod(classNode, namespace);

    MethodCallExpression tagLibraryLookupMethodCall =
        new MethodCallExpression(
            new VariableExpression(apiInstanceProperty, ClassHelper.make(TagLibraryApi.class)),
            "getTagLibraryLookup",
            ZERO_ARGS);
    for (PropertyNode tag : tags) {
      String tagName = tag.getName();
      addAttributesAndBodyMethod(classNode, tagLibraryLookupMethodCall, tagName);
      addAttributesAndStringBodyMethod(classNode, tagName);
      addAttributesAndBodyMethod(classNode, tagLibraryLookupMethodCall, tagName, false);
      addAttributesAndBodyMethod(classNode, tagLibraryLookupMethodCall, tagName, true, false);
      addAttributesAndBodyMethod(classNode, tagLibraryLookupMethodCall, tagName, false, false);
    }
  }
  public void visitConstructorCallExpression(ConstructorCallExpression call) {
    isSpecialConstructorCall = call.isSpecialCall();
    super.visitConstructorCallExpression(call);
    isSpecialConstructorCall = false;
    if (!call.isUsingAnonymousInnerClass()) return;

    pushState();
    InnerClassNode innerClass = (InnerClassNode) call.getType();
    innerClass.setVariableScope(currentScope);
    for (MethodNode method : innerClass.getMethods()) {
      Parameter[] parameters = method.getParameters();
      if (parameters.length == 0) parameters = null; // null means no implicit "it"
      ClosureExpression cl = new ClosureExpression(parameters, method.getCode());
      visitClosureExpression(cl);
    }

    for (FieldNode field : innerClass.getFields()) {
      final Expression expression = field.getInitialExpression();
      if (expression != null) {
        expression.visit(this);
      }
    }

    for (Statement statement : innerClass.getObjectInitializerStatements()) {
      statement.visit(this);
    }
    markClosureSharedVariables();
    popState();
  }
Ejemplo n.º 6
0
  protected Expression transformPropertyExpression(PropertyExpression pe) {
    boolean oldInPropertyExpression = inPropertyExpression;
    Expression oldFoundArgs = foundArgs;
    Expression oldFoundConstant = foundConstant;
    inPropertyExpression = true;
    foundArgs = null;
    foundConstant = null;
    Expression objectExpression = transform(pe.getObjectExpression());
    boolean candidate = false;
    if (objectExpression instanceof MethodCallExpression) {
      candidate = ((MethodCallExpression) objectExpression).isImplicitThis();
    }

    if (foundArgs != null && foundConstant != null && candidate) {
      Expression result = findStaticMethodImportFromModule(foundConstant, foundArgs);
      if (result != null) {
        objectExpression = result;
        objectExpression.setSourcePosition(pe);
      }
    }
    inPropertyExpression = oldInPropertyExpression;
    foundArgs = oldFoundArgs;
    foundConstant = oldFoundConstant;
    pe.setObjectExpression(objectExpression);
    return pe;
  }
Ejemplo n.º 7
0
 private static String lookupCategoryName(AnnotationNode logAnnotation) {
   Expression member = logAnnotation.getMember("category");
   if (member != null && member.getText() != null) {
     return member.getText();
   }
   return DEFAULT_CATEGORY_NAME;
 }
Ejemplo n.º 8
0
  private Expression transformInlineConstants(Expression exp) {
    if (exp instanceof PropertyExpression) {
      PropertyExpression pe = (PropertyExpression) exp;
      if (pe.getObjectExpression() instanceof ClassExpression) {
        ClassExpression ce = (ClassExpression) pe.getObjectExpression();
        ClassNode type = ce.getType();
        if (type.isEnum()) return exp;
        Expression constant = findConstant(type.getField(pe.getPropertyAsString()));
        // GRECLIPSE edit
        // if (constant != null) return constant;
        if (constant != null) {
          String name = pe.getText().replace('$', '.');
          Object alias = pe.getNodeMetaData("static.import.alias");
          if (alias != null && !alias.equals(pe.getPropertyAsString())) {
            name += " as " + alias;
          }
          // store the qualified name to facilitate organizing static imports
          constant.setNodeMetaData("static.import", name);

          return constant;
        }
        // GRECLIPSE end
      }
    } else if (exp instanceof ListExpression) {
      ListExpression le = (ListExpression) exp;
      ListExpression result = new ListExpression();
      for (Expression e : le.getExpressions()) {
        result.addExpression(transformInlineConstants(e));
      }
      return result;
    }

    return exp;
  }
Ejemplo n.º 9
0
 @Override
 public void visitBinaryExpression(final BinaryExpression expression) {
   boolean assignment =
       StaticTypeCheckingSupport.isAssignment(expression.getOperation().getType());
   boolean isDeclaration = expression instanceof DeclarationExpression;
   Expression leftExpression = expression.getLeftExpression();
   Expression rightExpression = expression.getRightExpression();
   if (isDeclaration && leftExpression instanceof VariableExpression) {
     VariableExpression var = (VariableExpression) leftExpression;
     if (Modifier.isFinal(var.getModifiers())) {
       declaredFinalVariables.add(var);
     }
   }
   leftExpression.visit(this);
   inAssignment = assignment;
   rightExpression.visit(this);
   inAssignment = false;
   if (assignment) {
     if (leftExpression instanceof Variable) {
       boolean uninitialized = isDeclaration && rightExpression == EmptyExpression.INSTANCE;
       recordAssignment(
           (Variable) leftExpression, isDeclaration, uninitialized, false, expression);
     }
   }
 }
 private Expression transformMethodCallExpression(final MethodCallExpression exp) {
   Expression objectExpression = transform(exp.getObjectExpression());
   ClassNode traitReceiver = objectExpression.getNodeMetaData(SuperCallTraitTransformer.class);
   if (traitReceiver != null) {
     TraitHelpersTuple helpers = Traits.findHelpers(traitReceiver);
     // (SomeTrait.super).foo() --> SomeTrait$Helper.foo(this)
     ClassExpression receiver = new ClassExpression(helpers.getHelper());
     ArgumentListExpression newArgs = new ArgumentListExpression();
     Expression arguments = exp.getArguments();
     newArgs.addExpression(new VariableExpression("this"));
     if (arguments instanceof TupleExpression) {
       List<Expression> expressions = ((TupleExpression) arguments).getExpressions();
       for (Expression expression : expressions) {
         newArgs.addExpression(transform(expression));
       }
     } else {
       newArgs.addExpression(arguments);
     }
     MethodCallExpression result = new MethodCallExpression(receiver, exp.getMethod(), newArgs);
     result.setImplicitThis(false);
     result.setSpreadSafe(exp.isSpreadSafe());
     result.setSafe(exp.isSafe());
     result.setSourcePosition(exp);
     return result;
   }
   return super.transform(exp);
 }
  @Override
  protected void assignToArray(
      Expression orig, Expression receiver, Expression index, Expression rhsValueLoader) {
    ClassNode current = getController().getClassNode();
    ClassNode arrayType = getController().getTypeChooser().resolveType(receiver, current);
    ClassNode arrayComponentType = arrayType.getComponentType();
    int operationType = getOperandType(arrayComponentType);
    BinaryExpressionWriter bew = binExpWriter[operationType];
    AsmClassGenerator acg = getController().getAcg();

    if (bew.arraySet(true) && arrayType.isArray()) {
      OperandStack operandStack = getController().getOperandStack();

      // load the array
      receiver.visit(acg);
      operandStack.doGroovyCast(arrayType);

      // load index
      index.visit(acg);
      operandStack.doGroovyCast(int_TYPE);

      // load rhs
      rhsValueLoader.visit(acg);
      operandStack.doGroovyCast(arrayComponentType);

      // store value in array
      bew.arraySet(false);

      // load return value && correct operand stack stack
      operandStack.remove(3);
      rhsValueLoader.visit(acg);
    } else {
      super.assignToArray(orig, receiver, index, rhsValueLoader);
    }
  }
 @Override
 public void makeSingleArgumentCall(
     final Expression receiver, final String message, final Expression arguments) {
   TypeChooser typeChooser = controller.getTypeChooser();
   ClassNode classNode = controller.getClassNode();
   ClassNode rType = typeChooser.resolveType(receiver, classNode);
   ClassNode aType = typeChooser.resolveType(arguments, classNode);
   if (trySubscript(receiver, message, arguments, rType, aType)) {
     return;
   }
   // new try with flow type instead of declaration type
   rType = receiver.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
   if (rType != null && trySubscript(receiver, message, arguments, rType, aType)) {
     return;
   }
   // todo: more cases
   throw new GroovyBugError(
       "At line "
           + receiver.getLineNumber()
           + " column "
           + receiver.getColumnNumber()
           + "\n"
           + "On receiver: "
           + receiver.getText()
           + " with message: "
           + message
           + " and arguments: "
           + arguments.getText()
           + "\n"
           + "This method should not have been called. Please try to create a simple example reproducing this error and file"
           + "a bug report at http://jira.codehaus.org/browse/GROOVY");
 }
Ejemplo n.º 13
0
 private static String lookupLogFieldName(AnnotationNode logAnnotation) {
   Expression member = logAnnotation.getMember("value");
   if (member != null && member.getText() != null) {
     return member.getText();
   } else {
     return "log";
   }
 }
 @Override
 public void visitConstantExpression(ConstantExpression expression) {
   // check for inlined constant (see ResolveVisitor.transformInlineConstants)
   Expression original = expression.getNodeMetaData(ORIGINAL_EXPRESSION);
   if (original != null) {
     original.visit(this);
   }
 }
  private Class[] getTransformClasses(ClassNode classNode) {
    if (!classNode.hasClass()) {
      List<AnnotationNode> annotations = classNode.getAnnotations();
      AnnotationNode transformAnnotation = null;
      for (AnnotationNode anno : annotations) {
        if (anno.getClassNode().getName().equals(GroovyASTTransformationClass.class.getName())) {
          transformAnnotation = anno;
          break;
        }
      }
      if (transformAnnotation != null) {
        Expression expr = (Expression) transformAnnotation.getMember("classes");
        if (expr == null) {
          return NO_CLASSES;
        }
        Class<?>[] values = NO_CLASSES;
        // Will need to extract the classnames
        if (expr instanceof ListExpression) {
          List<Class<?>> loadedClasses = new ArrayList<Class<?>>();
          ListExpression expression = (ListExpression) expr;
          List<Expression> expressions = expression.getExpressions();
          for (Expression oneExpr : expressions) {
            String classname = ((ClassExpression) oneExpr).getType().getName();
            try {
              Class<?> clazz = Class.forName(classname, false, transformLoader);
              loadedClasses.add(clazz);
            } catch (ClassNotFoundException cnfe) {
              source
                  .getErrorCollector()
                  .addError(
                      new SimpleMessage(
                          "Ast transform processing, cannot find " + classname, source));
            }
          }
          if (loadedClasses.size() != 0) {
            values = loadedClasses.toArray(new Class<?>[loadedClasses.size()]);
          }
          return values;
        } else {

        }

        throw new RuntimeException(
            "nyi implemented in eclipse: need to support: "
                + expr
                + " (class="
                + expr.getClass()
                + ")");
      }
      return null;
    } else {
      Annotation transformClassAnnotation = getTransformClassAnnotation(classNode);
      if (transformClassAnnotation == null) {
        return null;
      }
      return getTransformClasses(transformClassAnnotation);
    }
  }
  private void evaluateElvisOperatorExpression(ElvisOperatorExpression expression) {
    MethodVisitor mv = controller.getMethodVisitor();
    CompileStack compileStack = controller.getCompileStack();
    OperandStack operandStack = controller.getOperandStack();
    TypeChooser typeChooser = controller.getTypeChooser();

    Expression boolPart = expression.getBooleanExpression().getExpression();
    Expression falsePart = expression.getFalseExpression();

    ClassNode truePartType = typeChooser.resolveType(boolPart, controller.getClassNode());
    ClassNode falsePartType = typeChooser.resolveType(falsePart, controller.getClassNode());
    ClassNode common = WideningCategories.lowestUpperBound(truePartType, falsePartType);

    // x?:y is equal to x?x:y, which evals to
    //      var t=x; boolean(t)?t:y
    // first we load x, dup it, convert the dupped to boolean, then
    // jump depending on the value. For true we are done, for false we
    // have to load y, thus we first remove x and then load y.
    // But since x and y may have different stack lengths, this cannot work
    // Thus we have to have to do the following:
    // Be X the type of x, Y the type of y and S the common supertype of
    // X and Y, then we have to see x?:y as
    //      var t=x;boolean(t)?S(t):S(y)
    // so we load x, dup it, store the value in a local variable (t), then
    // do boolean conversion. In the true part load t and cast it to S,
    // in the false part load y and cast y to S

    // load x, dup it, store one in $t and cast the remaining one to boolean
    int mark = operandStack.getStackLength();
    boolPart.visit(controller.getAcg());
    operandStack.dup();
    if (ClassHelper.isPrimitiveType(truePartType)
        && !ClassHelper.isPrimitiveType(operandStack.getTopOperand())) {
      truePartType = ClassHelper.getWrapper(truePartType);
    }
    int retValueId = compileStack.defineTemporaryVariable("$t", truePartType, true);
    operandStack.castToBool(mark, true);

    Label l0 = operandStack.jump(IFEQ);
    // true part: load $t and cast to S
    operandStack.load(truePartType, retValueId);
    operandStack.doGroovyCast(common);
    Label l1 = new Label();
    mv.visitJumpInsn(GOTO, l1);

    // false part: load false expression and cast to S
    mv.visitLabel(l0);
    falsePart.visit(controller.getAcg());
    operandStack.doGroovyCast(common);

    // finish and cleanup
    mv.visitLabel(l1);
    compileStack.removeVar(retValueId);
    controller.getOperandStack().replace(common, 2);
  }
 private Expression transformPropertyExpression(final PropertyExpression expression) {
   Expression objectExpression = expression.getObjectExpression();
   ClassNode type = objectExpression.getType();
   if (objectExpression instanceof ClassExpression) {
     if (Traits.isTrait(type) && "super".equals(expression.getPropertyAsString())) {
       // SomeTrait.super --> annotate to recognize later
       expression.putNodeMetaData(SuperCallTraitTransformer.class, type);
     }
   }
   return super.transform(expression);
 }
Ejemplo n.º 18
0
 protected void visitListOfExpressions(List<? extends Expression> list) {
   if (list == null) return;
   for (Expression expression : list) {
     if (expression instanceof SpreadExpression) {
       Expression spread = ((SpreadExpression) expression).getExpression();
       spread.visit(this);
     } else {
       expression.visit(this);
     }
   }
 }
  public void visitProperty(PropertyNode node) {
    visitAnnotations(node);
    Statement statement = node.getGetterBlock();
    visitClassCodeContainer(statement);

    statement = node.getSetterBlock();
    visitClassCodeContainer(statement);

    Expression init = node.getInitialExpression();
    if (init != null) init.visit(this);
  }
 public void visitAnnotations(AnnotatedNode node) {
   List<AnnotationNode> annotations = node.getAnnotations();
   if (annotations.isEmpty()) return;
   for (AnnotationNode an : annotations) {
     // skip built-in properties
     if (an.isBuiltIn()) continue;
     for (Map.Entry<String, Expression> member : an.getMembers().entrySet()) {
       Expression annMemberValue = member.getValue();
       annMemberValue.visit(this);
     }
   }
 }
 private List createPropertiesForHasManyExpression(Expression e, ClassNode classNode) {
   List properties = new ArrayList();
   if (e instanceof MapExpression) {
     MapExpression me = (MapExpression) e;
     List mapEntries = me.getMapEntryExpressions();
     for (Iterator j = mapEntries.iterator(); j.hasNext(); ) {
       MapEntryExpression mee = (MapEntryExpression) j.next();
       Expression keyExpression = mee.getKeyExpression();
       String key = keyExpression.getText();
       addAssociationForKey(key, properties, classNode);
     }
   }
   return properties;
 }
 protected void visitListOfExpressions(List<? extends Expression> list) {
   if (list == null) return;
   for (Expression expression : list) {
     if (expression instanceof SpreadExpression) {
       Expression spread = ((SpreadExpression) expression).getExpression();
       spread.visit(this);
     } else {
       // GRECLIPSE: start: could be null for malformed code (GRE290)
       if (expression != null)
         // end
         expression.visit(this);
     }
   }
 }
Ejemplo n.º 23
0
 private void changeBaseScriptTypeFromPackageOrImport(
     final SourceUnit source, final AnnotatedNode parent, final AnnotationNode node) {
   Expression value = node.getMember("value");
   if (!(value instanceof ClassExpression)) {
     addError("Annotation " + MY_TYPE_NAME + " member 'value' should be a class literal.", value);
     return;
   }
   List<ClassNode> classes = source.getAST().getClasses();
   for (ClassNode classNode : classes) {
     if (classNode.isScriptBody()) {
       changeBaseScriptType(parent, classNode, value.getType());
     }
   }
 }
Ejemplo n.º 24
0
 public Expression transform(Expression exp) {
   if (exp == null) return null;
   if (exp.getClass() == VariableExpression.class) {
     return transformVariableExpression((VariableExpression) exp);
   }
   if (exp.getClass() == BinaryExpression.class) {
     return transformBinaryExpression((BinaryExpression) exp);
   }
   if (exp.getClass() == PropertyExpression.class) {
     return transformPropertyExpression((PropertyExpression) exp);
   }
   if (exp.getClass() == MethodCallExpression.class) {
     return transformMethodCallExpression((MethodCallExpression) exp);
   }
   if (exp.getClass() == ClosureExpression.class) {
     return transformClosureExpression((ClosureExpression) exp);
   }
   if (exp.getClass() == ConstructorCallExpression.class) {
     return transformConstructorCallExpression((ConstructorCallExpression) exp);
   }
   if (exp.getClass() == ArgumentListExpression.class) {
     Expression result = exp.transformExpression(this);
     if (inPropertyExpression) {
       foundArgs = result;
     }
     return result;
   }
   if (exp instanceof ConstantExpression) {
     Expression result = exp.transformExpression(this);
     if (inPropertyExpression) {
       foundConstant = result;
     }
     if (inAnnotation && exp instanceof AnnotationConstantExpression) {
       ConstantExpression ce = (ConstantExpression) result;
       if (ce.getValue() instanceof AnnotationNode) {
         // replicate a little bit of AnnotationVisitor here
         // because we can't wait until later to do this
         AnnotationNode an = (AnnotationNode) ce.getValue();
         Map<String, Expression> attributes = an.getMembers();
         for (Map.Entry<String, Expression> entry : attributes.entrySet()) {
           Expression attrExpr = transform(entry.getValue());
           entry.setValue(attrExpr);
         }
       }
     }
     return result;
   }
   return exp.transformExpression(this);
 }
 /**
  * For the supplied classnode, this method will check if there is an annotation on it of kind
  * 'GroovyASTTransformationClass'. If there is then the 'value' member of that annotation will be
  * retrieved and the value considered to be the class name of a transformation.
  *
  * @return null if no annotation found, otherwise a String[] of classnames - this will be size 0
  *     if no value was specified
  */
 private String[] getTransformClassNames(ClassNode cn) {
   if (!cn.hasClass()) {
     List<AnnotationNode> annotations = cn.getAnnotations();
     AnnotationNode transformAnnotation = null;
     for (AnnotationNode anno : annotations) {
       if (anno.getClassNode().getName().equals(GroovyASTTransformationClass.class.getName())) {
         transformAnnotation = anno;
         break;
       }
     }
     if (transformAnnotation != null) {
       // will work so long as signature for the member 'value' is String[]
       Expression expr2 = transformAnnotation.getMember("value");
       String[] values = null;
       if (expr2 == null) {
         return NONE;
       }
       if (expr2 instanceof ListExpression) {
         ListExpression expression = (ListExpression) expr2;
         List<Expression> expressions = expression.getExpressions();
         values = new String[expressions.size()];
         int e = 0;
         for (Expression expr : expressions) {
           values[e++] = ((ConstantExpression) expr).getText();
         }
       } else if (expr2 instanceof ConstantExpression) {
         values = new String[1];
         values[0] = ((ConstantExpression) expr2).getText();
       } else {
         throw new IllegalStateException(
             "NYI: eclipse doesn't understand this kind of expression in an Ast transform definition: "
                 + expr2
                 + " (class="
                 + expr2.getClass().getName()
                 + ")");
       }
       return values;
     }
     return null;
   } else {
     // FIXASC check haven't broken transforms for 'vanilla' (outside of eclipse) execution of
     // groovyc
     Annotation transformClassAnnotation = getTransformClassAnnotation(cn);
     if (transformClassAnnotation == null) {
       return null;
     }
     return getTransformClassNames(transformClassAnnotation);
   }
 }
Ejemplo n.º 26
0
  public static Expression transformExpression(final Expression exp, CompilerTransformer compiler) {
    if (exp instanceof BytecodeExpression) {
      if (exp instanceof BytecodeExpr) return exp;

      return new BytecodeExpr(exp, exp.getType()) {
        protected void compile(MethodVisitor mv) {
          ((BytecodeExpression) exp).visit(mv);
        }
      };
    }

    ExprTransformer t = transformers.get(exp.getClass());
    if (t == null) return compiler.transformImpl(exp);

    return t.transform(exp, compiler);
  }
Ejemplo n.º 27
0
 /**
  * Creates a 'match'. It also checks whether the 'found' expression's text actually contains the
  * searched text and ensures to make the match cover exactly the searchedText.
  */
 public Match createMatch(Expression valueExpression, String searchedText) throws CoreException {
   int start = valueExpression.getStart();
   int len = valueExpression.getLength();
   String foundText = cuText.substring(start, start + len);
   int displace = foundText.indexOf(searchedText);
   if (displace >= 0) {
     IJavaElement el = cu.getElementAt(start);
     return new Match(el, start + displace, searchedText.length());
   } else {
     throw new CoreException(
         new Status(
             IStatus.ERROR,
             GrailsCoreActivator.PLUGIN_ID,
             "Found AST node, but it doesn't contain searched text"));
   }
 }
  private void evaluateCompareTo(BinaryExpression expression) {
    Expression leftExpression = expression.getLeftExpression();
    AsmClassGenerator acg = controller.getAcg();
    OperandStack operandStack = controller.getOperandStack();

    leftExpression.visit(acg);
    operandStack.box();

    // if the right hand side is a boolean expression, we need to autobox
    Expression rightExpression = expression.getRightExpression();
    rightExpression.visit(acg);
    operandStack.box();

    compareToMethod.call(controller.getMethodVisitor());
    operandStack.replace(ClassHelper.Integer_TYPE, 2);
  }
  @Override
  protected boolean existsProperty(
      final PropertyExpression pexp,
      final boolean checkForReadOnly,
      final ClassCodeVisitorSupport visitor) {
    Expression objectExpression = pexp.getObjectExpression();
    ClassNode objectExpressionType = getType(objectExpression);
    final Reference<ClassNode> rType = new Reference<ClassNode>(objectExpressionType);
    ClassCodeVisitorSupport receiverMemoizer =
        new ClassCodeVisitorSupport() {
          @Override
          protected SourceUnit getSourceUnit() {
            return null;
          }

          public void visitField(final FieldNode node) {
            if (visitor != null) visitor.visitField(node);
            ClassNode declaringClass = node.getDeclaringClass();
            if (declaringClass != null) rType.set(declaringClass);
          }

          public void visitMethod(final MethodNode node) {
            if (visitor != null) visitor.visitMethod(node);
            ClassNode declaringClass = node.getDeclaringClass();
            if (declaringClass != null) rType.set(declaringClass);
          }

          @Override
          public void visitProperty(final PropertyNode node) {
            if (visitor != null) visitor.visitProperty(node);
            ClassNode declaringClass = node.getDeclaringClass();
            if (declaringClass != null) rType.set(declaringClass);
          }
        };
    boolean exists = super.existsProperty(pexp, checkForReadOnly, receiverMemoizer);
    if (exists) {
      if (objectExpression.getNodeMetaData(StaticCompilationMetadataKeys.PROPERTY_OWNER) == null) {
        objectExpression.putNodeMetaData(StaticCompilationMetadataKeys.PROPERTY_OWNER, rType.get());
      }
      if (StaticTypeCheckingSupport.implementsInterfaceOrIsSubclassOf(
          objectExpressionType, ClassHelper.LIST_TYPE)) {
        objectExpression.putNodeMetaData(
            COMPONENT_TYPE, inferComponentType(objectExpressionType, ClassHelper.int_TYPE));
      }
    }
    return exists;
  }
 private void writeArrayGet(
     final Expression receiver,
     final Expression arguments,
     final ClassNode rType,
     final ClassNode aType) {
   OperandStack operandStack = controller.getOperandStack();
   int m1 = operandStack.getStackLength();
   // visit receiver
   receiver.visit(controller.getAcg());
   // visit arguments as array index
   arguments.visit(controller.getAcg());
   operandStack.doGroovyCast(int_TYPE);
   int m2 = operandStack.getStackLength();
   // array access
   controller.getMethodVisitor().visitInsn(AALOAD);
   operandStack.replace(rType.getComponentType(), m2 - m1);
 }