001    /*
002     * Copyright (C) 2006-2007 the original author or authors.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of 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,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.codehaus.gmaven.runtime.support.stubgen.model;
018    
019    import java.util.Collections;
020    import java.util.Iterator;
021    import java.util.LinkedHashSet;
022    import java.util.Set;
023    
024    /**
025     * Representation of a class definition.
026     *
027     * @version $Id: ClassDef.java 18 2009-07-16 09:39:40Z user57 $
028     * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
029     */
030    public class ClassDef
031        extends Entity
032    {
033        private final Type type;
034    
035        private SourceDef parent;
036    
037        private TypeDef superClass;
038    
039        private final Set implementz = new LinkedHashSet();
040    
041        private final Set fields = new LinkedHashSet();
042    
043        private final Set methods = new LinkedHashSet();
044    
045        public ClassDef() {
046            this(Type.CLASS);
047        }
048    
049        protected ClassDef(final Type type) {
050            assert type != null;
051    
052            this.type = type;
053        }
054    
055        public SourceDef getParent() {
056            return parent;
057        }
058    
059        public void setParent(final SourceDef parent) {
060            this.parent = parent;
061        }
062    
063        public Type getType() {
064            return type;
065        }
066    
067        public boolean isInterface() {
068            return type == Type.INTERFACE;
069        }
070    
071        public boolean isEnum() {
072            return type == Type.ENUM;
073        }
074    
075        public boolean isAnnotation() {
076            return type == Type.ANNOTATION;    
077        }
078    
079        public PackageDef getPackage() {
080            if (parent == null) {
081                return null;
082            }
083    
084            return parent.getPackage();
085        }
086    
087        public Set getImports() {
088            if (parent == null) {
089                return Collections.EMPTY_SET;
090            }
091    
092            return parent.getImports();
093        }
094        
095        public TypeDef getSuperClass() {
096            return superClass;
097        }
098    
099        public void setSuperClass(final TypeDef type) {
100            this.superClass = type;
101        }
102    
103        public void setSuperClass(final String type) {
104            assert type != null;
105    
106            setSuperClass(new TypeDef(type));
107        }
108        
109        public void addImplements(final TypeDef type) {
110            assert type != null;
111    
112            implementz.add(type);
113        }
114    
115        public void addImplements(final String type) {
116            assert type != null;
117    
118            addImplements(new TypeDef(type));
119        }
120    
121        public Set getImplements() {
122            return implementz;
123        }
124    
125        public void addField(final FieldDef def) {
126            assert def != null;
127    
128            def.setParent(this);
129            fields.add(def);
130        }
131    
132        public Set getFields() {
133            return fields;
134        }
135    
136        public void addConstructor(final ConstructorDef def) {
137            assert def != null;
138    
139            def.setParent(this);
140            methods.add(def);
141        }
142    
143        public void addMethod(final MethodDef def) {
144            assert def != null;
145    
146            def.setParent(this);
147            methods.add(def);
148        }
149    
150        public Set getMethods() {
151            return methods;
152        }
153    
154        public Set getConstructors() {
155            Set set = new LinkedHashSet();
156    
157            Iterator iter = getMethods().iterator();
158            while (iter.hasNext()) {
159                MethodDef def = (MethodDef)iter.next();
160                if (def.isConstructor()) {
161                    set.add(def);
162                }
163            }
164    
165            return set;
166        }
167    
168        //
169        // Type
170        //
171    
172        /**
173         * Psuedo-enum for class type.
174         */
175        public static final class Type
176        {
177            public static final String CLASS_NAME = "class";
178    
179            public static final int CLASS_CODE = 0;
180    
181            public static final Type CLASS = new Type(CLASS_NAME, CLASS_CODE);
182    
183            public static final String INTERFACE_NAME = "interface";
184    
185            public static final int INTERFACE_CODE = 1;
186    
187            public static final Type INTERFACE = new Type(INTERFACE_NAME, INTERFACE_CODE);
188    
189            public static final String ENUM_NAME = "enum";
190    
191            public static final int ENUM_CODE = 2;
192    
193            public static final Type ENUM = new Type(ENUM_NAME, ENUM_CODE);
194    
195            public static final String ANNOTATION_NAME = "@interface";
196    
197            public static final int ANNOTATION_CODE = 3;
198    
199            public static final Type ANNOTATION = new Type(ANNOTATION_NAME, ANNOTATION_CODE);
200    
201            public final String name;
202    
203            public final int code;
204    
205            private Type(final String name, final int code) {
206                assert name != null;
207    
208                this.name = name;
209                this.code = code;
210            }
211    
212            public String toString() {
213                return name;
214            }
215        }
216    }