Containerに関連付けられた変数
6225 ワード
package com.opensymphony.xwork2.inject;
/**
* Dependency mapping key. Uniquely identified by the required type and name.
*
* @author [email protected] (Bob Lee)
*/
class Key<T> {
final Class<T> type;
final String name;
final int hashCode;
// private
private Key(Class<T> type, String name) {
if (type == null) {
throw new NullPointerException("Type is null.");
}
if (name == null) {
throw new NullPointerException("Name is null.");
}
this.type = type;
this.name = name;
hashCode = type.hashCode() * 31 + name.hashCode();
}
Class<T> getType() {
return type;
}
String getName() {
return name;
}
public int hashCode() {
return hashCode;
}
public boolean equals(Object o) {
if (!(o instanceof Key)) {
return false;
}
if (o == this) {
return true;
}
Key other = (Key) o;
return name.equals(other.name) && type.equals(other.type);
}
public String toString() {
return "[type=" + type.getName() + ", name='" + name + "']";
}
//
static <T> Key<T> newInstance(Class<T> type, String name) {
return new Key<T>(type, name);
}
}
//InternalFactory
package com.opensymphony.xwork2.inject;
import java.io.Serializable;
/**
* Creates objects which will be injected.
*
* @author [email protected] (Bob Lee)
*/
//
interface InternalFactory<T> extends Serializable {
/**
* Creates an object to be injected.
*
* @param context of this injection
* @return instance to be injected
*/
T create(InternalContext context);//
}
//InternalContext定義
package com.opensymphony.xwork2.inject;
import java.util.HashMap;
import java.util.Map;
/**
* Internal context. Used to coordinate injections and support circular
* dependencies.
*
* @author [email protected] (Bob Lee)
*/
class InternalContext {
final ContainerImpl container;// final
final Map<Object, ConstructionContext<?>> constructionContexts =
new HashMap<Object, ConstructionContext<?>>();
Scope.Strategy scopeStrategy;//
ExternalContext<?> externalContext;// ExternalContext<?>
//
InternalContext(ContainerImpl container) {
this.container = container;
}
public Container getContainer() {
return container;
}
// ?
ContainerImpl getContainerImpl() {
return container;
}
Scope.Strategy getScopeStrategy() {
if (scopeStrategy == null) {
scopeStrategy = (Scope.Strategy) container.localScopeStrategy.get();
if (scopeStrategy == null) {
throw new IllegalStateException("Scope strategy not set. "
+ "Please call Container.setScopeStrategy().");
}
}
return scopeStrategy;
}
@SuppressWarnings("unchecked")
<T> ConstructionContext<T> getConstructionContext(Object key) {
ConstructionContext<T> constructionContext =
(ConstructionContext<T>) constructionContexts.get(key);
if (constructionContext == null) {
constructionContext = new ConstructionContext<T>();
constructionContexts.put(key, constructionContext);
}
return constructionContext;
}
// get set
@SuppressWarnings("unchecked")
<T> ExternalContext<T> getExternalContext() {
return (ExternalContext<T>) externalContext;
}
void setExternalContext(ExternalContext<?> externalContext) {
this.externalContext = externalContext;
}
}
//ExtralContext定義
package com.opensymphony.xwork2.inject;
import java.lang.reflect.Member;
import java.util.LinkedHashMap;
/**
* An immutable snapshot of the current context which is safe to
* expose to client code.
*
* @author [email protected] (Bob Lee)
*/
class ExternalContext<T> implements Context {
final Member member;// final
final Key<T> key;
final ContainerImpl container;
// public
public ExternalContext(Member member, Key<T> key, ContainerImpl container) {
this.member = member;
this.key = key;
this.container = container;
}
public Class<T> getType() {
return key.getType();
}
public Scope.Strategy getScopeStrategy() {
return (Scope.Strategy) container.localScopeStrategy.get();
}
public Container getContainer() {
return container;
}
public Member getMember() {
return member;
}
public String getName() {
return key.getName();
}
public String toString() {
return "Context" + new LinkedHashMap<String, Object>() {{
put("member", member);
put("type", getType());
put("name", getName());
put("container", container);
}}.toString();
}
//
static <T> ExternalContext<T> newInstance(Member member, Key<T> key,
ContainerImpl container) {
return new ExternalContext<T>(member, key, container);
}
}
//ClonstructContext