001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.core.ext.typeinfo;
017:
018: import java.util.ArrayList;
019: import java.util.HashMap;
020: import java.util.List;
021: import java.util.Map;
022:
023: /**
024: * Class that initializes the different members of a
025: * {@link JDelegatingClassType} from its corresponding base type on demand.
026: */
027: class DelegateMembers extends AbstractMembers {
028: private final JClassType baseType;
029: private List<JConstructor> lazyConstructors;
030: private Map<String, JField> lazyFields;
031: private Map<String, List<JMethod>> lazyMethods;
032: private final Substitution substitution;
033:
034: /**
035: */
036: public DelegateMembers(JDelegatingClassType enclosingType,
037: JClassType baseType, Substitution substitution) {
038: super (enclosingType);
039: this .baseType = baseType;
040: this .substitution = substitution;
041: }
042:
043: @Override
044: protected List<JConstructor> doGetConstructors() {
045: if (lazyConstructors != null) {
046: /*
047: * Return if the constructors are being initialized or have been
048: * initialized.
049: */
050: return lazyConstructors;
051: }
052: lazyConstructors = new ArrayList<JConstructor>();
053:
054: JConstructor[] baseCtors = baseType.getConstructors();
055: for (JConstructor baseCtor : baseCtors) {
056: JConstructor newCtor = new JConstructor(getParentType(),
057: baseCtor);
058: initializeParams(baseCtor, newCtor);
059: addConstructor(newCtor);
060: }
061:
062: return lazyConstructors;
063: }
064:
065: @Override
066: protected Map<String, JField> doGetFields() {
067: if (lazyFields != null) {
068: /*
069: * Return if the fields are being initialized or have been
070: * initialized.
071: */
072: return lazyFields;
073: }
074: lazyFields = new HashMap<String, JField>();
075:
076: JField[] baseFields = baseType.getFields();
077: for (JField baseField : baseFields) {
078: JField newField = new JField(getParentType(), baseField);
079: newField.setType(substitution.getSubstitution(baseField
080: .getType()));
081: addField(newField);
082: }
083:
084: return lazyFields;
085: }
086:
087: @Override
088: protected Map<String, List<JMethod>> doGetMethods() {
089: if (lazyMethods != null) {
090: /*
091: * Return if the methods are being initialized or have been
092: * initialized.
093: */
094: return lazyMethods;
095: }
096: lazyMethods = new HashMap<String, List<JMethod>>();
097:
098: JMethod[] baseMethods = baseType.getMethods();
099: for (JMethod baseMethod : baseMethods) {
100: JMethod newMethod = new JMethod(getParentType(), baseMethod);
101: initializeParams(baseMethod, newMethod);
102: newMethod.setReturnType(substitution
103: .getSubstitution(baseMethod.getReturnType()));
104: addMethod(newMethod);
105: }
106:
107: return lazyMethods;
108: }
109:
110: private void initializeParams(JAbstractMethod srcMethod,
111: JAbstractMethod newMethod) {
112: for (JParameter srcParam : srcMethod.getParameters()) {
113: JParameter newParam = new JParameter(newMethod, srcParam);
114: newParam.setType(substitution.getSubstitution(srcParam
115: .getType()));
116: newMethod.addParameter(newParam);
117: }
118: }
119: }
|