List of usage examples for java.lang.reflect Method getGenericParameterTypes
@Override
public Type[] getGenericParameterTypes()
From source file:org.wso2.msf4j.swagger.ExtendedSwaggerReader.java
private Operation parseMethod(Class<?> cls, Method method, List<Parameter> globalParameters, List<ApiResponse> classApiResponses) { Operation operation = new Operation(); ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class); ApiResponses responseAnnotation = ReflectionUtils.getAnnotation(method, ApiResponses.class); String operationId = method.getName(); String responseContainer = null; Type responseType = null;//from w w w.j ava 2 s .com Map<String, Property> defaultResponseHeaders = new HashMap<>(); if (apiOperation != null) { if (apiOperation.hidden()) { return null; } if (!"".equals(apiOperation.nickname())) { operationId = apiOperation.nickname(); } defaultResponseHeaders = parseResponseHeaders(apiOperation.responseHeaders()); operation.summary(apiOperation.value()).description(apiOperation.notes()); if (apiOperation.response() != null && !isVoid(apiOperation.response())) { responseType = apiOperation.response(); } if (!"".equals(apiOperation.responseContainer())) { responseContainer = apiOperation.responseContainer(); } if (apiOperation.authorizations() != null) { List<SecurityRequirement> securities = new ArrayList<>(); for (Authorization auth : apiOperation.authorizations()) { if (auth.value() != null && !"".equals(auth.value())) { SecurityRequirement security = new SecurityRequirement(); security.setName(auth.value()); AuthorizationScope[] scopes = auth.scopes(); for (AuthorizationScope scope : scopes) { if (scope.scope() != null && !"".equals(scope.scope())) { security.addScope(scope.scope()); } } securities.add(security); } } if (securities.size() > 0) { securities.forEach(operation::security); } } if (apiOperation.consumes() != null && !apiOperation.consumes().isEmpty()) { String[] consumesAr = ReaderUtils.splitContentValues(new String[] { apiOperation.consumes() }); for (String consume : consumesAr) { operation.consumes(consume); } } if (apiOperation.produces() != null && !apiOperation.produces().isEmpty()) { String[] producesAr = ReaderUtils.splitContentValues(new String[] { apiOperation.produces() }); for (String produce : producesAr) { operation.produces(produce); } } } if (apiOperation != null && StringUtils.isNotEmpty(apiOperation.responseReference())) { Response response = new Response().description(SUCCESSFUL_OPERATION); response.schema(new RefProperty(apiOperation.responseReference())); operation.addResponse(String.valueOf(apiOperation.code()), response); } else if (responseType == null) { // pick out response from method declaration LOGGER.debug("picking up response class from method " + method); responseType = method.getGenericReturnType(); } if (isValidResponse(responseType)) { final Property property = ModelConverters.getInstance().readAsProperty(responseType); if (property != null) { final Property responseProperty = ContainerWrapper.wrapContainer(responseContainer, property); final int responseCode = apiOperation == null ? 200 : apiOperation.code(); operation.response(responseCode, new Response().description(SUCCESSFUL_OPERATION) .schema(responseProperty).headers(defaultResponseHeaders)); appendModels(responseType); } } operation.operationId(operationId); if (operation.getConsumes() == null || operation.getConsumes().isEmpty()) { final Consumes consumes = ReflectionUtils.getAnnotation(method, Consumes.class); if (consumes != null) { for (String mediaType : ReaderUtils.splitContentValues(consumes.value())) { operation.consumes(mediaType); } } } if (operation.getProduces() == null || operation.getProduces().isEmpty()) { final Produces produces = ReflectionUtils.getAnnotation(method, Produces.class); if (produces != null) { for (String mediaType : ReaderUtils.splitContentValues(produces.value())) { operation.produces(mediaType); } } } List<ApiResponse> apiResponses = new ArrayList<>(); if (responseAnnotation != null) { apiResponses.addAll(Arrays.asList(responseAnnotation.value())); } Class<?>[] exceptionTypes = method.getExceptionTypes(); for (Class<?> exceptionType : exceptionTypes) { ApiResponses exceptionResponses = ReflectionUtils.getAnnotation(exceptionType, ApiResponses.class); if (exceptionResponses != null) { apiResponses.addAll(Arrays.asList(exceptionResponses.value())); } } for (ApiResponse apiResponse : apiResponses) { addResponse(operation, apiResponse); } // merge class level @ApiResponse for (ApiResponse apiResponse : classApiResponses) { String key = apiResponse.code() == 0 ? "default" : String.valueOf(apiResponse.code()); if (operation.getResponses().containsKey(key)) { continue; } addResponse(operation, apiResponse); } if (ReflectionUtils.getAnnotation(method, Deprecated.class) != null) { operation.setDeprecated(true); } // process parameters globalParameters.forEach(operation::parameter); Type[] genericParameterTypes = method.getGenericParameterTypes(); Annotation[][] paramAnnotations = method.getParameterAnnotations(); for (int i = 0; i < genericParameterTypes.length; i++) { final Type type = TypeFactory.defaultInstance().constructType(genericParameterTypes[i], cls); List<Parameter> parameters = getParameters(type, Arrays.asList(paramAnnotations[i])); parameters.forEach(operation::parameter); } if (operation.getResponses() == null) { Response response = new Response().description(SUCCESSFUL_OPERATION); operation.defaultResponse(response); } return operation; }
From source file:io.swagger.jaxrs.Reader.java
private Operation parseMethod(Class<?> cls, Method method, List<Parameter> globalParameters) { Operation operation = new Operation(); ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class); ApiResponses responseAnnotation = ReflectionUtils.getAnnotation(method, ApiResponses.class); String operationId = method.getName(); String responseContainer = null; Type responseType = null;// w ww .ja va2 s . c o m Map<String, Property> defaultResponseHeaders = new HashMap<String, Property>(); if (apiOperation != null) { if (apiOperation.hidden()) { return null; } if (!"".equals(apiOperation.nickname())) { operationId = apiOperation.nickname(); } defaultResponseHeaders = parseResponseHeaders(apiOperation.responseHeaders()); operation.summary(apiOperation.value()).description(apiOperation.notes()); if (apiOperation.response() != null && !isVoid(apiOperation.response())) { responseType = apiOperation.response(); } if (!"".equals(apiOperation.responseContainer())) { responseContainer = apiOperation.responseContainer(); } if (apiOperation.authorizations() != null) { List<SecurityRequirement> securities = new ArrayList<SecurityRequirement>(); for (Authorization auth : apiOperation.authorizations()) { if (auth.value() != null && !"".equals(auth.value())) { SecurityRequirement security = new SecurityRequirement(); security.setName(auth.value()); AuthorizationScope[] scopes = auth.scopes(); for (AuthorizationScope scope : scopes) { if (scope.scope() != null && !"".equals(scope.scope())) { security.addScope(scope.scope()); } } securities.add(security); } } if (securities.size() > 0) { for (SecurityRequirement sec : securities) { operation.security(sec); } } } if (apiOperation.consumes() != null && !apiOperation.consumes().isEmpty()) { operation.consumes(apiOperation.consumes()); } if (apiOperation.produces() != null && !apiOperation.produces().isEmpty()) { operation.produces(apiOperation.produces()); } } if (apiOperation != null && StringUtils.isNotEmpty(apiOperation.responseReference())) { Response response = new Response().description(SUCCESSFUL_OPERATION); response.schema(new RefProperty(apiOperation.responseReference())); operation.addResponse(String.valueOf(apiOperation.code()), response); } else if (responseType == null) { // pick out response from method declaration LOGGER.debug("picking up response class from method " + method); responseType = method.getGenericReturnType(); } if (isValidResponse(responseType)) { final Property property = ModelConverters.getInstance().readAsProperty(responseType); if (property != null) { final Property responseProperty = ContainerWrapper.wrapContainer(responseContainer, property); final int responseCode = apiOperation == null ? 200 : apiOperation.code(); operation.response(responseCode, new Response().description(SUCCESSFUL_OPERATION) .schema(responseProperty).headers(defaultResponseHeaders)); appendModels(responseType); } } operation.operationId(operationId); if (apiOperation != null && apiOperation.consumes() != null && apiOperation.consumes().isEmpty()) { final Consumes consumes = ReflectionUtils.getAnnotation(method, Consumes.class); if (consumes != null) { for (String mediaType : ReaderUtils.splitContentValues(consumes.value())) { operation.consumes(mediaType); } } } if (apiOperation != null && apiOperation.produces() != null && apiOperation.produces().isEmpty()) { final Produces produces = ReflectionUtils.getAnnotation(method, Produces.class); if (produces != null) { for (String mediaType : ReaderUtils.splitContentValues(produces.value())) { operation.produces(mediaType); } } } List<ApiResponse> apiResponses = new ArrayList<ApiResponse>(); if (responseAnnotation != null) { apiResponses.addAll(Arrays.asList(responseAnnotation.value())); } Class<?>[] exceptionTypes = method.getExceptionTypes(); for (Class<?> exceptionType : exceptionTypes) { ApiResponses exceptionResponses = ReflectionUtils.getAnnotation(exceptionType, ApiResponses.class); if (exceptionResponses != null) { apiResponses.addAll(Arrays.asList(exceptionResponses.value())); } } for (ApiResponse apiResponse : apiResponses) { Map<String, Property> responseHeaders = parseResponseHeaders(apiResponse.responseHeaders()); Response response = new Response().description(apiResponse.message()).headers(responseHeaders); if (apiResponse.code() == 0) { operation.defaultResponse(response); } else { operation.response(apiResponse.code(), response); } if (StringUtils.isNotEmpty(apiResponse.reference())) { response.schema(new RefProperty(apiResponse.reference())); } else if (!isVoid(apiResponse.response())) { responseType = apiResponse.response(); final Property property = ModelConverters.getInstance().readAsProperty(responseType); if (property != null) { response.schema(ContainerWrapper.wrapContainer(apiResponse.responseContainer(), property)); appendModels(responseType); } } } if (ReflectionUtils.getAnnotation(method, Deprecated.class) != null) { operation.setDeprecated(true); } // process parameters for (Parameter globalParameter : globalParameters) { operation.parameter(globalParameter); } Type[] genericParameterTypes = method.getGenericParameterTypes(); Annotation[][] paramAnnotations = method.getParameterAnnotations(); for (int i = 0; i < genericParameterTypes.length; i++) { final Type type = TypeFactory.defaultInstance().constructType(genericParameterTypes[i], cls); List<Parameter> parameters = getParameters(type, Arrays.asList(paramAnnotations[i])); for (Parameter parameter : parameters) { operation.parameter(parameter); } } if (operation.getResponses() == null) { Response response = new Response().description(SUCCESSFUL_OPERATION); operation.defaultResponse(response); } return operation; }
From source file:com.evolveum.midpoint.prism.parser.PrismBeanConverter.java
public <T> T unmarshall(MapXNode xnode, Class<T> beanClass) throws SchemaException { if (PolyStringType.class.equals(beanClass)) { PolyString polyString = unmarshalPolyString(xnode); return (T) polyString; // violates the method interface but ... TODO fix it } else if (ProtectedStringType.class.equals(beanClass)) { ProtectedStringType protectedType = new ProtectedStringType(); XNodeProcessorUtil.parseProtectedType(protectedType, xnode, prismContext); return (T) protectedType; } else if (ProtectedByteArrayType.class.equals(beanClass)) { ProtectedByteArrayType protectedType = new ProtectedByteArrayType(); XNodeProcessorUtil.parseProtectedType(protectedType, xnode, prismContext); return (T) protectedType; } else if (SchemaDefinitionType.class.equals(beanClass)) { SchemaDefinitionType schemaDefType = unmarshalSchemaDefinitionType(xnode); return (T) schemaDefType; } else if (prismContext.getSchemaRegistry().determineDefinitionFromClass(beanClass) != null) { return (T) prismContext.getXnodeProcessor().parseObject(xnode).asObjectable(); } else if (XmlAsStringType.class.equals(beanClass)) { // reading a string represented a XML-style content // used e.g. when reading report templates (embedded XML) // A necessary condition: there may be only one map entry. if (xnode.size() > 1) { throw new SchemaException("Map with more than one item cannot be parsed as a string: " + xnode); } else if (xnode.isEmpty()) { return (T) new XmlAsStringType(); } else {/*from ww w .ja v a2s . c o m*/ Map.Entry<QName, XNode> entry = xnode.entrySet().iterator().next(); DomParser domParser = prismContext.getParserDom(); String value = domParser.serializeToString(entry.getValue(), entry.getKey()); return (T) new XmlAsStringType(value); } } T bean; Set<String> keysToParse; // only these keys will be parsed (null if all) if (SearchFilterType.class.isAssignableFrom(beanClass)) { keysToParse = Collections.singleton("condition"); // TODO fix this BRUTAL HACK - it is here because of c:ConditionalSearchFilterType bean = (T) unmarshalSearchFilterType(xnode, (Class<? extends SearchFilterType>) beanClass); } else { keysToParse = null; try { bean = beanClass.newInstance(); } catch (InstantiationException e) { throw new SystemException("Cannot instantiate bean of type " + beanClass + ": " + e.getMessage(), e); } catch (IllegalAccessException e) { throw new SystemException("Cannot instantiate bean of type " + beanClass + ": " + e.getMessage(), e); } } if (ProtectedDataType.class.isAssignableFrom(beanClass)) { ProtectedDataType protectedDataType = null; if (bean instanceof ProtectedStringType) { protectedDataType = new ProtectedStringType(); } else if (bean instanceof ProtectedByteArrayType) { protectedDataType = new ProtectedByteArrayType(); } else { throw new SchemaException("Unexpected subtype of protected data type: " + bean.getClass()); } XNodeProcessorUtil.parseProtectedType(protectedDataType, xnode, prismContext); return (T) protectedDataType; } for (Entry<QName, XNode> entry : xnode.entrySet()) { QName key = entry.getKey(); if (keysToParse != null && !keysToParse.contains(key.getLocalPart())) { continue; } XNode xsubnode = entry.getValue(); String propName = key.getLocalPart(); Field field = inspector.findPropertyField(beanClass, propName); Method propertyGetter = null; if (field == null) { propertyGetter = inspector.findPropertyGetter(beanClass, propName); } Method elementMethod = null; Object objectFactory = null; if (field == null && propertyGetter == null) { // We have to try to find a more generic field, such as xsd:any or substitution element // check for global element definition first Class objectFactoryClass = inspector.getObjectFactoryClass(beanClass.getPackage()); objectFactory = instantiateObjectFactory(objectFactoryClass); elementMethod = inspector.findElementMethodInObjectFactory(objectFactoryClass, propName); if (elementMethod == null) { // Check for "any" method elementMethod = inspector.findAnyMethod(beanClass); if (elementMethod == null) { String m = "No field " + propName + " in class " + beanClass + " (and no element method in object factory too)"; if (mode == XNodeProcessorEvaluationMode.COMPAT) { LOGGER.warn("{}", m); continue; } else { throw new SchemaException(m); } } unmarshallToAny(bean, elementMethod, key, xsubnode); continue; } field = inspector.lookupSubstitution(beanClass, elementMethod); if (field == null) { // Check for "any" field field = inspector.findAnyField(beanClass); if (field == null) { elementMethod = inspector.findAnyMethod(beanClass); if (elementMethod == null) { String m = "No field " + propName + " in class " + beanClass + " (and no element method in object factory too)"; if (mode == XNodeProcessorEvaluationMode.COMPAT) { LOGGER.warn("{}", m); continue; } else { throw new SchemaException(m); } } unmarshallToAny(bean, elementMethod, key, xsubnode); continue; // throw new SchemaException("No field "+propName+" in class "+beanClass+" (no suitable substitution and no 'any' field)"); } unmarshallToAny(bean, field, key, xsubnode); continue; } } boolean storeAsRawType; if (elementMethod != null) { storeAsRawType = elementMethod.getAnnotation(Raw.class) != null; } else if (propertyGetter != null) { storeAsRawType = propertyGetter.getAnnotation(Raw.class) != null; } else { storeAsRawType = field.getAnnotation(Raw.class) != null; } String fieldName; if (field != null) { fieldName = field.getName(); } else { fieldName = propName; } Method setter = inspector.findSetter(beanClass, fieldName); Method getter = null; boolean wrapInJaxbElement = false; Class<?> paramType = null; if (setter == null) { // No setter. But if the property is multi-value we need to look // for a getter that returns a collection (Collection<Whatever>) getter = inspector.findPropertyGetter(beanClass, fieldName); if (getter == null) { String m = "Cannot find setter or getter for field " + fieldName + " in " + beanClass; if (mode == XNodeProcessorEvaluationMode.COMPAT) { LOGGER.warn("{}", m); continue; } else { throw new SchemaException(m); } } Class<?> getterReturnType = getter.getReturnType(); if (!Collection.class.isAssignableFrom(getterReturnType)) { throw new SchemaException("Cannot find getter for field " + fieldName + " in " + beanClass + " does not return collection, cannot use it to set value"); } Type genericReturnType = getter.getGenericReturnType(); Type typeArgument = getTypeArgument(genericReturnType, "for field " + fieldName + " in " + beanClass + ", cannot determine collection type"); // System.out.println("type argument " + typeArgument); if (typeArgument instanceof Class) { paramType = (Class<?>) typeArgument; } else if (typeArgument instanceof ParameterizedType) { ParameterizedType paramTypeArgument = (ParameterizedType) typeArgument; Type rawTypeArgument = paramTypeArgument.getRawType(); if (rawTypeArgument.equals(JAXBElement.class)) { // This is the case of Collection<JAXBElement<....>> wrapInJaxbElement = true; Type innerTypeArgument = getTypeArgument(typeArgument, "for field " + fieldName + " in " + beanClass + ", cannot determine collection type (inner type argument)"); if (innerTypeArgument instanceof Class) { // This is the case of Collection<JAXBElement<Whatever>> paramType = (Class<?>) innerTypeArgument; } else if (innerTypeArgument instanceof WildcardType) { // This is the case of Collection<JAXBElement<?>> // we need to exctract the specific type from the factory method if (elementMethod == null) { // TODO: TEMPORARY CODE!!!!!!!!!! fix in 3.1 [med] Class objectFactoryClass = inspector.getObjectFactoryClass(beanClass.getPackage()); objectFactory = instantiateObjectFactory(objectFactoryClass); elementMethod = inspector.findElementMethodInObjectFactory(objectFactoryClass, propName); if (elementMethod == null) { throw new IllegalArgumentException( "Wildcard type in JAXBElement field specification and no factory method found for field " + fieldName + " in " + beanClass + ", cannot determine collection type (inner type argument)"); } } Type factoryMethodGenericReturnType = elementMethod.getGenericReturnType(); Type factoryMethodTypeArgument = getTypeArgument(factoryMethodGenericReturnType, "in factory method " + elementMethod + " return type for field " + fieldName + " in " + beanClass + ", cannot determine collection type"); if (factoryMethodTypeArgument instanceof Class) { // This is the case of JAXBElement<Whatever> paramType = (Class<?>) factoryMethodTypeArgument; if (Object.class.equals(paramType) && !storeAsRawType) { throw new IllegalArgumentException("Factory method " + elementMethod + " type argument is Object (and not @Raw) for field " + fieldName + " in " + beanClass + ", property " + propName); } } else { throw new IllegalArgumentException( "Cannot determine factory method return type, got " + factoryMethodTypeArgument + " - for field " + fieldName + " in " + beanClass + ", cannot determine collection type (inner type argument)"); } } else { throw new IllegalArgumentException("Ejha! " + innerTypeArgument + " " + innerTypeArgument.getClass() + " from " + getterReturnType + " from " + fieldName + " in " + propName + " " + beanClass); } } else { // The case of Collection<Whatever<Something>> if (rawTypeArgument instanceof Class) { paramType = (Class<?>) rawTypeArgument; } else { throw new IllegalArgumentException("EH? Eh!? " + typeArgument + " " + typeArgument.getClass() + " from " + getterReturnType + " from " + fieldName + " in " + propName + " " + beanClass); } } } else { throw new IllegalArgumentException( "EH? " + typeArgument + " " + typeArgument.getClass() + " from " + getterReturnType + " from " + fieldName + " in " + propName + " " + beanClass); } } else { Class<?> setterType = setter.getParameterTypes()[0]; if (JAXBElement.class.equals(setterType)) { // TODO some handling for the returned generic parameter types Type[] genericTypes = setter.getGenericParameterTypes(); if (genericTypes.length != 1) { throw new IllegalArgumentException("Too lazy to handle this."); } Type genericType = genericTypes[0]; if (genericType instanceof ParameterizedType) { Type actualType = getTypeArgument(genericType, "add some description"); if (actualType instanceof WildcardType) { if (elementMethod == null) { Class objectFactoryClass = inspector.getObjectFactoryClass(beanClass.getPackage()); objectFactory = instantiateObjectFactory(objectFactoryClass); elementMethod = inspector.findElementMethodInObjectFactory(objectFactoryClass, propName); } // This is the case of Collection<JAXBElement<?>> // we need to exctract the specific type from the factory method if (elementMethod == null) { throw new IllegalArgumentException( "Wildcard type in JAXBElement field specification and no facotry method found for field " + fieldName + " in " + beanClass + ", cannot determine collection type (inner type argument)"); } Type factoryMethodGenericReturnType = elementMethod.getGenericReturnType(); Type factoryMethodTypeArgument = getTypeArgument(factoryMethodGenericReturnType, "in factory method " + elementMethod + " return type for field " + fieldName + " in " + beanClass + ", cannot determine collection type"); if (factoryMethodTypeArgument instanceof Class) { // This is the case of JAXBElement<Whatever> paramType = (Class<?>) factoryMethodTypeArgument; if (Object.class.equals(paramType) && !storeAsRawType) { throw new IllegalArgumentException("Factory method " + elementMethod + " type argument is Object (without @Raw) for field " + fieldName + " in " + beanClass + ", property " + propName); } } else { throw new IllegalArgumentException( "Cannot determine factory method return type, got " + factoryMethodTypeArgument + " - for field " + fieldName + " in " + beanClass + ", cannot determine collection type (inner type argument)"); } } } // Class enclosing = paramType.getEnclosingClass(); // Class clazz = paramType.getClass(); // Class declaring = paramType.getDeclaringClass(); wrapInJaxbElement = true; } else { paramType = setterType; } } if (Element.class.isAssignableFrom(paramType)) { // DOM! throw new IllegalArgumentException("DOM not supported in field " + fieldName + " in " + beanClass); } //check for subclasses??? if (!storeAsRawType && xsubnode.getTypeQName() != null) { Class explicitParamType = getSchemaRegistry().determineCompileTimeClass(xsubnode.getTypeQName()); if (explicitParamType == null) { explicitParamType = XsdTypeMapper.toJavaTypeIfKnown(xsubnode.getTypeQName()); } if (explicitParamType != null) { paramType = explicitParamType; } } if (!(xsubnode instanceof ListXNode) && Object.class.equals(paramType) && !storeAsRawType) { throw new IllegalArgumentException( "Object property (without @Raw) not supported in field " + fieldName + " in " + beanClass); } String paramNamespace = inspector.determineNamespace(paramType); boolean problem = false; Object propValue = null; Collection<Object> propValues = null; if (xsubnode instanceof ListXNode) { ListXNode xlist = (ListXNode) xsubnode; if (setter != null) { try { propValue = convertSinglePropValue(xsubnode, fieldName, paramType, storeAsRawType, beanClass, paramNamespace); } catch (SchemaException e) { problem = processSchemaException(e, xsubnode); } } else { // No setter, we have to use collection getter propValues = new ArrayList<>(xlist.size()); for (XNode xsubsubnode : xlist) { try { propValues.add(convertSinglePropValue(xsubsubnode, fieldName, paramType, storeAsRawType, beanClass, paramNamespace)); } catch (SchemaException e) { problem = processSchemaException(e, xsubsubnode); } } } } else { try { propValue = convertSinglePropValue(xsubnode, fieldName, paramType, storeAsRawType, beanClass, paramNamespace); } catch (SchemaException e) { problem = processSchemaException(e, xsubnode); } } if (setter != null) { try { setter.invoke(bean, prepareValueToBeStored(propValue, wrapInJaxbElement, objectFactory, elementMethod, propName, beanClass)); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { throw new SystemException("Cannot invoke setter " + setter + " on bean of type " + beanClass + ": " + e.getMessage(), e); } } else if (getter != null) { Object getterReturn; Collection<Object> col; try { getterReturn = getter.invoke(bean); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { throw new SystemException("Cannot invoke getter " + getter + " on bean of type " + beanClass + ": " + e.getMessage(), e); } try { col = (Collection<Object>) getterReturn; } catch (ClassCastException e) { throw new SystemException("Getter " + getter + " on bean of type " + beanClass + " returned " + getterReturn + " instead of collection"); } if (propValue != null) { col.add(prepareValueToBeStored(propValue, wrapInJaxbElement, objectFactory, elementMethod, propName, beanClass)); } else if (propValues != null) { for (Object propVal : propValues) { col.add(prepareValueToBeStored(propVal, wrapInJaxbElement, objectFactory, elementMethod, propName, beanClass)); } } else if (!problem) { throw new IllegalStateException("Strange. Multival property " + propName + " in " + beanClass + " produced null values list, parsed from " + xnode); } checkJaxbElementConsistence(col); } else { throw new IllegalStateException("Uh? No setter nor getter."); } } if (prismContext != null && bean instanceof Revivable) { ((Revivable) bean).revive(prismContext); } return bean; }
From source file:io.sinistral.proteus.server.tools.swagger.Reader.java
@SuppressWarnings("deprecation") private Operation parseMethod(Class<?> cls, Method method, AnnotatedMethod annotatedMethod, List<Parameter> globalParameters, List<ApiResponse> classApiResponses, List<String> pathParamNames) { Operation operation = new Operation(); if (annotatedMethod != null) { method = annotatedMethod.getAnnotated(); }// w w w .ja v a2 s . c o m ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class); ApiResponses responseAnnotation = ReflectionUtils.getAnnotation(method, ApiResponses.class); String operationId; // check if it's an inherited or implemented method. boolean methodInSuperType = false; if (!cls.isInterface()) { methodInSuperType = ReflectionUtils.findMethod(method, cls.getSuperclass()) != null; } if (!methodInSuperType) { for (Class<?> implementedInterface : cls.getInterfaces()) { methodInSuperType = ReflectionUtils.findMethod(method, implementedInterface) != null; if (methodInSuperType) { break; } } } if (!methodInSuperType) { operationId = method.getName(); } else { operationId = this.getOperationId(method.getName()); } String responseContainer = null; Type responseType = null; Map<String, Property> defaultResponseHeaders = new LinkedHashMap<String, Property>(); if (apiOperation != null) { if (apiOperation.hidden()) { return null; } if (operationId == null) { operationId = apiOperation.nickname(); } defaultResponseHeaders = parseResponseHeaders(apiOperation.responseHeaders()); operation.summary(apiOperation.value()).description(apiOperation.notes()); if (!isVoid(apiOperation.response())) { responseType = apiOperation.response(); } if (!apiOperation.responseContainer().isEmpty()) { responseContainer = apiOperation.responseContainer(); } List<SecurityRequirement> securities = new ArrayList<SecurityRequirement>(); for (Authorization auth : apiOperation.authorizations()) { if (!auth.value().isEmpty()) { SecurityRequirement security = new SecurityRequirement(); security.setName(auth.value()); for (AuthorizationScope scope : auth.scopes()) { if (!scope.scope().isEmpty()) { security.addScope(scope.scope()); } } securities.add(security); } } for (SecurityRequirement sec : securities) { operation.security(sec); } if (!apiOperation.consumes().isEmpty()) { String[] consumesAr = ReaderUtils.splitContentValues(new String[] { apiOperation.consumes() }); for (String consume : consumesAr) { operation.consumes(consume); } } if (!apiOperation.produces().isEmpty()) { String[] producesAr = ReaderUtils.splitContentValues(new String[] { apiOperation.produces() }); for (String produce : producesAr) { operation.produces(produce); } } } /* * @TODO * Use apiOperation response class instead of unwrapping ServerResponse's inner type */ if (apiOperation != null && StringUtils.isNotEmpty(apiOperation.responseReference())) { Response response = new Response().description(SUCCESSFUL_OPERATION); response.schema(new RefProperty(apiOperation.responseReference())); operation.addResponse(String.valueOf(apiOperation.code()), response); } else if (responseType == null) { // pick out response from method declaration LOGGER.debug("picking up response class from method {}", method); responseType = method.getGenericReturnType(); } if (responseType != null) { final JavaType javaType = TypeFactory.defaultInstance().constructType(responseType); if (!isVoid(javaType)) { final Class<?> responseCls = javaType.getRawClass(); if (responseCls != null) { if (responseCls.isAssignableFrom(ServerResponse.class)) { responseType = javaType.containedType(0); } else if (responseCls.isAssignableFrom(CompletableFuture.class)) { Class<?> futureCls = javaType.containedType(0).getRawClass(); if (futureCls.isAssignableFrom(ServerResponse.class)) { final JavaType futureType = TypeFactory.defaultInstance() .constructType(javaType.containedType(0)); responseType = futureType.containedType(0); } else { responseType = javaType.containedType(0); } } } } } if (isValidResponse(responseType)) { final Property property = ModelConverters.getInstance().readAsProperty(responseType); if (property != null) { final Property responseProperty = ContainerWrapper.wrapContainer(responseContainer, property); final int responseCode = (apiOperation == null) ? 200 : apiOperation.code(); operation.response(responseCode, new Response().description(SUCCESSFUL_OPERATION) .schema(responseProperty).headers(defaultResponseHeaders)); appendModels(responseType); } } operation.operationId(operationId); if (operation.getConsumes() == null || operation.getConsumes().isEmpty()) { final Consumes consumes = ReflectionUtils.getAnnotation(method, Consumes.class); if (consumes != null) { for (String mediaType : ReaderUtils.splitContentValues(consumes.value())) { operation.consumes(mediaType); } } } if (operation.getProduces() == null || operation.getProduces().isEmpty()) { final Produces produces = ReflectionUtils.getAnnotation(method, Produces.class); if (produces != null) { for (String mediaType : ReaderUtils.splitContentValues(produces.value())) { operation.produces(mediaType); } } } List<ApiResponse> apiResponses = new ArrayList<>(); if (responseAnnotation != null) { apiResponses.addAll(Arrays.asList(responseAnnotation.value())); } Class<?>[] exceptionTypes = method.getExceptionTypes(); for (Class<?> exceptionType : exceptionTypes) { ApiResponses exceptionResponses = ReflectionUtils.getAnnotation(exceptionType, ApiResponses.class); if (exceptionResponses != null) { apiResponses.addAll(Arrays.asList(exceptionResponses.value())); } } for (ApiResponse apiResponse : apiResponses) { addResponse(operation, apiResponse); } // merge class level @ApiResponse for (ApiResponse apiResponse : classApiResponses) { String key = (apiResponse.code() == 0) ? "default" : String.valueOf(apiResponse.code()); if (operation.getResponses() != null && operation.getResponses().containsKey(key)) { continue; } addResponse(operation, apiResponse); } if (ReflectionUtils.getAnnotation(method, Deprecated.class) != null) { operation.setDeprecated(true); } // process parameters for (Parameter globalParameter : globalParameters) { LOGGER.debug("globalParameters TYPE: " + globalParameter); operation.parameter(globalParameter); } Annotation[][] paramAnnotations = ReflectionUtils.getParameterAnnotations(method); java.lang.reflect.Parameter[] methodParameters = method.getParameters(); if (annotatedMethod == null) { Type[] genericParameterTypes = method.getGenericParameterTypes(); for (int i = 0; i < genericParameterTypes.length; i++) { Type type = TypeFactory.defaultInstance().constructType(genericParameterTypes[i], cls); if (type.getTypeName().contains("Optional") || type.getTypeName().contains("io.sinistral.proteus.server.ServerResponse")) { if (type instanceof com.fasterxml.jackson.databind.type.SimpleType) { com.fasterxml.jackson.databind.type.SimpleType simpleType = (com.fasterxml.jackson.databind.type.SimpleType) type; type = simpleType.containedType(0); } } if (type.equals(ServerRequest.class) || type.equals(HttpServerExchange.class) || type.equals(HttpHandler.class) || type.getTypeName().contains("io.sinistral.proteus.server.ServerResponse")) { continue; } List<Parameter> parameters = getParameters(type, Arrays.asList(paramAnnotations[i]), methodParameters[i], pathParamNames); for (Parameter parameter : parameters) { operation.parameter(parameter); } } } else { for (int i = 0; i < annotatedMethod.getParameterCount(); i++) { AnnotatedParameter param = annotatedMethod.getParameter(i); if (param.getParameterType().equals(ServerRequest.class) || param.getParameterType().equals(HttpServerExchange.class) || param.getParameterType().equals(HttpHandler.class) || param.getParameterType().getTypeName().contains("ServerResponse")) { continue; } Type type = TypeFactory.defaultInstance().constructType(param.getParameterType(), cls); List<Parameter> parameters = getParameters(type, Arrays.asList(paramAnnotations[i]), methodParameters[i], pathParamNames); for (Parameter parameter : parameters) { operation.parameter(parameter); } } } if (operation.getResponses() == null) { Response response = new Response().description(SUCCESSFUL_OPERATION); operation.response(200, response); } processOperationDecorator(operation, method); return operation; }