Search in sources :

Example 66 with Type

use of java.lang.reflect.Type in project buck by facebook.

the class TypeCoercerTest method coerceToEitherLeftOrRight.

@Test
public void coerceToEitherLeftOrRight() throws NoSuchFieldException, CoerceFailedException {
    Type type = TestFields.class.getField("eitherStringSetOrStringToStringMap").getGenericType();
    TypeCoercer<?> coercer = typeCoercerFactory.typeCoercerForType(type);
    Set<String> inputSet = ImmutableSet.of("a", "b", "x");
    Map<String, String> inputMap = ImmutableMap.of("key1", "One", "key2", "Two");
    assertEquals(Either.ofLeft(inputSet), coercer.coerce(cellRoots, filesystem, Paths.get(""), inputSet));
    assertEquals(Either.ofRight(inputMap), coercer.coerce(cellRoots, filesystem, Paths.get(""), inputMap));
}
Also used : Type(java.lang.reflect.Type) Test(org.junit.Test)

Example 67 with Type

use of java.lang.reflect.Type in project neo4j by neo4j.

the class MethodSignatureCompiler method inputTypesFor.

public List<Neo4jTypes.AnyType> inputTypesFor(Method method) throws ProcedureException {
    Type[] types = method.getGenericParameterTypes();
    List<Neo4jTypes.AnyType> neoTypes = new ArrayList<>(types.length);
    for (Type type : types) {
        NeoValueConverter valueConverter = typeMappers.converterFor(type);
        neoTypes.add(valueConverter.type());
    }
    return neoTypes;
}
Also used : Type(java.lang.reflect.Type) NeoValueConverter(org.neo4j.kernel.impl.proc.TypeMappers.NeoValueConverter) ArrayList(java.util.ArrayList)

Example 68 with Type

use of java.lang.reflect.Type in project neo4j by neo4j.

the class MethodSignatureCompiler method signatureFor.

public List<FieldSignature> signatureFor(Method method) throws ProcedureException {
    Parameter[] params = method.getParameters();
    Type[] types = method.getGenericParameterTypes();
    List<FieldSignature> signature = new ArrayList<>(params.length);
    boolean seenDefault = false;
    for (int i = 0; i < params.length; i++) {
        Parameter param = params[i];
        Type type = types[i];
        if (!param.isAnnotationPresent(Name.class)) {
            throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Argument at position %d in method `%s` is missing an `@%s` annotation.%n" + "Please add the annotation, recompile the class and try again.", i, method.getName(), Name.class.getSimpleName());
        }
        Name parameter = param.getAnnotation(Name.class);
        String name = parameter.value();
        if (name.trim().length() == 0) {
            throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Argument at position %d in method `%s` is annotated with a name,%n" + "but the name is empty, please provide a non-empty name for the argument.", i, method.getName());
        }
        try {
            NeoValueConverter valueConverter = typeMappers.converterFor(type);
            Optional<Neo4jValue> defaultValue = valueConverter.defaultValue(parameter);
            //it is not allowed to have holes in default values
            if (seenDefault && !defaultValue.isPresent()) {
                throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Non-default argument at position %d with name %s in method %s follows default argument. " + "Add a default value or rearrange arguments so that the non-default values comes first.", i, parameter.value(), method.getName());
            }
            seenDefault = defaultValue.isPresent();
            signature.add(new FieldSignature(name, valueConverter.type(), defaultValue));
        } catch (ProcedureException e) {
            throw new ProcedureException(e.status(), "Argument `%s` at position %d in `%s` with%n" + "type `%s` cannot be converted to a Neo4j type: %s", name, i, method.getName(), param.getType().getSimpleName(), e.getMessage());
        }
    }
    return signature;
}
Also used : NeoValueConverter(org.neo4j.kernel.impl.proc.TypeMappers.NeoValueConverter) ArrayList(java.util.ArrayList) FieldSignature(org.neo4j.kernel.api.proc.FieldSignature) Name(org.neo4j.procedure.Name) Type(java.lang.reflect.Type) Parameter(java.lang.reflect.Parameter) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException)

Example 69 with Type

use of java.lang.reflect.Type in project neo4j by neo4j.

the class TypeMappers method converterFor.

public NeoValueConverter converterFor(Type javaType) throws ProcedureException {
    NeoValueConverter converter = javaToNeo.get(javaType);
    if (converter != null) {
        return converter;
    }
    if (javaType instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) javaType;
        Type rawType = pt.getRawType();
        if (rawType == List.class) {
            Type type = pt.getActualTypeArguments()[0];
            return toList(converterFor(type), type);
        } else if (rawType == Map.class) {
            Type type = pt.getActualTypeArguments()[0];
            if (type != String.class) {
                throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "Maps are required to have `String` keys - but this map has `%s` keys.", type.getTypeName());
            }
            return TO_MAP;
        }
    }
    throw javaToNeoMappingError(javaType);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) AnyType(org.neo4j.kernel.api.proc.Neo4jTypes.AnyType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) NTString(org.neo4j.kernel.api.proc.Neo4jTypes.NTString) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException) HashMap(java.util.HashMap) Map(java.util.Map) NTMap(org.neo4j.kernel.api.proc.Neo4jTypes.NTMap)

Example 70 with Type

use of java.lang.reflect.Type in project mybatis-3 by mybatis.

the class Reflector method addGetMethod.

private void addGetMethod(String name, Method method) {
    if (isValidPropertyName(name)) {
        getMethods.put(name, new MethodInvoker(method));
        Type returnType = TypeParameterResolver.resolveReturnType(method, type);
        getTypes.put(name, typeToClass(returnType));
    }
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) MethodInvoker(org.apache.ibatis.reflection.invoker.MethodInvoker)

Aggregations

Type (java.lang.reflect.Type)6423 ParameterizedType (java.lang.reflect.ParameterizedType)1761 ProgressRequestBody (io.kubernetes.client.ProgressRequestBody)722 ProgressResponseBody (io.kubernetes.client.ProgressResponseBody)722 GenericArrayType (java.lang.reflect.GenericArrayType)690 WildcardType (java.lang.reflect.WildcardType)571 Test (org.junit.Test)512 ArrayList (java.util.ArrayList)427 Method (java.lang.reflect.Method)416 TypeVariable (java.lang.reflect.TypeVariable)337 List (java.util.List)335 Map (java.util.Map)289 Gson (com.google.gson.Gson)231 V1Status (io.kubernetes.client.models.V1Status)228 V1Status (io.kubernetes.client.openapi.models.V1Status)224 HashMap (java.util.HashMap)204 Field (java.lang.reflect.Field)163 Annotation (java.lang.annotation.Annotation)160 IOException (java.io.IOException)140 Collection (java.util.Collection)111