コード例 #1
0
 public static String getGetterWithCastMethod(
     Class<?> methodType, String methodToDecorate, String fieldName) {
   return String.format(
       "  public %s %s() {\n   %s\n}\n",
       filterTypeName(methodType),
       StringTool.getGetterName(fieldName),
       getDecoratorBody(methodType, methodToDecorate, fieldName));
 }
コード例 #2
0
 public static String getPublicGetterMethod(String fieldType, String fieldName) {
   String getterName = StringTool.getGetterName(fieldName);
   if ("getClass".equals(getterName)) {
     return null;
   }
   return String.format(
       "\n  public %s %s() {\n    return %s;\n  }\n", fieldType, getterName, fieldName);
 }
コード例 #3
0
 public static String getHashCodeMethod(Set<String> fields) {
   StringBuilder buf = new StringBuilder();
   buf.append("\n  public int hashCode() {\n");
   buf.append("    HashCodeBuilder builder = new HashCodeBuilder();\n");
   for (String field : fields) {
     String getter = StringTool.getGetterName(field) + "()";
     buf.append(String.format("    builder.append(%s);\n", getter));
   }
   buf.append("    return builder.toHashCode();\n");
   buf.append("  }\n");
   return buf.toString();
 }
コード例 #4
0
 public static String getEqualsMethod(String simpleClassName, Set<String> fields) {
   StringBuilder buf = new StringBuilder();
   buf.append("\n  public boolean equals(Object obj) {\n");
   buf.append("    EqualsBuilder builder = new EqualsBuilder();\n");
   buf.append(String.format("    if (!(obj instanceof %s)) {;\n", simpleClassName));
   buf.append("        return false;\n");
   buf.append("    }\n");
   buf.append(String.format("    %s another = (%s)obj;\n", simpleClassName, simpleClassName));
   for (String field : fields) {
     String getter = StringTool.getGetterName(field) + "()";
     buf.append(String.format("    builder.append(another.%s,%s);\n", getter, getter));
   }
   buf.append("    return builder.isEquals();\n");
   buf.append("  }\n");
   return buf.toString();
 }
コード例 #5
0
 public static String getToStringMethod(String simpleClassName, Map<String, Class<?>> fields) {
   StringBuilder buf = new StringBuilder();
   buf.append("\n  public String toString() {\n");
   buf.append("    StringBuilder builder = new StringBuilder();\n");
   buf.append(String.format("    builder.append(\"%s {\");\n", simpleClassName));
   for (Entry<String, Class<?>> field : fields.entrySet()) {
     buf.append(String.format("    builder.append(\" %s=\");\n", field.getKey()));
     String getter = StringTool.getGetterName(field.getKey()) + "()";
     if (field.getValue().isArray()) {
       buf.append(String.format("    builder.append(ArrayUtils.toString(%s));\n", getter));
     } else {
       buf.append(String.format("    builder.append(%s);\n", getter));
     }
   }
   buf.append("    builder.append(\" }\");\n");
   buf.append("    return builder.toString();\n");
   buf.append("  }\n");
   return buf.toString();
 }
コード例 #6
0
 public static String getPublicSetterMethod(String fieldType, String fieldName) {
   return String.format(
       "\n  public void %s(%s %s) {\n    this.%s = %s;\n  }\n",
       StringTool.getSetterName(fieldName), fieldType, fieldName, fieldName, fieldName);
 }
コード例 #7
0
  public static Object[] readIn(String typeName, Object[][] array) {
    Set<Type> types = ProjectData.getCurrentInstance().getTypes();
    Type type = null;
    for (Type t : types) {
      if (t.getName().equalsIgnoreCase(typeName)) {
        type = t;
        break;
      }
    }

    if (type == null) {
      throw new IllegalArgumentException("Type '" + typeName + "' can't be found");
    }
    if (array.length <= 1) {
      return new Object[0];
    }

    array = transposeIfNeeded(array, type);

    List<Object> objects = new ArrayList<Object>();

    Class clazz;
    Object instance;
    try {
      ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
      clazz = Class.forName("org.openl.generated.beans." + type.getName(), true, classLoader);
    } catch (ClassNotFoundException e) {
      throw new IllegalArgumentException("The type '" + typeName + "' can't be found");
    }

    for (int i = 1; i < array.length; i++) {
      try {
        instance = clazz.newInstance();
      } catch (InstantiationException e) {
        throw new IllegalArgumentException("Can't instantiate the type '" + typeName + "'");
      } catch (IllegalAccessException e) {
        throw new IllegalArgumentException("Can't access the type '" + typeName + "'");
      }
      for (int j = 0; j < array[0].length; j++) {
        FieldImpl field = findField(type, (String) array[0][j]);
        if (field == null) {
          continue;
        }

        String setterName = StringTool.getSetterName(field.getName());
        Method method = null;
        for (Method m : clazz.getMethods()) {
          if (m.getName().equals(setterName)) {
            method = m;
            break;
          }
        }
        if (method == null) {
          throw new IllegalArgumentException(
              "Can't find a setter for the field'"
                  + field.getName()
                  + "' in the type '"
                  + typeName
                  + "'");
        }
        try {
          method.invoke(instance, array[i][j]);
        } catch (IllegalAccessException e) {
          throw new IllegalArgumentException(
              "Can't access the field '" + field.getName() + "' in the type '" + typeName + "'");
        } catch (InvocationTargetException e) {
          throw new IllegalArgumentException(
              "Setter for the field '"
                  + field.getName()
                  + "' in the type '"
                  + typeName
                  + "' throwed an error ",
              e);
        }
      }

      objects.add(instance);
    }

    return objects.toArray((Object[]) Array.newInstance(clazz, objects.size()));
  }