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.util;
18 
19 import signature.model.IAnnotatableElement;
20 import signature.model.IAnnotation;
21 import signature.model.IAnnotationElement;
22 import signature.model.IAnnotationField;
23 import signature.model.IApi;
24 import signature.model.IClassDefinition;
25 import signature.model.IField;
26 import signature.model.IPackage;
27 import signature.model.ITypeReference;
28 
29 import java.util.Collection;
30 
31 public class ModelUtil {
ModelUtil()32     private ModelUtil() {
33     }
34 
35     /**
36      * Returns the IClass for the given className.<br>
37      * Format: a.b.C
38      *
39      * @param qualifiedClassName
40      *            the fully qualified class name
41      * @return the IClass instance or null;
42      */
getClass(IPackage aPackage, String qualifiedClassName)43     public static IClassDefinition getClass(IPackage aPackage,
44             String qualifiedClassName) {
45         for (IClassDefinition clazz : aPackage.getClasses()) {
46             if (qualifiedClassName.equals(clazz.getName())) {
47                 return clazz;
48             }
49         }
50         return null;
51     }
52 
getAnnotation(IAnnotatableElement element, String qualifiedTypeName)53     public static IAnnotation getAnnotation(IAnnotatableElement element,
54             String qualifiedTypeName) {
55         for (IAnnotation annotation : element.getAnnotations()) {
56             if (qualifiedTypeName.equals(annotation.getType()
57                     .getClassDefinition().getQualifiedName())) {
58                 return annotation;
59             }
60         }
61         return null;
62     }
63 
getAnnotationElement( IAnnotation annotation, String elementName)64     public static IAnnotationElement getAnnotationElement(
65             IAnnotation annotation, String elementName) {
66         for (IAnnotationElement element : annotation.getElements()) {
67             if (elementName.equals(element.getDeclaringField().getName())) {
68                 return element;
69             }
70         }
71         return null;
72     }
73 
getField(IClassDefinition clazz, String fieldName)74     public static IField getField(IClassDefinition clazz, String fieldName) {
75         for (IField field : clazz.getFields()) {
76             if (fieldName.equals(field.getName())) {
77                 return field;
78             }
79         }
80         return null;
81     }
82 
getAnnotationField( IClassDefinition annotation, String fieldName)83     public static IAnnotationField getAnnotationField(
84             IClassDefinition annotation, String fieldName) {
85         for (IAnnotationField field : annotation.getAnnotationFields()) {
86             if (fieldName.equals(field.getName())) {
87                 return field;
88             }
89         }
90         return null;
91     }
92 
93     /**
94      * Returns the IPackage for the given className.<br>
95      * Format: a.b
96      *
97      * @param api
98      *            the api
99      * @param packageName
100      *            the name of the package
101      * @return the IClass instance or null;
102      */
getPackage(IApi api, String packageName)103     public static IPackage getPackage(IApi api, String packageName) {
104         for (IPackage aPackage : api.getPackages()) {
105             if (packageName.equals(aPackage.getName())) {
106                 return aPackage;
107             }
108         }
109         return null;
110     }
111 
112     /**
113      * "a.b.c.A;" -> "a.b.c" "A" -> "" empty string
114      *
115      * @param classIdentifier
116      * @return the package name
117      */
getPackageName(String classIdentifier)118     public static String getPackageName(String classIdentifier) {
119         int lastIndexOfSlash = classIdentifier.lastIndexOf('.');
120         String packageName = null;
121         if (lastIndexOfSlash == -1) {
122             packageName = "";
123         } else {
124             packageName = classIdentifier.substring(0, lastIndexOfSlash);
125         }
126         return packageName;
127     }
128 
129     /**
130      * "a.b.c.A;" -> "A" "A" -> "A"
131      *
132      * @param classIdentifier
133      *            fully qualified class name
134      * @return the class name
135      */
getClassName(String classIdentifier)136     public static String getClassName(String classIdentifier) {
137         int lastIndexOfDot = classIdentifier.lastIndexOf('.');
138         String className = null;
139         if (lastIndexOfDot == -1) {
140             className = classIdentifier;
141         } else {
142             className = classIdentifier.substring(lastIndexOfDot + 1);
143         }
144         return className;
145     }
146 
147 
separate(Collection<? extends Object> elements, String separator)148     public static String separate(Collection<? extends Object> elements,
149             String separator) {
150         StringBuilder s = new StringBuilder();
151         boolean first = true;
152         for (Object object : elements) {
153             if (!first) {
154                 s.append(separator);
155             }
156             s.append(object.toString());
157             first = false;
158         }
159         return s.toString();
160     }
161 
isJavaLangObject(ITypeReference type)162     public static boolean isJavaLangObject(ITypeReference type) {
163         if (type instanceof IClassDefinition) {
164             IClassDefinition clazz = (IClassDefinition) type;
165             if ("java.lang".equals(clazz.getPackageName())) {
166                 return "Object".equals(clazz.getName());
167             }
168         }
169         return false;
170     }
171 
172 }
173