Search in sources :

Example 16 with Type

use of java.lang.reflect.Type in project che by eclipse.

the class DtoModel method analyzeDtoGetterMethod.

/**
     * Populate model from given reflect getter method
     * @param method the method to analyze
     * @param methodModel the model to update
     */
protected void analyzeDtoGetterMethod(Method method, MethodModel methodModel) {
    methodModel.setGetter(true);
    Type fieldType = method.getGenericReturnType();
    String fieldName = getGetterFieldName(method);
    fieldAttributes.put(fieldName, fieldType);
    methodModel.setFieldName(fieldName);
    methodModel.setFieldType(convertType(fieldType));
}
Also used : DTOHelper.convertType(org.eclipse.che.plugin.typescript.dto.DTOHelper.convertType) Type(java.lang.reflect.Type)

Example 17 with Type

use of java.lang.reflect.Type in project che by eclipse.

the class DtoImplClientTemplate method emitDeserializerImpl.

/**
     * Produces code to deserialize the type with the given variable names.
     *
     * @param expandedTypes
     *         the type and its generic (and its generic (..)) expanded into a list, @see {@link #expandType(java.lang.reflect.Type)}
     * @param depth
     *         the depth (in the generics) for this recursive call. This can be used to index into {@code expandedTypes}
     * @param builder
     *         StringBuilder to add generated code for deserialization
     * @param inVar
     *         the java type that will be the input for deserialization
     * @param outVar
     *         the JsonElement subtype that will be the output for serialization
     * @param i
     *         indentation string
     */
private void emitDeserializerImpl(List<Type> expandedTypes, int depth, StringBuilder builder, String inVar, String outVar, String i) {
    Type type = expandedTypes.get(depth);
    String childInVar = inVar + "_";
    String childInVarIterator = childInVar + "_iterator";
    String childOutVar = outVar + "_";
    Class<?> rawClass = getRawClass(type);
    if (isList(rawClass)) {
        builder.append(i).append(getImplName(type, false)).append(" ").append(outVar).append(" = null;\n");
        builder.append(i).append("if (").append(inVar).append(" != null && ").append(inVar).append(".isNull() == null) {\n");
        builder.append(i).append("  ").append(outVar).append(" = new ").append(getImplName(type, true)).append("();\n");
        builder.append(i).append("  for (int ").append(childInVarIterator).append(" = 0; ").append(childInVarIterator).append(" < ").append(inVar).append(".isArray().size(); ").append(childInVarIterator).append("++) {\n");
        builder.append(i).append("    JSONValue ").append(childInVar).append(" = ").append(inVar).append(".isArray().get(").append(childInVarIterator).append(");\n");
        emitDeserializerImpl(expandedTypes, depth + 1, builder, childInVar, childOutVar, i + "    ");
        builder.append(i).append("    ").append(outVar).append(".add(").append(childOutVar).append(");\n");
        builder.append(i).append("  }\n");
        builder.append(i).append("}\n");
    } else if (isMap(rawClass)) {
        // TODO: Handle type
        String entryVar = "key" + depth;
        String entriesVar = "keySet" + depth;
        builder.append(i).append(getImplName(type, false)).append(" ").append(outVar).append(" = null;\n");
        builder.append(i).append("if (").append(inVar).append(" != null && ").append(inVar).append(".isNull() == null) {\n");
        builder.append(i).append("  ").append(outVar).append(" = new ").append(getImplName(type, true)).append("();\n");
        builder.append(i).append("  java.util.Set<String> ").append(entriesVar).append(" = ").append(inVar).append(".isObject().keySet();\n");
        builder.append(i).append("  for (String ").append(entryVar).append(" : ").append(entriesVar).append(") {\n");
        builder.append(i).append("    JSONValue ").append(childInVar).append(" = ").append(inVar).append(".isObject().get(").append(entryVar).append(");\n");
        emitDeserializerImpl(expandedTypes, depth + 1, builder, childInVar, childOutVar, i + "    ");
        builder.append(i).append("    ").append(outVar).append(".put(").append(entryVar).append(", ").append(childOutVar).append(");\n");
        builder.append(i).append("  }\n");
        builder.append(i).append("}\n");
    } else if (rawClass.isEnum()) {
        String primitiveName = rawClass.getCanonicalName();
        builder.append(i).append(primitiveName).append(" ").append(outVar).append(" = ").append(primitiveName).append(".valueOf(").append(inVar).append(".isString().stringValue());\n");
    } else if (getEnclosingTemplate().isDtoInterface(rawClass)) {
        String className = getImplName(rawClass, false);
        builder.append(i).append(className).append(" ").append(outVar).append(" = ").append(getImplNameForDto(rawClass)).append(".fromJsonObject(").append(inVar).append(");\n");
    } else if (rawClass.equals(String.class)) {
        String primitiveName = rawClass.getSimpleName();
        builder.append(i).append(primitiveName).append(" ").append(outVar).append(" = ").append(inVar).append(".isString() != null ? ").append(inVar).append(".isString().stringValue() : null;\n");
    } else if (isNumber(rawClass)) {
        String primitiveName = rawClass.getSimpleName();
        String typeCast = rawClass.equals(double.class) || rawClass.equals(Double.class) ? "" : "(" + getPrimitiveName(rawClass) + ")";
        if (rawClass.isPrimitive()) {
            builder.append(i).append(primitiveName).append(" ").append(outVar).append(" = ").append(typeCast).append(inVar).append(".isNumber().doubleValue();\n");
        } else {
            if (isInteger(rawClass)) {
                builder.append(i).append(primitiveName).append(" ").append(outVar).append(" = ").append(inVar).append(".isNumber() != null ? ((Double)").append(inVar).append(".isNumber().doubleValue()).intValue() : null;\n");
            } else if (isFloat(rawClass)) {
                builder.append(i).append(primitiveName).append(" ").append(outVar).append(" = ").append(inVar).append(".isNumber() != null ? ((Double)").append(inVar).append(".isNumber().doubleValue()).floatValue() : null;\n");
            } else if (isLong(rawClass)) {
                builder.append(i).append(primitiveName).append(" ").append(outVar).append(" = ").append(inVar).append(".isNumber() != null ? ((Double)").append(inVar).append(".isNumber().doubleValue()).longValue() : null;\n");
            } else if (isDouble(rawClass)) {
                builder.append(i).append(primitiveName).append(" ").append(outVar).append(" = ").append(inVar).append(".isNumber() != null ? ((Double)").append(inVar).append(".isNumber().doubleValue()).doubleValue() : null;\n");
            }
        }
    } else if (isBoolean(rawClass)) {
        String primitiveName = rawClass.getSimpleName();
        if (rawClass.isPrimitive()) {
            builder.append(i).append(primitiveName).append(" ").append(outVar).append(" = ").append(inVar).append(".isBoolean().booleanValue();\n");
        } else {
            builder.append(i).append(primitiveName).append(" ").append(outVar).append(" = ").append(inVar).append(".isBoolean() != null ? ").append(inVar).append(".isBoolean().booleanValue() : null;\n");
        }
    } else if (isAny(rawClass)) {
        // TODO JsonElement.deepCopy() is package-protected, JSONs are serialized to strings then parsed for copying them
        // outVar = copyJsons ? new JsonParser().parse(inVar) : inVar;
        builder.append(i).append("JSONValue ").append(outVar).append(" = ");
        appendCopyJsonExpression(inVar, builder).append(";\n");
    } else {
        final Class<?> dtoImplementation = getEnclosingTemplate().getDtoImplementation(rawClass);
        if (dtoImplementation != null) {
            String className = getImplName(rawClass, false);
            builder.append(i).append(className).append(" ").append(outVar).append(" = ").append(dtoImplementation.getCanonicalName()).append(".fromJsonObject(").append(inVar).append(");\n");
        } else {
            throw new IllegalArgumentException("Unable to generate client implementation for DTO interface " + getDtoInterface().getCanonicalName() + ". Type " + rawClass + " is not allowed to use in DTO interface.");
        }
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type)

Example 18 with Type

use of java.lang.reflect.Type in project che by eclipse.

the class DtoImplClientTemplate method emitDeserializeFieldForMethodCompact.

private void emitDeserializeFieldForMethodCompact(Method method, final StringBuilder builder) {
    final String fieldName = getFieldNameFromGetterName(method.getName());
    final String fieldNameIn = fieldName + "In";
    final String fieldNameOut = fieldName + "Out";
    final String baseIndentation = "        ";
    SerializationIndex serializationIndex = Preconditions.checkNotNull(method.getAnnotation(SerializationIndex.class));
    int index = serializationIndex.value() - 1;
    builder.append("\n");
    builder.append("      if (").append(index).append(" < json.size()) {\n");
    List<Type> expandedTypes = expandType(method.getGenericReturnType());
    builder.append("        JSONValue ").append(fieldNameIn).append(" = json.get(").append(index).append(");\n");
    emitDeserializerImpl(expandedTypes, 0, builder, fieldNameIn, fieldNameOut, baseIndentation);
    builder.append("        dto.").append(getSetterName(fieldName)).append("(").append(fieldNameOut).append(");\n");
    builder.append("      }\n");
}
Also used : SerializationIndex(org.eclipse.che.dto.shared.SerializationIndex) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type)

Example 19 with Type

use of java.lang.reflect.Type in project che by eclipse.

the class DtoImplClientTemplate method emitDeepCopyCollections.

private void emitDeepCopyCollections(List<Type> expandedTypes, int depth, StringBuilder builder, String varIn, String varOut, String i) {
    Type type = expandedTypes.get(depth);
    String childVarIn = varIn + "_";
    String childVarOut = varOut + "_";
    String entryVar = "entry" + depth;
    Class<?> rawClass = getRawClass(type);
    Class<?> childRawType = getRawClass(expandedTypes.get(depth + 1));
    final String childTypeName = getImplName(expandedTypes.get(depth + 1), false);
    if (isList(rawClass)) {
        builder.append(i).append("  for (").append(childTypeName).append(" ").append(childVarIn).append(" : ").append(varIn).append(") {\n");
    } else if (isMap(rawClass)) {
        builder.append(i).append("  for (java.util.Map.Entry<String, ").append(childTypeName).append("> ").append(entryVar).append(" : ").append(varIn).append(".entrySet()) {\n");
        builder.append(i).append("    ").append(childTypeName).append(" ").append(childVarIn).append(" = ").append(entryVar).append(".getValue();\n");
    }
    if (isList(childRawType) || isMap(childRawType)) {
        builder.append(i).append("    if (").append(childVarIn).append(" != null) {\n");
        builder.append(i).append("      ").append(childTypeName).append(" ").append(childVarOut).append(" = new ").append(getImplName(expandedTypes.get(depth + 1), true)).append("();\n");
        emitDeepCopyCollections(expandedTypes, depth + 1, builder, childVarIn, childVarOut, i + "    ");
        builder.append(i).append("      ").append(varOut);
        if (isList(rawClass)) {
            builder.append(".add(");
        } else {
            builder.append(".put(").append(entryVar).append(".getKey(), ");
        }
        builder.append(childVarOut);
        builder.append(");\n");
        builder.append(i).append("    ").append("}\n");
    } else {
        builder.append(i).append("      ").append(varOut);
        if (isList(rawClass)) {
            builder.append(".add(");
        } else {
            builder.append(".put(").append(entryVar).append(".getKey(), ");
        }
        if (getEnclosingTemplate().isDtoInterface(childRawType)) {
            emitCheckNullAndCopyDto(childRawType, childVarIn, builder);
        } else {
            builder.append(childVarIn);
        }
        builder.append(");\n");
    }
    builder.append(i).append("  }\n");
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type)

Example 20 with Type

use of java.lang.reflect.Type in project che by eclipse.

the class DtoImplClientTemplate method emitSerializerImpl.

/**
     * Produces code to serialize the type with the given variable names.
     *
     * @param expandedTypes
     *         the type and its generic (and its generic (..)) expanded into a list, @see {@link #expandType(java.lang.reflect.Type)}
     * @param depth
     *         the depth (in the generics) for this recursive call. This can be used to index into {@code expandedTypes}
     * @param builder
     *         StringBuilder to add generated code for serialization
     * @param inVar
     *         the java type that will be the input for serialization
     * @param outVar
     *         the JsonElement subtype that will be the output for serialization
     * @param i
     *         indentation string
     */
private void emitSerializerImpl(List<Type> expandedTypes, int depth, StringBuilder builder, String inVar, String outVar, String i) {
    Type type = expandedTypes.get(depth);
    String childInVar = inVar + "_";
    String childOutVar = outVar + "_";
    String entryVar = "entry" + depth;
    Class<?> rawClass = getRawClass(type);
    if (isList(rawClass)) {
        String childInTypeName = getImplName(expandedTypes.get(depth + 1), false);
        builder.append(i).append("JSONArray ").append(outVar).append(" = new JSONArray();\n");
        if (depth == 0) {
            builder.append(i).append("this.").append(getEnsureName(inVar)).append("();\n");
        }
        builder.append(i).append("for (").append(childInTypeName).append(" ").append(childInVar).append(" : ").append(depth == 0 ? "this." + inVar : inVar).append(") {\n");
    } else if (isMap(rawClass)) {
        String childInTypeName = getImplName(expandedTypes.get(depth + 1), false);
        builder.append(i).append("JSONObject ").append(outVar).append(" = new JSONObject();\n");
        if (depth == 0) {
            builder.append(i).append("this.").append(getEnsureName(inVar)).append("();\n");
        }
        builder.append(i).append("for (java.util.Map.Entry<String, ").append(childInTypeName).append("> ").append(entryVar).append(" : ").append(depth == 0 ? "this." + inVar : inVar).append(".entrySet()) {\n");
        builder.append(i).append("  ").append(childInTypeName).append(" ").append(childInVar).append(" = ").append(entryVar).append(".getValue();\n");
    } else if (rawClass.isEnum()) {
        builder.append(i).append("JSONValue ").append(outVar).append(" = (").append(depth == 0 ? "this." + inVar : inVar).append(" == null) ? JSONNull.getInstance() : new JSONString(").append(depth == 0 ? "this." + inVar : inVar).append(".name());\n");
    } else if (getEnclosingTemplate().isDtoInterface(rawClass)) {
        builder.append(i).append("JSONValue ").append(outVar).append(" = ").append(depth == 0 ? "this." + inVar : inVar).append(" == null ? JSONNull.getInstance() : ((").append(getImplNameForDto((Class<?>) expandedTypes.get(depth))).append(")").append(inVar).append(").toJsonObject();\n");
    } else if (rawClass.equals(String.class)) {
        builder.append(i).append("JSONValue ").append(outVar).append(" = (").append(depth == 0 ? "this." + inVar : inVar).append(" == null) ? JSONNull.getInstance() : new JSONString(").append(depth == 0 ? "this." + inVar : inVar).append(");\n");
    } else if (isNumber(rawClass)) {
        if (rawClass.isPrimitive()) {
            builder.append(i).append("JSONValue ").append(outVar).append(" = new JSONNumber(").append(depth == 0 ? "this." + inVar : inVar).append(");\n");
        } else {
            builder.append(i).append("JSONValue ").append(outVar).append(" = ").append(depth == 0 ? " this." + inVar : inVar).append(" == null ? JSONNull.getInstance() : new JSONNumber(").append(depth == 0 ? "this." + inVar : inVar).append(");\n");
        }
    } else if (isBoolean(rawClass)) {
        if (rawClass.isPrimitive()) {
            builder.append(i).append("JSONValue ").append(outVar).append(" = JSONBoolean.getInstance(").append(depth == 0 ? "this." + inVar : inVar).append(");\n");
        } else {
            builder.append(i).append("JSONValue ").append(outVar).append(" = ").append(depth == 0 ? " this." + inVar : inVar).append(" == null ? JSONNull.getInstance() : JSONBoolean.getInstance(").append(depth == 0 ? "this." + inVar : inVar).append(");\n");
        }
    } else if (isAny(rawClass)) {
        // TODO a better method to clone instances of 
        // outVar = inVar == null ? JsonNull.INSTNACE : (copyJsons ? new JsonParser().parse(inVar) : inVar);
        builder.append(i).append("JSONValue ").append(outVar).append(" = ").append(depth == 0 ? " this." + inVar : inVar).append(" == null ? JSONNull.getInstance() : (");
        appendCopyJsonExpression(inVar, builder).append(");\n");
    } else {
        final Class<?> dtoImplementation = getEnclosingTemplate().getDtoImplementation(rawClass);
        if (dtoImplementation != null) {
            builder.append(i).append("JSONValue ").append(outVar).append(" = ").append(depth == 0 ? "this." + inVar : inVar).append(" == null ? JSONNull.getInstance() : ((").append(dtoImplementation.getCanonicalName()).append(")").append(depth == 0 ? "this." + inVar : inVar).append(").toJsonObject();\n");
        } else {
            throw new IllegalArgumentException("Unable to generate client implementation for DTO interface " + getDtoInterface().getCanonicalName() + ". Type " + rawClass + " is not allowed to use in DTO interface.");
        }
    }
    if (depth + 1 < expandedTypes.size()) {
        emitSerializerImpl(expandedTypes, depth + 1, builder, childInVar, childOutVar, i + "  ");
    }
    if (isList(rawClass)) {
        builder.append(i).append("  ").append(outVar).append(".set(").append(outVar).append(".size(), ").append(childOutVar).append(");\n");
        builder.append(i).append("}\n");
    } else if (isMap(rawClass)) {
        builder.append(i).append("  ").append(outVar).append(".put(").append(entryVar).append(".getKey(), ").append(childOutVar).append(");\n");
        builder.append(i).append("}\n");
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type)

Aggregations

Type (java.lang.reflect.Type)6477 ParameterizedType (java.lang.reflect.ParameterizedType)1798 ProgressRequestBody (io.kubernetes.client.ProgressRequestBody)722 ProgressResponseBody (io.kubernetes.client.ProgressResponseBody)722 GenericArrayType (java.lang.reflect.GenericArrayType)711 WildcardType (java.lang.reflect.WildcardType)580 Test (org.junit.Test)513 ArrayList (java.util.ArrayList)429 Method (java.lang.reflect.Method)424 TypeVariable (java.lang.reflect.TypeVariable)350 List (java.util.List)342 Map (java.util.Map)296 Gson (com.google.gson.Gson)230 V1Status (io.kubernetes.client.models.V1Status)228 V1Status (io.kubernetes.client.openapi.models.V1Status)224 HashMap (java.util.HashMap)211 Field (java.lang.reflect.Field)170 Annotation (java.lang.annotation.Annotation)161 IOException (java.io.IOException)146 Collection (java.util.Collection)116