コード例 #1
0
 public IGroovyProposal toProposal(ClassNode declaringType, ResolverCache resolver) {
   GroovyMethodProposal groovyMethodProposal =
       new GroovyMethodProposal(toMethod(declaringType.redirect(), resolver), provider, options);
   groovyMethodProposal.setUseNamedArguments(useNamedArgs);
   groovyMethodProposal.setRelevanceMultiplier(relevanceMultiplier);
   return groovyMethodProposal;
 }
コード例 #2
0
  private void printDefaultValue(PrintWriter out, ClassNode type) {
    if (type.redirect() != ClassHelper.OBJECT_TYPE && type.redirect() != ClassHelper.boolean_TYPE) {
      out.print("(");
      printType(out, type);
      out.print(")");
    }

    if (ClassHelper.isPrimitiveType(type)) {
      if (type == ClassHelper.boolean_TYPE) {
        out.print("false");
      } else {
        out.print("0");
      }
    } else {
      out.print("null");
    }
  }
コード例 #3
0
 /** @return the ClassNode of the super class of this type */
 public ClassNode getSuperClass() {
   if (!lazyInitDone && !isResolved()) {
     throw new GroovyBugError(
         "ClassNode#getSuperClass for " + getName() + " called before class resolving");
   }
   ClassNode sn = redirect().getUnresolvedSuperClass();
   if (sn != null) sn = sn.redirect();
   return sn;
 }
コード例 #4
0
 /**
  * Sets this instance as proxy for the given ClassNode.
  *
  * @param cn the class to redirect to. If set to null the redirect will be removed
  */
 public void setRedirect(ClassNode cn) {
   if (isPrimaryNode)
     throw new GroovyBugError(
         "tried to set a redirect for a primary ClassNode ("
             + getName()
             + "->"
             + cn.getName()
             + ").");
   if (cn != null) cn = cn.redirect();
   if (cn == this) return;
   redirect = cn;
 }
コード例 #5
0
ファイル: Java5.java プロジェクト: TTwxj/groovy
 public static GenericsType configureTypeVariableDefinition(ClassNode base, ClassNode[] cBounds) {
   ClassNode redirect = base.redirect();
   base.setRedirect(null);
   GenericsType gt;
   if (cBounds == null || cBounds.length == 0) {
     gt = new GenericsType(base);
   } else {
     gt = new GenericsType(base, cBounds, null);
     gt.setName(base.getName());
     gt.setPlaceholder(true);
   }
   base.setRedirect(redirect);
   return gt;
 }
コード例 #6
0
  private void doConvertAndCast(ClassNode targetType, boolean coerce) {
    int size = stack.size();
    throwExceptionForNoStackElement(size, targetType, coerce);

    ClassNode top = stack.get(size - 1);
    targetType = targetType.redirect();
    if (targetType == top) return;

    if (coerce) {
      controller.getInvocationWriter().coerce(top, targetType);
      return;
    }

    boolean primTarget = ClassHelper.isPrimitiveType(targetType);
    boolean primTop = ClassHelper.isPrimitiveType(top);

    if (primTop && primTarget) {
      // here we box and unbox to get the goal type
      if (convertPrimitive(top, targetType)) {
        replace(targetType);
        return;
      }
      box();
    } else if (primTop) {
      // top is primitive, target is not
      // so box and do groovy cast
      controller.getInvocationWriter().castToNonPrimitiveIfNecessary(top, targetType);
    } else if (primTarget) {
      // top is not primitive so unbox
      // leave that BH#doCast later
    } else {
      controller.getInvocationWriter().castToNonPrimitiveIfNecessary(top, targetType);
    }

    MethodVisitor mv = controller.getMethodVisitor();
    if (primTarget
        && !ClassHelper.boolean_TYPE.equals(targetType)
        && !primTop
        && ClassHelper.getWrapper(targetType).equals(top)) {
      BytecodeHelper.doCastToPrimitive(mv, top, targetType);
    } else {
      top = stack.get(size - 1);
      if (!WideningCategories.implementsInterfaceOrSubclassOf(top, targetType)) {
        BytecodeHelper.doCast(mv, targetType);
      }
    }
    replace(targetType);
  }
コード例 #7
0
  private static boolean isDirectAccessAllowed(
      FieldNode a, ClassNode receiver, boolean isSamePackage) {
    ClassNode declaringClass = a.getDeclaringClass().redirect();
    ClassNode receiverType = receiver.redirect();

    // first, direct access from within the class or inner class nodes
    if (declaringClass.equals(receiverType)) return true;
    if (receiverType instanceof InnerClassNode) {
      while (receiverType != null && receiverType instanceof InnerClassNode) {
        if (declaringClass.equals(receiverType)) return true;
        receiverType = receiverType.getOuterClass();
      }
    }

    // no getter
    return a.isPublic() || (a.isProtected() && isSamePackage);
  }
コード例 #8
0
ファイル: Java5.java プロジェクト: bodiam/discobot
 private GenericsType configureTypeVariableDefinition(TypeVariable tv) {
   ClassNode base = configureTypeVariableReference(tv);
   ClassNode redirect = base.redirect();
   base.setRedirect(null);
   Type[] tBounds = tv.getBounds();
   GenericsType gt;
   if (tBounds.length == 0) {
     gt = new GenericsType(base);
   } else {
     ClassNode[] cBounds = configureTypes(tBounds);
     gt = new GenericsType(base, cBounds, null);
     gt.setName(base.getName());
     gt.setPlaceholder(true);
   }
   base.setRedirect(redirect);
   return gt;
 }
コード例 #9
0
  public List<IGroovyProposal> extraProposals(
      ClassNode declaringType, ResolverCache resolver, Expression expression) {
    // first find the arguments that are possible
    Map<String, ClassNode> availableParams = findAvailableParamNames(resolver);

    if (availableParams.isEmpty()) {
      return ProposalUtils.NO_PROPOSALS;
    }

    if (expression instanceof MethodCallExpression) {
      // next find out if there are any existing named args
      MethodCallExpression call = (MethodCallExpression) expression;
      Expression arguments = call.getArguments();
      if (arguments instanceof TupleExpression) {
        for (Expression maybeArg : ((TupleExpression) arguments).getExpressions()) {
          if (maybeArg instanceof MapExpression) {
            arguments = maybeArg;
            break;
          }
        }
      }

      // now remove the arguments that are already written
      if (arguments instanceof MapExpression) {
        // Do extra filtering to determine what parameters are still available
        MapExpression enclosingCallArgs = (MapExpression) arguments;
        for (MapEntryExpression entry : enclosingCallArgs.getMapEntryExpressions()) {
          String paramName = entry.getKeyExpression().getText();
          availableParams.remove(paramName);
        }
      }
    }

    List<IGroovyProposal> extraProposals = new ArrayList<IGroovyProposal>(availableParams.size());
    for (Entry<String, ClassNode> available : availableParams.entrySet()) {
      extraProposals.add(
          new GroovyNamedArgumentProposal(
              available.getKey(),
              available.getValue(),
              toMethod(declaringType.redirect(), resolver),
              provider));
    }
    return extraProposals;
  }
コード例 #10
0
 private String createClassLabel(ClassNode node) {
   StringBuilder sb = new StringBuilder();
   node = node.redirect();
   if (ClassHelper.DYNAMIC_TYPE == node) {
     return "def";
   }
   sb.append(node.getNameWithoutPackage());
   GenericsType[] genericsTypes = node.getGenericsTypes();
   if (genericsTypes != null && genericsTypes.length > 0) {
     sb.append(" <");
     for (int i = 0; i < genericsTypes.length; i++) {
       sb.append(genericsTypes[i].getName());
       if (i < genericsTypes.length - 1) {
         sb.append(", ");
       }
     }
     sb.append("> ");
   }
   return sb.toString();
 }
コード例 #11
0
  /**
   * For a given classnode, fills in the supplied map with the parameterized types it defines.
   *
   * @param node
   * @param map
   */
  public static void extractPlaceholders(ClassNode node, Map<String, GenericsType> map) {
    if (node == null) return;

    if (node.isArray()) {
      extractPlaceholders(node.getComponentType(), map);
      return;
    }

    if (!node.isUsingGenerics() || !node.isRedirectNode()) return;
    GenericsType[] parameterized = node.getGenericsTypes();
    if (parameterized == null || parameterized.length == 0) return;
    GenericsType[] redirectGenericsTypes = node.redirect().getGenericsTypes();
    if (redirectGenericsTypes == null) redirectGenericsTypes = parameterized;
    for (int i = 0; i < redirectGenericsTypes.length; i++) {
      GenericsType redirectType = redirectGenericsTypes[i];
      if (redirectType.isPlaceholder()) {
        String name = redirectType.getName();
        if (!map.containsKey(name)) map.put(name, parameterized[i]);
      }
    }
  }
コード例 #12
0
 @SuppressWarnings("unchecked")
 private boolean makeGetPrivateFieldWithBridgeMethod(
     final Expression receiver,
     final ClassNode receiverType,
     final String fieldName,
     final boolean safe,
     final boolean implicitThis) {
   FieldNode field = receiverType.getField(fieldName);
   ClassNode classNode = controller.getClassNode();
   if (field != null
       && Modifier.isPrivate(field.getModifiers())
       && (StaticInvocationWriter.isPrivateBridgeMethodsCallAllowed(receiverType, classNode)
           || StaticInvocationWriter.isPrivateBridgeMethodsCallAllowed(classNode, receiverType))
       && !receiverType.equals(classNode)) {
     Map<String, MethodNode> accessors =
         (Map<String, MethodNode>)
             receiverType
                 .redirect()
                 .getNodeMetaData(StaticCompilationMetadataKeys.PRIVATE_FIELDS_ACCESSORS);
     if (accessors != null) {
       MethodNode methodNode = accessors.get(fieldName);
       if (methodNode != null) {
         MethodCallExpression mce =
             new MethodCallExpression(
                 receiver,
                 methodNode.getName(),
                 new ArgumentListExpression(
                     field.isStatic() ? new ConstantExpression(null) : receiver));
         mce.setMethodTarget(methodNode);
         mce.setSafe(safe);
         mce.setImplicitThis(implicitThis);
         mce.visit(controller.getAcg());
         return true;
       }
     }
   }
   return false;
 }
コード例 #13
0
 /*
  * Constructor used by makeArray() if no real class is available
  */
 private ClassNode(ClassNode componentType) {
   this(componentType.getName() + "[]", ACC_PUBLIC, ClassHelper.OBJECT_TYPE);
   this.componentType = componentType.redirect();
   isPrimaryNode = false;
 }
コード例 #14
0
 /** Returns the ClassNode this ClassNode is redirecting to. */
 public ClassNode redirect() {
   if (redirect == null) return this;
   return redirect.redirect();
 }