Search in sources :

Example 41 with Type

use of java.lang.reflect.Type in project okhttp-OkGo by jeasonlzy.

the class TypeUtils method resolve.

public static Type resolve(Type context, Class<?> contextRawType, Type toResolve) {
    // This implementation is made a little more complicated in an attempt to avoid object-creation.
    while (true) {
        if (toResolve instanceof TypeVariable) {
            TypeVariable<?> typeVariable = (TypeVariable<?>) toResolve;
            toResolve = resolveTypeVariable(context, contextRawType, typeVariable);
            if (toResolve == typeVariable) {
                return toResolve;
            }
        } else if (toResolve instanceof Class && ((Class<?>) toResolve).isArray()) {
            Class<?> original = (Class<?>) toResolve;
            Type componentType = original.getComponentType();
            Type newComponentType = resolve(context, contextRawType, componentType);
            return componentType == newComponentType ? original : new GenericArrayTypeImpl(newComponentType);
        } else if (toResolve instanceof GenericArrayType) {
            GenericArrayType original = (GenericArrayType) toResolve;
            Type componentType = original.getGenericComponentType();
            Type newComponentType = resolve(context, contextRawType, componentType);
            return componentType == newComponentType ? original : new GenericArrayTypeImpl(newComponentType);
        } else if (toResolve instanceof ParameterizedType) {
            ParameterizedType original = (ParameterizedType) toResolve;
            Type ownerType = original.getOwnerType();
            Type newOwnerType = resolve(context, contextRawType, ownerType);
            boolean changed = newOwnerType != ownerType;
            Type[] args = original.getActualTypeArguments();
            for (int t = 0, length = args.length; t < length; t++) {
                Type resolvedTypeArgument = resolve(context, contextRawType, args[t]);
                if (resolvedTypeArgument != args[t]) {
                    if (!changed) {
                        args = args.clone();
                        changed = true;
                    }
                    args[t] = resolvedTypeArgument;
                }
            }
            return changed ? new ParameterizedTypeImpl(newOwnerType, original.getRawType(), args) : original;
        } else if (toResolve instanceof WildcardType) {
            WildcardType original = (WildcardType) toResolve;
            Type[] originalLowerBound = original.getLowerBounds();
            Type[] originalUpperBound = original.getUpperBounds();
            if (originalLowerBound.length == 1) {
                Type lowerBound = resolve(context, contextRawType, originalLowerBound[0]);
                if (lowerBound != originalLowerBound[0]) {
                    return new WildcardTypeImpl(new Type[] { Object.class }, new Type[] { lowerBound });
                }
            } else if (originalUpperBound.length == 1) {
                Type upperBound = resolve(context, contextRawType, originalUpperBound[0]);
                if (upperBound != originalUpperBound[0]) {
                    return new WildcardTypeImpl(new Type[] { upperBound }, EMPTY_TYPE_ARRAY);
                }
            }
            return original;
        } else {
            return toResolve;
        }
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 42 with Type

use of java.lang.reflect.Type in project okhttp-OkGo by jeasonlzy.

the class TypeUtils method getParameterUpperBound.

public static Type getParameterUpperBound(int index, ParameterizedType type) {
    Type[] types = type.getActualTypeArguments();
    if (index < 0 || index >= types.length) {
        throw new IllegalArgumentException("Index " + index + " not in range [0," + types.length + ") for " + type);
    }
    Type paramType = types[index];
    if (paramType instanceof WildcardType) {
        return ((WildcardType) paramType).getUpperBounds()[0];
    }
    return paramType;
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType)

Example 43 with Type

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

the class JaxbStringReaderProviderTest method stringReaderDoesNotReadExternalDtds.

@Test
public void stringReaderDoesNotReadExternalDtds() {
    Provider<SAXParserFactory> saxParserFactoryProvider = new Provider<SAXParserFactory>() {

        final SaxParserFactoryInjectionProvider spf = new SaxParserFactoryInjectionProvider(new CommonConfig(RuntimeType.SERVER, ComponentBag.INCLUDE_ALL));

        @Override
        public SAXParserFactory get() {
            return spf.get();
        }
    };
    JaxbStringReaderProvider.RootElementProvider provider = new JaxbStringReaderProvider.RootElementProvider(saxParserFactoryProvider, new Providers() {

        @Override
        public <T> MessageBodyReader<T> getMessageBodyReader(Class<T> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
            return null;
        }

        @Override
        public <T> MessageBodyWriter<T> getMessageBodyWriter(Class<T> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
            return null;
        }

        @Override
        public <T extends Throwable> ExceptionMapper<T> getExceptionMapper(Class<T> type) {
            return null;
        }

        @Override
        public <T> ContextResolver<T> getContextResolver(Class<T> contextType, MediaType mediaType) {
            return null;
        }
    });
    String content = "<!DOCTYPE x SYSTEM 'file:///no-such-file'> <rootObject/>";
    provider.getConverter(RootObject.class, null, null).fromString(content);
}
Also used : CommonConfig(org.glassfish.jersey.model.internal.CommonConfig) Providers(javax.ws.rs.ext.Providers) Annotation(java.lang.annotation.Annotation) Provider(javax.inject.Provider) MessageBodyReader(javax.ws.rs.ext.MessageBodyReader) RuntimeType(javax.ws.rs.RuntimeType) MediaType(javax.ws.rs.core.MediaType) Type(java.lang.reflect.Type) ExceptionMapper(javax.ws.rs.ext.ExceptionMapper) MediaType(javax.ws.rs.core.MediaType) ContextResolver(javax.ws.rs.ext.ContextResolver) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) SAXParserFactory(javax.xml.parsers.SAXParserFactory) Test(org.junit.Test)

Example 44 with Type

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

the class AbstractCollectionJaxbProvider method verifyGenericType.

/**
     * The method could be used to check if given type is a collection of JAXB beans.
     * It allows customizing the "is this a JAXB bean?" part.
     *
     * @param genericType the type to be checked
     * @param checker     allows JAXB bean check customization
     * @return true if given type is a collection of JAXB beans
     */
public static boolean verifyGenericType(Type genericType, JaxbTypeChecker checker) {
    if (!(genericType instanceof ParameterizedType)) {
        return false;
    }
    final ParameterizedType pt = (ParameterizedType) genericType;
    if (pt.getActualTypeArguments().length > 1) {
        return false;
    }
    final Type ta = pt.getActualTypeArguments()[0];
    if (ta instanceof ParameterizedType) {
        ParameterizedType lpt = (ParameterizedType) ta;
        return (lpt.getRawType() instanceof Class) && JAXBElement.class.isAssignableFrom((Class) lpt.getRawType());
    }
    if (!(pt.getActualTypeArguments()[0] instanceof Class)) {
        return false;
    }
    final Class listClass = (Class) pt.getActualTypeArguments()[0];
    return checker.isJaxbType(listClass);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) MediaType(javax.ws.rs.core.MediaType) XmlType(javax.xml.bind.annotation.XmlType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) JAXBElement(javax.xml.bind.JAXBElement)

Example 45 with Type

use of java.lang.reflect.Type in project storm by apache.

the class RelNodeCompiler method aggregate.

private void aggregate(AggregateCall call) {
    SqlAggFunction aggFunction = call.getAggregation();
    String aggregationName = call.getAggregation().getName();
    Type ty = typeFactory.getJavaClass(call.getType());
    if (call.getArgList().size() != 1) {
        if (aggregationName.equals("COUNT")) {
            if (call.getArgList().size() != 0) {
                throw new UnsupportedOperationException("Count with nullable fields");
            }
        }
    }
    if (aggFunction instanceof SqlUserDefinedAggFunction) {
        AggregateFunction aggregateFunction = ((SqlUserDefinedAggFunction) aggFunction).function;
        doAggregate((AggregateFunctionImpl) aggregateFunction, reserveAggVarName(call), ty, call.getArgList());
    } else {
        List<BuiltinAggregateFunctions.TypeClass> typeClasses = BuiltinAggregateFunctions.TABLE.get(aggregationName);
        if (typeClasses == null) {
            throw new UnsupportedOperationException(aggregationName + " Not implemented");
        }
        doAggregate(AggregateFunctionImpl.create(findMatchingClass(aggregationName, typeClasses, ty)), reserveAggVarName(call), ty, call.getArgList());
    }
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType) Type(java.lang.reflect.Type) AggregateFunction(org.apache.calcite.schema.AggregateFunction) SqlUserDefinedAggFunction(org.apache.calcite.sql.validate.SqlUserDefinedAggFunction) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction)

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