public MethodMetadata getMethod(final JavaSymbolName methodName) {
   for (final MemberHoldingTypeDetails memberHoldingTypeDetails : details) {
     final MethodMetadata md =
         MemberFindingUtils.getDeclaredMethod(memberHoldingTypeDetails, methodName);
     if (md != null) {
       return md;
     }
   }
   return null;
 }
 public MethodMetadata getMethod(
     final JavaSymbolName methodName, final List<JavaType> parameters, final String excludingMid) {
   for (final MemberHoldingTypeDetails memberHoldingTypeDetails : details) {
     final MethodMetadata method =
         MemberFindingUtils.getDeclaredMethod(memberHoldingTypeDetails, methodName, parameters);
     if (method != null && !method.getDeclaredByMetadataId().equals(excludingMid)) {
       return method;
     }
   }
   return null;
 }
Пример #3
0
  private MethodMetadata getToJsonMethod() {
    // Compute the relevant method name
    JavaSymbolName methodName = getToJsonMethodName();
    if (methodName == null) {
      return null;
    }

    // See if the type itself declared the method
    MethodMetadata result =
        MemberFindingUtils.getDeclaredMethod(governorTypeDetails, methodName, null);
    if (result != null) {
      return result;
    }

    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    String serializer =
        new JavaType("flexjson.JSONSerializer")
            .getNameIncludingTypeParameters(false, builder.getImportRegistrationResolver());
    String root =
        annotationValues.getRootName() != null && annotationValues.getRootName().length() > 0
            ? ".rootName(\"" + annotationValues.getRootName() + "\")"
            : "";
    bodyBuilder.appendFormalLine(
        "return new "
            + serializer
            + "()"
            + root
            + ".exclude(\"*.class\")"
            + (annotationValues.isDeepSerialize() ? ".deepSerialize(this)" : ".serialize(this)")
            + ";");

    MethodMetadataBuilder methodBuilder =
        new MethodMetadataBuilder(
            getId(), Modifier.PUBLIC, methodName, new JavaType("java.lang.String"), bodyBuilder);
    methodBuilder.putCustomData(CustomDataJsonTags.TO_JSON_METHOD, null);
    return methodBuilder.build();
  }
  /**
   * Obtains the "toString" method for this type, if available.
   *
   * <p>If the user provided a non-default name for "toString", that method will be returned.
   *
   * @return the "toString" method declared on this type or that will be introduced (or null if
   *     undeclared and not introduced)
   */
  public MethodMetadata getToStringMethod() {
    // Compute the relevant toString method name
    JavaSymbolName methodName = new JavaSymbolName("toString");
    if (!this.toStringMethod.equals("")) {
      methodName = new JavaSymbolName(this.toStringMethod);
    }

    // See if the type itself declared the method
    MethodMetadata result =
        MemberFindingUtils.getDeclaredMethod(governorTypeDetails, methodName, null);
    if (result != null) {
      return result;
    }

    // Decide whether we need to produce the toString method
    if (this.toStringMethod.equals("")) {
      return null;
    }

    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    bodyBuilder.appendFormalLine("StringBuilder sb = new StringBuilder();");

    /** Key: field name, Value: accessor name */
    Map<String, String> map = new LinkedHashMap<String, String>();

    /** Field names */
    List<String> order = new ArrayList<String>();

    Set<String> excludeFieldsSet = new LinkedHashSet<String>();
    if (excludeFields != null && excludeFields.length > 0) {
      Collections.addAll(excludeFieldsSet, excludeFields);
    }

    for (MethodMetadata accessor : locatedAccessors) {
      String accessorName = accessor.getMethodName().getSymbolName();
      String fieldName = BeanInfoUtils.getPropertyNameForJavaBeanMethod(accessor).getSymbolName();
      if (!excludeFieldsSet.contains(StringUtils.uncapitalize(fieldName))
          && !map.containsKey(fieldName)) {
        String accessorText = accessorName + "()";
        if (accessor.getReturnType().isCommonCollectionType()) {
          accessorText = accessorName + "() == null ? \"null\" : " + accessorName + "().size()";
        } else if (accessor.getReturnType().isArray()) {
          accessorText = "java.util.Arrays.toString(" + accessorName + "())";
        } else if (Calendar.class
            .getName()
            .equals(accessor.getReturnType().getFullyQualifiedTypeName())) {
          accessorText = accessorName + "() == null ? \"null\" : " + accessorName + "().getTime()";
        }
        map.put(fieldName, accessorText);
        order.add(fieldName);
      }
    }

    if (!order.isEmpty()) {
      int index = 0;
      int size = map.keySet().size();
      for (String fieldName : order) {
        index++;
        String accessorText = map.get(fieldName);
        StringBuilder string = new StringBuilder();
        string
            .append("sb.append(\"")
            .append(fieldName)
            .append(": \").append(")
            .append(accessorText)
            .append(")");
        if (index < size) {
          string.append(".append(\", \")");
        }
        string.append(";");
        bodyBuilder.appendFormalLine(string.toString());
      }

      bodyBuilder.appendFormalLine("return sb.toString();");

      MethodMetadataBuilder methodBuilder =
          new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, STRING, bodyBuilder);
      result = methodBuilder.build();
    }

    return result;
  }