Browse Source

Made ConfigurationModel semantics explicit by having it extend LinkedHashSet<ConfigurationClass>

conversation
Chris Beams 16 years ago
parent
commit
c9ab18e7e7
  1. 3
      org.springframework.config.java/src/main/java/org/springframework/config/java/support/BeanMethod.java
  2. 60
      org.springframework.config.java/src/main/java/org/springframework/config/java/support/ConfigurationModel.java
  3. 4
      org.springframework.config.java/src/main/java/org/springframework/config/java/support/ConfigurationModelBeanDefinitionReader.java
  4. 46
      org.springframework.config.java/src/test/java/org/springframework/config/java/support/ConfigurationModelTests.java

3
org.springframework.config.java/src/main/java/org/springframework/config/java/support/BeanMethod.java

@ -21,7 +21,6 @@ import static org.springframework.config.java.StandardScopes.*; @@ -21,7 +21,6 @@ import static org.springframework.config.java.StandardScopes.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.BeanMetadataElement;
import org.springframework.beans.factory.parsing.Location;
@ -48,7 +47,7 @@ final class BeanMethod implements BeanMetadataElement { @@ -48,7 +47,7 @@ final class BeanMethod implements BeanMetadataElement {
private final String name;
private final int modifiers;
private final ModelClass returnType;
private final List<Annotation> annotations = new ArrayList<Annotation>();
private final ArrayList<Annotation> annotations = new ArrayList<Annotation>();
private transient ConfigurationClass declaringClass;
private transient Object source;

60
org.springframework.config.java/src/main/java/org/springframework/config/java/support/ConfigurationModel.java

@ -17,7 +17,7 @@ package org.springframework.config.java.support; @@ -17,7 +17,7 @@ package org.springframework.config.java.support;
import static java.lang.String.*;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.parsing.ProblemReporter;
@ -36,66 +36,24 @@ import org.springframework.config.java.Configuration; @@ -36,66 +36,24 @@ import org.springframework.config.java.Configuration;
* @see ConfigurationParser
* @see ConfigurationModelBeanDefinitionReader
*/
final class ConfigurationModel {
/* list is used because order and collection equality matters. */
private final ArrayList<ConfigurationClass> configurationClasses = new ArrayList<ConfigurationClass>();
/**
* Add a {@link Configuration @Configuration} class to the model. Classes may be added
* at will and without any particular validation. Malformed classes will be caught and
* errors processed during {@link #validate() validation}
*
* @param configurationClass user-supplied Configuration class
*/
public ConfigurationModel add(ConfigurationClass configurationClass) {
configurationClasses.add(configurationClass);
return this;
}
public ConfigurationClass[] getAllConfigurationClasses() {
return configurationClasses.toArray(new ConfigurationClass[configurationClasses.size()]);
}
@SuppressWarnings("serial")
final class ConfigurationModel extends LinkedHashSet<ConfigurationClass> {
/**
* Recurses through the model validating each object along the way and aggregating any
* <var>errors</var>.
* Recurses through the model validating each {@link ConfigurationClass}.
*
* @param problemReporter {@link ProblemReporter} against which any validation errors
* will be registered
* @see ConfigurationClass#validate
*/
public void validate(ProblemReporter problemReporter) {
for (ConfigurationClass configClass : configurationClasses)
for (ConfigurationClass configClass : this)
configClass.validate(problemReporter);
}
@Override
public String toString() {
return format("%s: configurationClasses=%s", getClass().getSimpleName(), configurationClasses);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((configurationClasses == null) ? 0 : configurationClasses.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ConfigurationModel other = (ConfigurationModel) obj;
if (configurationClasses == null) {
if (other.configurationClasses != null)
return false;
} else if (!configurationClasses.equals(other.configurationClasses))
return false;
return true;
return format("%s containing @Configuration classes: %s", getClass().getSimpleName(), super.toString());
}
}
}

4
org.springframework.config.java/src/main/java/org/springframework/config/java/support/ConfigurationModelBeanDefinitionReader.java

@ -68,10 +68,10 @@ class ConfigurationModelBeanDefinitionReader { @@ -68,10 +68,10 @@ class ConfigurationModelBeanDefinitionReader {
*
* @return number of bean definitions generated
*/
public BeanDefinitionRegistry loadBeanDefinitions(ConfigurationModel model) {
public BeanDefinitionRegistry loadBeanDefinitions(ConfigurationModel configurationModel) {
registry = new SimpleBeanDefinitionRegistry();
for (ConfigurationClass configClass : model.getAllConfigurationClasses())
for (ConfigurationClass configClass : configurationModel)
loadBeanDefinitionsForConfigurationClass(configClass);
return registry;

46
org.springframework.config.java/src/test/java/org/springframework/config/java/support/ConfigurationModelTests.java

@ -0,0 +1,46 @@ @@ -0,0 +1,46 @@
/*
* Copyright 2002-2009 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.config.java.support;
import static java.lang.String.*;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.Test;
/**
* Unit tests for {@link ConfigurationModel}.
*
* @author Chris Beams
*/
public class ConfigurationModelTests {
@Test
public void testToString() {
ConfigurationModel model = new ConfigurationModel();
assertThat(model.toString(), equalTo(
"ConfigurationModel containing @Configuration classes: []"));
ConfigurationClass config1 = new ConfigurationClass();
config1.setName("test.Config1");
model.add(config1);
assertThat(model.toString(), equalTo(format(
"ConfigurationModel containing @Configuration classes: [%s]", config1)));
}
}
Loading…
Cancel
Save