001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.controls.runtime.generator;
020:
021: import java.util.Collection;
022: import java.util.HashMap;
023: import java.util.Iterator;
024:
025: import com.sun.mirror.declaration.FieldDeclaration;
026: import com.sun.mirror.declaration.TypeDeclaration;
027: import com.sun.mirror.declaration.TypeParameterDeclaration;
028: import com.sun.mirror.type.DeclaredType;
029: import com.sun.mirror.type.ReferenceType;
030: import com.sun.mirror.type.TypeMirror;
031:
032: /**
033: * The AptEventField class represents a field type that is also an event source
034: */
035: abstract public class AptEventField extends AptField {
036: public AptEventField(FieldDeclaration fieldDecl) {
037: super (fieldDecl);
038: }
039:
040: /**
041: * Inits the ControlInterface associated with this event field. The public interface
042: * for controls and contextual services, and their associated events can be modeled in the
043: * same way. Subclasses will override this to assign an appropriate interface.
044: */
045: abstract protected AptControlInterface initControlInterface();
046:
047: /**
048: * Computes the binding from any formal type parameters declared on the control interface
049: * to bound types on the field declaration.
050: */
051: private void initTypeParameterBindings() {
052: //
053: // Get an iterator to both the declared type arguments and the original type
054: // declaration on the associated control interface
055: //
056: DeclaredType fieldType = (DeclaredType) _fieldDecl.getType();
057: Iterator<TypeMirror> paramBoundIter = fieldType
058: .getActualTypeArguments().iterator();
059:
060: TypeDeclaration intfDecl = (TypeDeclaration) _controlIntf
061: .getTypeDeclaration();
062: Iterator<TypeParameterDeclaration> paramDeclIter = intfDecl
063: .getFormalTypeParameters().iterator();
064:
065: //
066: // Iterate through them in parallel, creating a mapping from the original formal
067: // type parameter name to the actual bound type. In parallel, also build up a
068: // representation of the bound type declaration.
069: //
070: // NOTE: If no type binding is done on the field declaration, then loop below
071: // will not execute and no mappings/empty bound decl will be the result.
072: //
073: StringBuffer sb = new StringBuffer();
074: boolean isFirst = true;
075: while (paramBoundIter.hasNext()) {
076: TypeMirror paramBound = paramBoundIter.next();
077: TypeParameterDeclaration paramDecl = paramDeclIter.next();
078:
079: //
080: // Save a mapping from the formal type name to the bound mirror type
081: //
082: _typeBindingMap.put(paramDecl.getSimpleName(), paramBound);
083:
084: if (isFirst) {
085: sb.append("<");
086: isFirst = false;
087: } else
088: sb.append(", ");
089: sb.append(paramBound);
090: }
091: if (!isFirst)
092: sb.append(">");
093:
094: _boundParameterDecl = sb.toString();
095: }
096:
097: /**
098: * Returns the ControlInterface associated with this event field
099: */
100: public AptControlInterface getControlInterface() {
101: if (_controlIntf == null) {
102: _controlIntf = initControlInterface();
103: if (_controlIntf != null)
104: initTypeParameterBindings();
105: }
106: return _controlIntf;
107: }
108:
109: /**
110: * Gets the EventAdaptor for a particular EventSet
111: */
112: public EventAdaptor getEventAdaptor(AptEventSet eventSet) {
113: return _eventAdaptors.get(eventSet);
114: }
115:
116: /**
117: * Adds a EventAdaptor for a particular EventSet
118: */
119: public void addEventAdaptor(AptEventSet eventSet,
120: EventAdaptor eventAdaptor) {
121: assert !_eventAdaptors.containsKey(eventSet);
122: _eventAdaptors.put(eventSet, eventAdaptor);
123: }
124:
125: /**
126: * Returns all EventAdaptors for this EventField
127: */
128: public Collection<EventAdaptor> getEventAdaptors() {
129: return _eventAdaptors.values();
130: }
131:
132: /**
133: * Returns the bound parameter declaration for this event field
134: */
135: public String getBoundParameters() {
136: return _boundParameterDecl;
137: }
138:
139: /**
140: * Returns the formal type binding map (from name to bound type) for the event field
141: */
142: public HashMap<String, TypeMirror> getTypeBindingMap() {
143: return _typeBindingMap;
144: }
145:
146: HashMap<AptEventSet, EventAdaptor> _eventAdaptors = new HashMap<AptEventSet, EventAdaptor>();
147:
148: String _boundParameterDecl;
149: HashMap<String, TypeMirror> _typeBindingMap = new HashMap<String, TypeMirror>();
150: private AptControlInterface _controlIntf;
151: }
|