1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package signature.model.impl;
18 
19 import java.io.Serializable;
20 import java.util.Arrays;
21 import java.util.List;
22 import java.util.Set;
23 
24 import signature.model.IAnnotationField;
25 import signature.model.IClassDefinition;
26 import signature.model.IConstructor;
27 import signature.model.IEnumConstant;
28 import signature.model.IField;
29 import signature.model.IMethod;
30 import signature.model.ITypeReference;
31 import signature.model.ITypeVariableDefinition;
32 import signature.model.Kind;
33 import signature.model.Modifier;
34 import signature.model.util.ModelUtil;
35 
36 @SuppressWarnings("serial")
37 public class SigClassDefinition extends SigAnnotatableElement implements
38         IClassDefinition, Serializable {
39 
40     private String name;
41     private Kind kind = Kind.UNINITIALIZED;
42     private ITypeReference superClass = Uninitialized.unset();
43     private Set<ITypeReference> interfaces = Uninitialized.unset();
44     private Set<Modifier> modifiers = Uninitialized.unset();
45     private Set<IMethod> methods = Uninitialized.unset();
46     private Set<IConstructor> constructors = Uninitialized.unset();
47     private Set<IClassDefinition> innerClasses = Uninitialized.unset();
48     private Set<IAnnotationField> annotationFields = Uninitialized.unset();
49     private Set<IField> fields = Uninitialized.unset();
50     private Set<IEnumConstant> enumConstants = Uninitialized.unset();
51     private IClassDefinition declaringClass = Uninitialized.unset();
52     private List<ITypeVariableDefinition> typeParameters = Uninitialized
53             .unset();
54     private String packageName;
55 
SigClassDefinition(String packageName, String name)56     public SigClassDefinition(String packageName, String name) {
57         this.packageName = packageName;
58         this.name = name;
59     }
60 
getKind()61     public Kind getKind() {
62         return kind;
63     }
64 
setKind(Kind kind)65     public void setKind(Kind kind) {
66         this.kind = kind;
67     }
68 
getName()69     public String getName() {
70         return name;
71     }
72 
getPackageName()73     public String getPackageName() {
74         return packageName;
75     }
76 
getPackageFragments()77     public List<String> getPackageFragments() {
78         return Arrays.asList(packageName.split("\\."));
79     }
80 
getQualifiedName()81     public String getQualifiedName() {
82         return packageName + "." + name;
83     }
84 
getModifiers()85     public Set<Modifier> getModifiers() {
86         return modifiers;
87     }
88 
setModifiers(Set<Modifier> modifiers)89     public void setModifiers(Set<Modifier> modifiers) {
90         this.modifiers = modifiers;
91     }
92 
getInnerClasses()93     public Set<IClassDefinition> getInnerClasses() {
94         return innerClasses;
95     }
96 
setInnerClasses(Set<IClassDefinition> innerClasses)97     public void setInnerClasses(Set<IClassDefinition> innerClasses) {
98         this.innerClasses = innerClasses;
99     }
100 
getInterfaces()101     public Set<ITypeReference> getInterfaces() {
102         return interfaces;
103     }
104 
setInterfaces(Set<ITypeReference> interfaces)105     public void setInterfaces(Set<ITypeReference> interfaces) {
106         this.interfaces = interfaces;
107     }
108 
getMethods()109     public Set<IMethod> getMethods() {
110         return methods;
111     }
112 
setMethods(Set<IMethod> methods)113     public void setMethods(Set<IMethod> methods) {
114         this.methods = methods;
115     }
116 
getConstructors()117     public Set<IConstructor> getConstructors() {
118         return constructors;
119     }
120 
setConstructors(Set<IConstructor> constructors)121     public void setConstructors(Set<IConstructor> constructors) {
122         this.constructors = constructors;
123     }
124 
getSuperClass()125     public ITypeReference getSuperClass() {
126         return superClass;
127     }
128 
setSuperClass(ITypeReference superClass)129     public void setSuperClass(ITypeReference superClass) {
130         this.superClass = superClass;
131     }
132 
getDeclaringClass()133     public IClassDefinition getDeclaringClass() {
134         return declaringClass;
135     }
136 
setDeclaringClass(IClassDefinition declaringClass)137     public void setDeclaringClass(IClassDefinition declaringClass) {
138         this.declaringClass = declaringClass;
139     }
140 
getAnnotationFields()141     public Set<IAnnotationField> getAnnotationFields() {
142         return annotationFields;
143     }
144 
setAnnotationFields(Set<IAnnotationField> annotationFields)145     public void setAnnotationFields(Set<IAnnotationField> annotationFields) {
146         this.annotationFields = annotationFields;
147     }
148 
getFields()149     public Set<IField> getFields() {
150         return fields;
151     }
152 
setFields(Set<IField> fields)153     public void setFields(Set<IField> fields) {
154         this.fields = fields;
155     }
156 
getEnumConstants()157     public Set<IEnumConstant> getEnumConstants() {
158         return enumConstants;
159     }
160 
setEnumConstants(Set<IEnumConstant> enumConstants)161     public void setEnumConstants(Set<IEnumConstant> enumConstants) {
162         this.enumConstants = enumConstants;
163     }
164 
getTypeParameters()165     public List<ITypeVariableDefinition> getTypeParameters() {
166         return typeParameters;
167     }
168 
setTypeParameters( List<ITypeVariableDefinition> typeParameters)169     public void setTypeParameters(
170             List<ITypeVariableDefinition> typeParameters) {
171         this.typeParameters = typeParameters;
172     }
173 
174     @Override
hashCode()175     public int hashCode() {
176         return SigClassDefinition.hashCode(this);
177     }
178 
hashCode(IClassDefinition definition)179     public static int hashCode(IClassDefinition definition) {
180         // FIXME find out why name and packageName are null during
181         // de-serialization some cases
182         // replace the following lines with:
183         // return definition.getQualifiedName().hashCode();
184         final int prime = 31;
185         int result = 1;
186         result = prime
187                 + ((definition.getName() == null) ? 0 : definition.getName()
188                         .hashCode());
189         result = prime
190                 * result
191                 + ((definition.getPackageName() == null) ? 0 : definition
192                         .getPackageName().hashCode());
193         return result;
194     }
195 
196     @Override
equals(Object obj)197     public boolean equals(Object obj) {
198         return SigClassDefinition.equals(this, obj);
199     }
200 
equals(IClassDefinition thiz, Object obj)201     public static boolean equals(IClassDefinition thiz, Object obj) {
202         if (thiz == obj) return true;
203         if (obj instanceof IClassDefinition) {
204             IClassDefinition that = (IClassDefinition) obj;
205             return thiz.getName().equals(that.getName())
206                     && thiz.getPackageName().equals(that.getPackageName());
207         }
208         return false;
209     }
210 
211     @Override
toString()212     public String toString() {
213         return SigClassDefinition.toString(this);
214     }
215 
toString(IClassDefinition type)216     public static String toString(IClassDefinition type) {
217         StringBuilder builder = new StringBuilder();
218         if (type.getAnnotations() != null && !type.getAnnotations().isEmpty()) {
219             // FIXME print annotations builder.append(super.toString());
220             builder.append("\n");
221         }
222         builder.append(type.getQualifiedName());
223         if (type.getTypeParameters() != null
224                 && (!type.getTypeParameters().isEmpty())) {
225             builder.append("<");
226             ModelUtil.separate(type.getTypeParameters(), ", ");
227             builder.append(">");
228         }
229         return builder.toString();
230     }
231 }
232