Java Code Examples for org.apache.commons.lang3.reflect.TypeUtils#getRawType()

The following examples show how to use org.apache.commons.lang3.reflect.TypeUtils#getRawType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpringSwaggerExtension.java    From swagger-maven-plugin with Apache License 2.0 6 votes vote down vote up
@Override
public boolean shouldIgnoreType(Type type, Set<Type> typesToSkip) {
    Class<?> clazz = TypeUtils.getRawType(type, type);
    if (clazz == null) {
        return false;
    }

    String clazzName = clazz.getName();

    switch (clazzName) {
        case "javax.servlet.ServletRequest":
        case "javax.servlet.ServletResponse":
        case "javax.servlet.http.HttpSession":
        case "javax.servlet.http.PushBuilder":
        case "java.security.Principal":
        case "java.io.OutputStream":
        case "java.io.Writer":
            return true;
        default:
    }

    return clazzName.startsWith("org.springframework") &&
            !"org.springframework.web.multipart.MultipartFile".equals(clazzName);
}
 
Example 2
Source File: BeanParamInjectParamExtension.java    From swagger-maven-plugin with Apache License 2.0 6 votes vote down vote up
@Override
public List<Parameter> extractParameters(List<Annotation> annotations, Type type, Set<Type> typesToSkip, Iterator<SwaggerExtension> chain) {
    Class<?> cls = TypeUtils.getRawType(type, type);

    if (shouldIgnoreClass(cls) || typesToSkip.contains(type)) {
        // stop the processing chain
        typesToSkip.add(type);
        return Lists.newArrayList();
    }
    for (Annotation annotation : annotations) {
        if (annotation instanceof BeanParam || annotation instanceof InjectParam) {
            return reader.extractTypes(cls, typesToSkip, Lists.newArrayList());
        }
    }
    return super.extractParameters(annotations, type, typesToSkip, chain);
}
 
Example 3
Source File: DefaultArgumentValueResolver.java    From yare with MIT License 5 votes vote down vote up
@Override
public Object resolve(VariableResolver variableResolver, Argument argument) {
    if (argument instanceof Argument.Value) {
        return ((Argument.Value) argument).getValue();
    }
    if (argument instanceof Argument.Values) {
        return ((Argument.Values) argument).getArguments().stream()
                .map(a -> resolve(variableResolver, a))
                .collect(Collectors.toList());
    }
    if (argument instanceof Argument.Reference) {
        if (!(variableResolver instanceof PredicateContext)) {
            throw new IllegalArgumentException("Expected PredicateContext as VariableResolver");
        }
        Argument.Reference reference = (Argument.Reference) argument;
        String path = reference.getReference();
        int dotIndex = path.indexOf(".");
        if (dotIndex == -1) {
            return variableResolver.resolve(path);
        }
        String identifier = path.substring(0, dotIndex);
        path = path.substring(dotIndex + 1);
        Object resolvedValue = variableResolver.resolve(identifier);
        Class referenceType = TypeUtils.getRawType(reference.getReferenceType(), null);
        referenceType = resolvedValue != null && Object.class.equals(referenceType)
                ? resolvedValue.getClass()
                : referenceType;
        return FieldReferringClassFactory.create(referenceType, identifier, path).get((PredicateContext) variableResolver);
    }
    if (argument instanceof Argument.Invocation) {
        if (!(variableResolver instanceof ProcessingContext)) {
            throw new IllegalArgumentException("Expected ProcessingContext as VariableResolver");
        }
        ProcessingContext processingContext = (ProcessingContext) variableResolver;
        Invocation<ProcessingContext, Object> value = processingInvocationFactory.create((Argument.Invocation) argument);
        return value.proceed(processingContext);
    }

    throw new IllegalArgumentException(String.format("Unsupported argument type %s", argument.getClass()));
}
 
Example 4
Source File: AbstractReader.java    From swagger-maven-plugin with Apache License 2.0 5 votes vote down vote up
protected List<Parameter> getParameters(Type type, List<Annotation> annotations, Set<Type> typesToSkip) {
    if (!hasValidAnnotations(annotations) || isApiParamHidden(annotations)) {
        return Collections.emptyList();
    }

    Iterator<SwaggerExtension> chain = SwaggerExtensions.chain();
    List<Parameter> parameters = new ArrayList<>();
    Class<?> cls = TypeUtils.getRawType(type, type);
    LOG.debug("Looking for path/query/header/form/cookie params in " + cls);

    if (chain.hasNext()) {
        SwaggerExtension extension = chain.next();
        LOG.debug("trying extension " + extension);
        parameters = extension.extractParameters(annotations, type, typesToSkip, chain);
    }

    if (!parameters.isEmpty()) {
        for (Parameter parameter : parameters) {
            ParameterProcessor.applyAnnotations(swagger, parameter, type, annotations);
        }
    } else {
        LOG.debug("Looking for body params in " + cls);
        // parameters is guaranteed to be empty at this point, replace it with a mutable collection
        parameters = Lists.newArrayList();
        if (!typesToSkip.contains(type)) {
            Parameter param = ParameterProcessor.applyAnnotations(swagger, null, type, annotations);
            if (param != null) {
                parameters.add(param);
            }
        }
    }
    return parameters;
}
 
Example 5
Source File: SpringSwaggerExtension.java    From swagger-maven-plugin with Apache License 2.0 4 votes vote down vote up
private List<Parameter> extractParametersFromModelAttributeAnnotation(Type type, Map<Class<?>, Annotation> annotations) {
    ModelAttribute modelAttribute = (ModelAttribute)annotations.get(ModelAttribute.class);
    if ((modelAttribute == null || !hasClassStartingWith(annotations.keySet(), "org.springframework.web.bind.annotation"))&& BeanUtils.isSimpleProperty(TypeUtils.getRawType(type, null))) {
        return Collections.emptyList();
    }

    List<Parameter> parameters = new ArrayList<Parameter>();
    Class<?> clazz = TypeUtils.getRawType(type, type);
    for (PropertyDescriptor propertyDescriptor : BeanUtils.getPropertyDescriptors(clazz)) {
        // Get all the valid setter methods inside the bean
        Method propertyDescriptorSetter = propertyDescriptor.getWriteMethod();
        if (propertyDescriptorSetter != null) {
            ApiParam propertySetterApiParam = AnnotationUtils.findAnnotation(propertyDescriptorSetter, ApiParam.class);
            if (propertySetterApiParam == null) {
                // If we find a setter that doesn't have @ApiParam annotation, then skip it
                continue;
            }

            // Here we have a bean setter method that is annotted with @ApiParam, but we still
            // need to know what type of parameter to create. In order to do this, we look for
            // any annotation attached to the first method parameter of the setter fucntion.
            Annotation[][] parameterAnnotations = propertyDescriptorSetter.getParameterAnnotations();
            if (parameterAnnotations == null || parameterAnnotations.length == 0) {
                continue;
            }

            Class parameterClass = propertyDescriptor.getPropertyType();
            List<Parameter> propertySetterExtractedParameters = this.extractParametersFromAnnotation(
                    parameterClass, toMap(Arrays.asList(parameterAnnotations[0])));

            for (Parameter parameter : propertySetterExtractedParameters) {
                if (Strings.isNullOrEmpty(parameter.getName())) {
                    parameter.setName(propertyDescriptor.getDisplayName());
                }
                ParameterProcessor.applyAnnotations(new Swagger(), parameter, type, Lists.newArrayList(propertySetterApiParam));
            }
            parameters.addAll(propertySetterExtractedParameters);
        }
    }

    return parameters;
}