Skip to content

Commit

Permalink
Consistent instanceof/casting of Class references
Browse files Browse the repository at this point in the history
(cherry picked from commit ac80ac6)
  • Loading branch information
jhoeller committed Oct 31, 2016
1 parent 7e80d2d commit bf0df54
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 38 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -458,7 +458,7 @@ protected GroovyBeanDefinitionReader invokeBeanDefiningClosure(Closure callable)
private GroovyBeanDefinitionWrapper invokeBeanDefiningMethod(String beanName, Object[] args) {
boolean hasClosureArgument = args[args.length - 1] instanceof Closure;
if (args[0] instanceof Class) {
Class<?> beanClass = (args[0] instanceof Class ? (Class) args[0] : args[0].getClass());
Class<?> beanClass = (args[0] instanceof Class ? (Class<?>) args[0] : args[0].getClass());
if (args.length >= 1) {
if (hasClosureArgument) {
if (args.length-1 != 1) {
Expand Down
Expand Up @@ -37,24 +37,17 @@
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.Aware;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.parsing.Location;
import org.springframework.beans.factory.parsing.Problem;
import org.springframework.beans.factory.parsing.ProblemReporter;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ConfigurationCondition.ConfigurationPhase;
import org.springframework.core.NestedIOException;
import org.springframework.core.annotation.AnnotationAttributes;
Expand Down Expand Up @@ -724,7 +717,7 @@ private class SourceClass {

public SourceClass(Object source) {
this.source = source;
if (source instanceof Class<?>) {
if (source instanceof Class) {
this.metadata = new StandardAnnotationMetadata((Class<?>) source, true);
}
else {
Expand All @@ -737,7 +730,7 @@ public final AnnotationMetadata getMetadata() {
}

public Class<?> loadClass() throws ClassNotFoundException {
if (this.source instanceof Class<?>) {
if (this.source instanceof Class) {
return (Class<?>) this.source;
}
String className = ((MetadataReader) this.source).getClassMetadata().getClassName();
Expand All @@ -752,15 +745,15 @@ public boolean isAssignable(Class<?> clazz) throws IOException {
}

public ConfigurationClass asConfigClass(ConfigurationClass importedBy) throws IOException {
if (this.source instanceof Class<?>) {
if (this.source instanceof Class) {
return new ConfigurationClass((Class<?>) this.source, importedBy);
}
return new ConfigurationClass((MetadataReader) this.source, importedBy);
}

public Collection<SourceClass> getMemberClasses() throws IOException {
Object sourceToProcess = this.source;
if (sourceToProcess instanceof Class<?>) {
if (sourceToProcess instanceof Class) {
Class<?> sourceClass = (Class<?>) sourceToProcess;
try {
Class<?>[] declaredClasses = sourceClass.getDeclaredClasses();
Expand Down Expand Up @@ -797,15 +790,15 @@ public Collection<SourceClass> getMemberClasses() throws IOException {
}

public SourceClass getSuperClass() throws IOException {
if (this.source instanceof Class<?>) {
if (this.source instanceof Class) {
return asSourceClass(((Class<?>) this.source).getSuperclass());
}
return asSourceClass(((MetadataReader) this.source).getClassMetadata().getSuperClassName());
}

public Set<SourceClass> getInterfaces() throws IOException {
Set<SourceClass> result = new LinkedHashSet<SourceClass>();
if (this.source instanceof Class<?>) {
if (this.source instanceof Class) {
Class<?> sourceClass = (Class<?>) this.source;
for (Class<?> ifcClass : sourceClass.getInterfaces()) {
result.add(asSourceClass(ifcClass));
Expand Down Expand Up @@ -847,7 +840,7 @@ public Collection<SourceClass> getAnnotationAttributes(String annotationType, St
}

private SourceClass getRelated(String className) throws IOException {
if (this.source instanceof Class<?>) {
if (this.source instanceof Class) {
try {
Class<?> clazz = ((Class<?>) this.source).getClassLoader().loadClass(className);
return asSourceClass(clazz);
Expand Down
Expand Up @@ -516,7 +516,8 @@ public static <A extends Annotation> A findAnnotation(AnnotatedElement annotated
* @since 4.2
*/
@SuppressWarnings("unchecked")
private static <A extends Annotation> A findAnnotation(AnnotatedElement annotatedElement, Class<A> annotationType, Set<Annotation> visited) {
private static <A extends Annotation> A findAnnotation(
AnnotatedElement annotatedElement, Class<A> annotationType, Set<Annotation> visited) {
try {
Annotation[] anns = annotatedElement.getDeclaredAnnotations();
for (Annotation ann : anns) {
Expand Down Expand Up @@ -1108,10 +1109,10 @@ static Object adaptValue(Object annotatedElement, Object value, boolean classVal
boolean nestedAnnotationsAsMap) {

if (classValuesAsString) {
if (value instanceof Class<?>) {
if (value instanceof Class) {
return ((Class<?>) value).getName();
}
else if (value instanceof Class<?>[]) {
else if (value instanceof Class[]) {
Class<?>[] clazzArray = (Class<?>[]) value;
String[] classNames = new String[clazzArray.length];
for (int i = 0; i < clazzArray.length; i++) {
Expand Down
Expand Up @@ -80,10 +80,10 @@ else if (value instanceof Type[]) {
value = convArray;
}
else if (classValuesAsString) {
if (value instanceof Class<?>) {
if (value instanceof Class) {
value = ((Class<?>) value).getName();
}
else if (value instanceof Class<?>[]) {
else if (value instanceof Class[]) {
Class<?>[] clazzArray = (Class<?>[]) value;
String[] newValue = new String[clazzArray.length];
for (int i = 0; i < clazzArray.length; i++) {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -48,19 +48,19 @@ public static boolean isAssignable(Type lhsType, Type rhsType) {
return true;
}

if (lhsType instanceof Class<?>) {
if (lhsType instanceof Class) {
Class<?> lhsClass = (Class<?>) lhsType;

// just comparing two classes
if (rhsType instanceof Class<?>) {
if (rhsType instanceof Class) {
return ClassUtils.isAssignable(lhsClass, (Class<?>) rhsType);
}

if (rhsType instanceof ParameterizedType) {
Type rhsRaw = ((ParameterizedType) rhsType).getRawType();

// a parameterized type is always assignable to its raw class type
if (rhsRaw instanceof Class<?>) {
if (rhsRaw instanceof Class) {
return ClassUtils.isAssignable(lhsClass, (Class<?>) rhsRaw);
}
}
Expand All @@ -73,10 +73,10 @@ else if (lhsClass.isArray() && rhsType instanceof GenericArrayType) {

// parameterized types are only assignable to other parameterized types and class types
if (lhsType instanceof ParameterizedType) {
if (rhsType instanceof Class<?>) {
if (rhsType instanceof Class) {
Type lhsRaw = ((ParameterizedType) lhsType).getRawType();

if (lhsRaw instanceof Class<?>) {
if (lhsRaw instanceof Class) {
return ClassUtils.isAssignable((Class<?>) lhsRaw, (Class<?>) rhsType);
}
}
Expand All @@ -88,7 +88,7 @@ else if (rhsType instanceof ParameterizedType) {
if (lhsType instanceof GenericArrayType) {
Type lhsComponent = ((GenericArrayType) lhsType).getGenericComponentType();

if (rhsType instanceof Class<?>) {
if (rhsType instanceof Class) {
Class<?> rhsClass = (Class<?>) rhsType;

if (rhsClass.isArray()) {
Expand Down
Expand Up @@ -59,7 +59,7 @@ public BooleanTypedValue getValueInternal(ExpressionState state) throws Evaluati
Object leftValue = left.getValue();
Object rightValue = right.getValue();
BooleanTypedValue result = null;
if (rightValue == null || !(rightValue instanceof Class<?>)) {
if (rightValue == null || !(rightValue instanceof Class)) {
throw new SpelEvaluationException(getRightOperand().getStartPosition(),
SpelMessage.INSTANCEOF_OPERATOR_NEEDS_CLASS_OPERAND,
(rightValue == null ? "null" : rightValue.getClass().getName()));
Expand Down
Expand Up @@ -1464,12 +1464,12 @@ else if (method.getName().equals("hashCode")) {
return System.identityHashCode(proxy);
}
else if (method.getName().equals("unwrap")) {
if (((Class) args[0]).isInstance(proxy)) {
if (((Class<?>) args[0]).isInstance(proxy)) {
return proxy;
}
}
else if (method.getName().equals("isWrapperFor")) {
if (((Class) args[0]).isInstance(proxy)) {
if (((Class<?>) args[0]).isInstance(proxy)) {
return true;
}
}
Expand Down
Expand Up @@ -530,7 +530,7 @@ else if (conversionHint instanceof JsonView) {
return extractViewClass((JsonView) conversionHint, conversionHint);
}
else if (conversionHint instanceof Class) {
return (Class) conversionHint;
return (Class<?>) conversionHint;
}
else {
return null;
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -171,7 +171,7 @@ protected <T> Object readWithMessageConverters(HttpInputMessage inputMessage, Me
}

Class<?> contextClass = (param != null ? param.getContainingClass() : null);
Class<T> targetClass = (targetType instanceof Class<?> ? (Class<T>) targetType : null);
Class<T> targetClass = (targetType instanceof Class ? (Class<T>) targetType : null);
if (targetClass == null) {
ResolvableType resolvableType = (param != null ?
ResolvableType.forMethodParameter(param) : ResolvableType.forType(targetType));
Expand Down
Expand Up @@ -203,7 +203,7 @@ protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handler
*/
private RequestMappingInfo createRequestMappingInfo(AnnotatedElement element) {
RequestMapping requestMapping = AnnotatedElementUtils.findMergedAnnotation(element, RequestMapping.class);
RequestCondition<?> condition = (element instanceof Class<?> ?
RequestCondition<?> condition = (element instanceof Class ?
getCustomTypeCondition((Class<?>) element) : getCustomMethodCondition((Method) element));
return (requestMapping != null ? createRequestMappingInfo(requestMapping, condition) : null);
}
Expand Down
Expand Up @@ -51,6 +51,7 @@
* a method argument that provides access to the response stream.
*
* @author Rossen Stoyanchev
* @author Juergen Hoeller
* @since 3.1
*/
public class ServletInvocableHandlerMethod extends InvocableHandlerMethod {
Expand Down Expand Up @@ -81,6 +82,7 @@ public ServletInvocableHandlerMethod(HandlerMethod handlerMethod) {
initResponseStatus();
}


private void initResponseStatus() {
ResponseStatus annotation = getMethodAnnotation(ResponseStatus.class);
if (annotation == null) {
Expand All @@ -92,7 +94,6 @@ private void initResponseStatus() {
}
}


/**
* Register {@link HandlerMethodReturnValueHandler} instances to use to
* handle return values.
Expand All @@ -101,8 +102,9 @@ public void setHandlerMethodReturnValueHandlers(HandlerMethodReturnValueHandlerC
this.returnValueHandlers = returnValueHandlers;
}


/**
* Invokes the method and handles the return value through one of the
* Invoke the method and handle the return value through one of the
* configured {@link HandlerMethodReturnValueHandler}s.
* @param webRequest the current request
* @param mavContainer the ModelAndViewContainer for this request
Expand Down Expand Up @@ -151,7 +153,7 @@ private void setResponseStatus(ServletWebRequest webRequest) throws IOException
else {
webRequest.getResponse().setStatus(this.responseStatus.value());
}
// to be picked up by the RedirectView
// To be picked up by RedirectView
webRequest.getRequest().setAttribute(View.RESPONSE_STATUS_ATTRIBUTE, this.responseStatus);
}

Expand Down Expand Up @@ -214,6 +216,7 @@ else if (result instanceof Throwable) {
return result;
}
}, CALLABLE_METHOD);

setHandlerMethodReturnValueHandlers(ServletInvocableHandlerMethod.this.returnValueHandlers);
this.returnType = returnType;
}
Expand Down

0 comments on commit bf0df54

Please sign in to comment.