Skip to content

Commit

Permalink
Polishing
Browse files Browse the repository at this point in the history
(cherry picked from commit a31ebb6)
  • Loading branch information
jhoeller committed Aug 14, 2014
1 parent 0c89279 commit 0492c99
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 87 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 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 @@ -170,8 +170,7 @@ public interface BeanFactory {
* <p>Allows for specifying explicit constructor arguments / factory method arguments,
* overriding the specified default arguments (if any) in the bean definition.
* @param name the name of the bean to retrieve
* @param args arguments to use if creating a prototype using explicit arguments to a
* static factory method. It is invalid to use a non-null args value in any other case.
* @param args arguments to use if creating a prototype using explicit arguments
* @return an instance of the bean
* @throws NoSuchBeanDefinitionException if there is no such bean definition
* @throws BeanDefinitionStoreException if arguments have been given but
Expand Down
Expand Up @@ -236,7 +236,8 @@ public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, Strin
Annotation annotation = findAutowiredAnnotation(candidate);
if (annotation != null) {
if (requiredConstructor != null) {
throw new BeanCreationException("Invalid autowire-marked constructor: " + candidate +
throw new BeanCreationException(beanName,
"Invalid autowire-marked constructor: " + candidate +
". Found another constructor with 'required' Autowired annotation: " +
requiredConstructor);
}
Expand All @@ -247,10 +248,10 @@ public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, Strin
boolean required = determineRequiredStatus(annotation);
if (required) {
if (!candidates.isEmpty()) {
throw new BeanCreationException(
throw new BeanCreationException(beanName,
"Invalid autowire-marked constructors: " + candidates +
". Found another constructor with 'required' Autowired annotation: " +
requiredConstructor);
candidate);
}
requiredConstructor = candidate;
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 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 @@ -1228,7 +1228,7 @@ protected void checkMergedBeanDefinition(RootBeanDefinition mbd, String beanName
// Check validity of the usage of the args parameter. This can
// only be used for prototypes constructed via a factory method.
if (args != null && !mbd.isPrototype()) {
throw new BeanDefinitionStoreException(
throw new BeanDefinitionStoreException(mbd.getResourceDescription(), beanName,
"Can only specify arguments for the getBean method when referring to a prototype bean definition");
}
}
Expand Down
Expand Up @@ -33,8 +33,9 @@

/**
* Default object instantiation strategy for use in BeanFactories.
* Uses CGLIB to generate subclasses dynamically if methods need to be
* overridden by the container, to implement Method Injection.
*
* <p>Uses CGLIB to generate subclasses dynamically if methods need to be
* overridden by the container to implement <em>Method Injection</em>.
*
* @author Rod Johnson
* @author Juergen Hoeller
Expand All @@ -50,13 +51,13 @@ public class CglibSubclassingInstantiationStrategy extends SimpleInstantiationSt

/**
* Index in the CGLIB callback array for a method that should
* be overridden to provide method lookup.
* be overridden to provide <em>method lookup</em>.
*/
private static final int LOOKUP_OVERRIDE = 1;

/**
* Index in the CGLIB callback array for a method that should
* be overridden using generic Methodreplacer functionality.
* be overridden using generic <em>method replacer</em> functionality.
*/
private static final int METHOD_REPLACER = 2;

Expand Down Expand Up @@ -96,13 +97,13 @@ public CglibSubclassCreator(RootBeanDefinition beanDefinition, BeanFactory owner
}

/**
* Create a new instance of a dynamically generated subclasses implementing the
* Create a new instance of a dynamically generated subclass implementing the
* required lookups.
* @param ctor constructor to use. If this is {@code null}, use the
* no-arg constructor (no parameterization, or Setter Injection)
* @param args arguments to use for the constructor.
* Ignored if the ctor parameter is {@code null}.
* @return new instance of the dynamically generated class
* Ignored if the {@code ctor} parameter is {@code null}.
* @return new instance of the dynamically generated subclass
*/
public Object instantiate(Constructor<?> ctor, Object[] args) {
Enhancer enhancer = new Enhancer();
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 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 @@ -37,39 +37,33 @@ public class LookupOverride extends MethodOverride {

/**
* Construct a new LookupOverride.
* @param methodName the name of the method to override.
* This method must have no arguments.
* @param beanName name of the bean in the current BeanFactory
* that the overriden method should return
* @param methodName the name of the method to override
* @param beanName the name of the bean in the current BeanFactory
* that the overridden method should return
*/
public LookupOverride(String methodName, String beanName) {
super(methodName);
Assert.notNull(beanName, "Bean name must not be null");
this.beanName = beanName;
}


/**
* Return the name of the bean that should be returned by this method.
*/
public String getBeanName() {
return this.beanName;
}


/**
* Match method of the given name, with no parameters.
* Match the method of the given name, with no parameters.
*/
@Override
public boolean matches(Method method) {
return (method.getName().equals(getMethodName()) && method.getParameterTypes().length == 0);
}


@Override
public String toString() {
return "LookupOverride for method '" + getMethodName() + "'; will return bean '" + this.beanName + "'";
}

@Override
public boolean equals(Object other) {
return (other instanceof LookupOverride && super.equals(other) &&
Expand All @@ -81,4 +75,9 @@ public int hashCode() {
return (29 * super.hashCode() + ObjectUtils.nullSafeHashCode(this.beanName));
}

@Override
public String toString() {
return "LookupOverride for method '" + getMethodName() + "'; will return bean '" + this.beanName + "'";
}

}
Expand Up @@ -52,6 +52,7 @@ protected MethodOverride(String methodName) {
this.methodName = methodName;
}


/**
* Return the name of the method to be overridden.
*/
Expand Down Expand Up @@ -98,6 +99,7 @@ public Object getSource() {
*/
public abstract boolean matches(Method method);


@Override
public boolean equals(Object other) {
if (this == other) {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 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 @@ -52,6 +52,7 @@ public ReplaceOverride(String methodName, String methodReplacerBeanName) {
this.methodReplacerBeanName = methodReplacerBeanName;
}


/**
* Return the name of the bean implementing MethodReplacer.
*/
Expand All @@ -68,41 +69,29 @@ public void addTypeIdentifier(String identifier) {
this.typeIdentifiers.add(identifier);
}


@Override
public boolean matches(Method method) {
// TODO could cache result for efficiency
if (!method.getName().equals(getMethodName())) {
// It can't match.
return false;
}

if (!isOverloaded()) {
// No overloaded: don't worry about arg type matching.
// Not overloaded: don't worry about arg type matching...
return true;
}

// If we get to here, we need to insist on precise argument matching.
// If we get here, we need to insist on precise argument matching...
if (this.typeIdentifiers.size() != method.getParameterTypes().length) {
return false;
}
for (int i = 0; i < this.typeIdentifiers.size(); i++) {
String identifier = this.typeIdentifiers.get(i);
if (!method.getParameterTypes()[i].getName().contains(identifier)) {
// This parameter cannot match.
return false;
}
}
return true;
}


@Override
public String toString() {
return "Replace override for method '" + getMethodName() + "; will call bean '" +
this.methodReplacerBeanName + "'";
}

@Override
public boolean equals(Object other) {
if (!(other instanceof ReplaceOverride) || !super.equals(other)) {
Expand All @@ -121,4 +110,10 @@ public int hashCode() {
return hashCode;
}

@Override
public String toString() {
return "Replace override for method '" + getMethodName() + "; will call bean '" +
this.methodReplacerBeanName + "'";
}

}

0 comments on commit 0492c99

Please sign in to comment.