001    package org.mesopotamia.lang.java;
002    
003    import java.util.List;
004    
005    import org.mesopotamia.MesopotamiaException;
006    import org.mesopotamia.NodeData;
007    import org.mesopotamia.RepositoryLanguage;
008    import org.mesopotamia.Scan;
009    import org.mesopotamia.lang.java.ref.MethodInfo;
010    import org.mesopotamia.lang.java.ref.Scope;
011    import org.mesopotamia.lang.java.ref.TypeInfo;
012    import org.mesopotamia.lang.java.ref.VariableInfo;
013    import org.w3c.dom.Element;
014    
015    import biz.hammurapi.util.Visitable;
016    import biz.hammurapi.util.Visitor;
017    
018    public class TypeParameter extends JavaLanguageElement implements TypeInfo {
019    
020            public TypeParameter(NodeData xData, Class<?> context, Scan scan,
021                            RepositoryLanguage language, Object environment) throws MesopotamiaException {
022                    super(xData, context, scan, language, environment);
023    
024                    // Select attributes
025                    Name = selectSingleElementText(Identifier.class, "IDENT[0]");
026                    TypeBound = selectSingleElement(TypeBound.class, "#org.mesopotamia.lang.java.TypeBound");
027    
028            }
029    
030            public void toDom(Element holder) {
031                    super.toDom(holder);
032    
033                    // Serialize attributes
034                    setAttribute(holder, "Name", Name);
035                    setElement(holder, "TypeBound", TypeBound);
036            }
037    
038            // Attributes
039            private String Name;
040    
041            private TypeBound TypeBound;
042    
043            // Accessors
044            public String getName() {
045                    return Name;
046            }
047    
048            public TypeBound getTypeBound() {
049                    return TypeBound;
050            }
051    
052            protected void acceptChildren(Visitor visitor) {
053                    super.acceptChildren(visitor);
054                    // Visiting non-text attributes
055                    if (TypeBound instanceof Visitable) {
056                            ((Visitable) TypeBound).accept(visitor);
057                    }
058            }
059    
060            public MethodInfo findTypeMethod(String name, String[] parameterTypes) {
061                    Scope ec = findParent(Scope.class);
062                    if (ec!=null) {
063                            TypeInfo ti = ec.findType(TypeBound==null ? JAVA_LANG_OBJECT : TypeBound.getBoundType().getPath());
064                            if (ti!=null) {
065                                    return ti.findTypeMethod(name, parameterTypes);
066                            }
067                    }
068                    
069                    return null;
070            }
071    
072            public TypeInfo findNestedType(String name) {
073                    if (TypeBound==null) {
074                            return null;
075                    }
076                    Scope ec = findParent(Scope.class);
077                    if (ec!=null) {
078                            TypeInfo ti = ec.findType(TypeBound.getBoundType().getPath());
079                            if (ti!=null) {
080                                    return ti.findNestedType(name);
081                            }
082                    }
083                    
084                    return null;
085            }
086    
087            public VariableInfo findTypeVariable(String name) {
088                    Scope ec = findParent(Scope.class);
089                    if (ec!=null) {
090                            TypeInfo ti = ec.findType(TypeBound==null ? JAVA_LANG_OBJECT : TypeBound.getBoundType().getPath());
091                            if (ti!=null) {
092                                    return ti.findTypeVariable(name);
093                            }
094                    }
095                    
096                    return null;
097            }
098    
099            public boolean isKindOf(String superFcn) {
100                    if (TypeBound==null) {
101                            return JAVA_LANG_OBJECT.equals(superFcn);
102                    }
103                    Scope ec = findParent(Scope.class);
104                    if (ec!=null) {
105                            TypeInfo ti = ec.findType(TypeBound.getBoundType().getPath());
106                            if (ti!=null) {
107                                    return ti.isKindOf(superFcn);
108                            }
109                    }
110                    
111                    return false;
112            }
113    
114            public String getFcn() {
115                    if (TypeBound==null) {
116                            return JAVA_LANG_OBJECT;
117                    }
118                    Scope ec = findParent(Scope.class);
119                    if (ec!=null) {
120                            Identifier boundType = TypeBound.getBoundType();
121                            if (boundType!=null) {
122                                    TypeInfo ti = ec.findType(boundType.getPath());
123                                    if (ti!=null) {
124                                            return ti.getFcn();
125                                    }
126                            }
127                    }
128                    
129                    return null;
130            }
131    
132            public TypeInfo getDeclaringType() {
133                    return null;
134            }
135    
136            @SuppressWarnings("unchecked")
137            public List<String> getModifiers() {
138                    return emptyList;
139            }
140    
141    
142            public MethodInfo findConstructor(String[] parameterTypes) {
143                    if (TypeBound==null) {
144                            return null;
145                    }
146                    Scope ec = findParent(Scope.class);
147                    if (ec!=null) {
148                            TypeInfo ti = ec.findType(TypeBound.getBoundType().getPath());
149                            if (ti!=null) {
150                                    return ti.findConstructor(parameterTypes);
151                            }
152                    }
153                    
154                    return null;
155            }
156            
157    }