Example #1
0
 private static JMethod createEmptyMethodFromExample(
     JDeclaredType inType, JMethod exampleMethod, boolean isAbstract) {
   JMethod emptyMethod =
       new JMethod(
           exampleMethod.getSourceInfo(),
           exampleMethod.getName(),
           inType,
           exampleMethod.getType(),
           isAbstract,
           false,
           false,
           exampleMethod.getAccess());
   emptyMethod.addThrownExceptions(exampleMethod.getThrownExceptions());
   emptyMethod.setSynthetic();
   // Copy parameters.
   for (JParameter param : exampleMethod.getParams()) {
     emptyMethod.addParam(
         new JParameter(
             param.getSourceInfo(),
             param.getName(),
             param.getType(),
             param.isFinal(),
             param.isThis(),
             emptyMethod));
   }
   JMethodBody body = new JMethodBody(exampleMethod.getSourceInfo());
   emptyMethod.setBody(body);
   emptyMethod.freezeParamTypes();
   inType.addMethod(emptyMethod);
   return emptyMethod;
 }
Example #2
0
  /**
   * Creates a synthetic forwarding stub in {@code type} with the same signature as {@code
   * superTypeMethod} that dispatchs to that method..
   */
  public static JMethod createForwardingMethod(JDeclaredType type, JMethod methodToDelegateTo) {
    JMethod forwardingMethod = createEmptyMethodFromExample(type, methodToDelegateTo, false);
    forwardingMethod.setForwarding();

    // This is a synthetic forwading method due to a default.
    if (methodToDelegateTo.isDefaultMethod()) {
      forwardingMethod.setDefaultMethod();
    }

    // Create the forwarding body.
    JMethodBody body = (JMethodBody) forwardingMethod.getBody();
    // Invoke methodToDelegate
    JMethodCall forwardingCall =
        new JMethodCall(
            methodToDelegateTo.getSourceInfo(),
            new JThisRef(methodToDelegateTo.getSourceInfo(), type),
            methodToDelegateTo);
    forwardingCall.setStaticDispatchOnly();
    // copy params
    for (JParameter p : forwardingMethod.getParams()) {
      forwardingCall.addArg(new JParameterRef(p.getSourceInfo(), p));
    }

    // return statement if not void return type
    body.getBlock().addStmt(makeMethodEndStatement(forwardingMethod.getType(), forwardingCall));
    return forwardingMethod;
  }
Example #3
0
  /** Returns a description for a member suitable for reporting errors to the users. */
  public static String getReadableDescription(JMember member) {
    if (member instanceof JField) {
      return String.format(
          "%s %s.%s",
          getReadableDescription(member.getType()),
          getReadableDescription(member.getEnclosingType()),
          member.getName());
    }

    JMethod method = (JMethod) member;
    String printableDescription = "";
    if (!method.isConstructor()) {
      printableDescription += getReadableDescription(method.getType()) + " ";
    }
    printableDescription +=
        String.format(
            "%s.%s(%s)",
            getReadableDescription(method.getEnclosingType()),
            method.getName(),
            Joiner.on(", ")
                .join(
                    Iterables.transform(
                        method.getOriginalParamTypes(),
                        new Function<JType, String>() {
                          @Override
                          public String apply(JType type) {
                            return getReadableDescription(type);
                          }
                        })));
    return printableDescription;
  }
Example #4
0
 @Override
 public void exit(JMethod x, Context ctx) {
   JType type = x.getType();
   if (type instanceof JReferenceType
       && !program.typeOracle.isInstantiatedType((JReferenceType) type)) {
     x.setType(JReferenceType.NULL_TYPE);
   }
   Predicate<JMethod> isPruned =
       new Predicate<JMethod>() {
         @Override
         public boolean apply(JMethod method) {
           return isPruned(method);
         }
       };
   Iterables.removeIf(x.getOverriddenMethods(), isPruned);
   Iterables.removeIf(x.getOverridingMethods(), isPruned);
 }