001: // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
002:
003: package jodd.petite;
004:
005: import jodd.petite.scope.Scope;
006: import jodd.util.HashCodeUtil;
007: import jodd.util.ObjectUtil;
008:
009: import java.util.Set;
010: import java.util.HashSet;
011:
012: /**
013: * Petite bean definition, for internal use only.
014: */
015: class BeanDef {
016:
017: BeanDef(String name, Class type, Scope scope) {
018: this .name = name;
019: this .type = type;
020: this .scope = scope;
021: }
022:
023: final String name; // bean name
024:
025: Class type; // bean type
026:
027: // ---------------------------------------------------------------- scope delegates
028:
029: private final Scope scope; // bean scope
030:
031: /**
032: * Delegates to {@link jodd.petite.scope.Scope#lookup(String)}.
033: */
034: Object scopeLookup() {
035: return scope.lookup(name);
036: }
037:
038: /**
039: * Delegates to {@link jodd.petite.scope.Scope#register(String, Object)}.
040: */
041: void scopeRegister(Object object) {
042: scope.register(name, object);
043: }
044:
045: /**
046: * Delegates to {@link jodd.petite.scope.Scope#remove(String)}.
047: */
048: void scopeRemove() {
049: scope.remove(name);
050: }
051:
052: // ---------------------------------------------------------------- injection tracking
053:
054: /**
055: * Holds information about injection: target bean name and its field name.
056: */
057: static class InjectionTarget {
058: InjectionTarget(String beanName, String fieldName) {
059: this .beanName = beanName;
060: this .fieldName = fieldName;
061: }
062:
063: final String beanName; // target
064: final String fieldName; // field name
065:
066: @Override
067: public int hashCode() {
068: int result = HashCodeUtil.SEED;
069: result = HashCodeUtil.hash(result, beanName);
070: result = HashCodeUtil.hash(result, fieldName);
071: return result;
072: }
073:
074: @Override
075: public boolean equals(Object object) {
076: if (this == object) {
077: return true;
078: }
079: if (ObjectUtil.equalsType(object, this ) == false) {
080: return false;
081: }
082: final InjectionTarget it = (InjectionTarget) object;
083: return (it.beanName.equals(this .beanName))
084: && (it.fieldName.equals(this .fieldName));
085: }
086: }
087:
088: Set<InjectionTarget> injectectionTargets; // set of injection informations
089:
090: /**
091: * Stores injection information.
092: */
093: void injectedTo(String targetBeanName, String fieldName) {
094: if (injectectionTargets == null) {
095: injectectionTargets = new HashSet<InjectionTarget>();
096: }
097: injectectionTargets.add(new InjectionTarget(targetBeanName,
098: fieldName));
099: }
100:
101: }
|