View Javadoc

1   /*
2    * Copyright 2008-2009 the original author or authors.
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  package net.entropysoft.transmorph.signature;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  /**
22   * signature for a class type
23   * 
24   * This class is thread-safe
25   * 
26   * @author Cedric Chabanois (cchabanois at gmail.com)
27   * 
28   */
29  public class ClassTypeSignature extends FieldTypeSignature {
30  
31  	private final String binaryName;
32  	private final TypeArgSignature[] typeArgSignatures;
33  	private final ClassTypeSignature ownerTypeSignature;
34  	private volatile FullTypeSignature typeErasureSignature;
35  
36  	/**
37  	 * Creates a Class type signature
38  	 * 
39  	 * @param binaryName
40  	 *            the fqn of the class if not an inner class or the simple name
41  	 *            if it is an inner class
42  	 * @param typeArgSignatures
43  	 *            the type arg signatures or an empty array of not a
44  	 *            parameterized class type
45  	 * @param ownerTypeSignature
46  	 *            the owner type signature if class is an inner class or null if
47  	 *            not
48  	 */
49  	public ClassTypeSignature(String binaryName,
50  			TypeArgSignature[] typeArgSignatures,
51  			ClassTypeSignature ownerTypeSignature) {
52  		this.binaryName = binaryName;
53  		this.typeArgSignatures = typeArgSignatures;
54  		this.ownerTypeSignature = ownerTypeSignature;
55  	}
56  
57  	/**
58  	 * Returns true if is an inner class signature
59  	 * 
60  	 * @return
61  	 */
62  	public boolean isInnerClass() {
63  		return ownerTypeSignature != null;
64  	}
65  
66  	/**
67  	 * the fqn of the class if not an inner class or the simple name if it is an
68  	 * inner class
69  	 * 
70  	 * @return
71  	 */
72  	public String getBinaryName() {
73  		return binaryName;
74  	}
75  
76  	/**
77  	 * get the fully qualified class name ('$' is used to separate inner
78  	 * classes)
79  	 * 
80  	 * @return
81  	 */
82  	public String getClassName() {
83  		if (ownerTypeSignature == null) {
84  			return binaryName;
85  		}
86  		return ownerTypeSignature.getClassName() + '$' + binaryName;
87  	}
88  
89  	public String getInnerClassName() {
90  		if (ownerTypeSignature == null) {
91  			return null;
92  		}
93  		return binaryName;
94  	}
95  
96  	/**
97  	 * get the owner type signature
98  	 * 
99  	 * @return the owner type signature or null if not an inner class
100 	 */
101 	public ClassTypeSignature getOwnerTypeSignature() {
102 		return ownerTypeSignature;
103 	}
104 
105 	/**
106 	 * get the type arguments signatures
107 	 * 
108 	 * @return the type arguments signatures or an empty array if not
109 	 *         parameterized
110 	 */
111 	public TypeArgSignature[] getTypeArgSignatures() {
112 		return typeArgSignatures;
113 	}
114 
115 	/**
116 	 * get the type erasure signature
117 	 */
118 	public FullTypeSignature getTypeErasureSignature() {
119 		if (typeErasureSignature == null) {
120 			typeErasureSignature = new ClassTypeSignature(binaryName,
121 					new TypeArgSignature[0], ownerTypeSignature == null ? null
122 							: (ClassTypeSignature) ownerTypeSignature
123 									.getTypeErasureSignature());
124 		}
125 		return typeErasureSignature;
126 	}
127 
128 	private ClassTypeSignature[] getClassTypeSignatures() {
129 		List<ClassTypeSignature> list = new ArrayList<ClassTypeSignature>();
130 
131 		ClassTypeSignature classTypeSignature = this;
132 		while (classTypeSignature != null) {
133 			list.add(0, classTypeSignature);
134 			classTypeSignature = classTypeSignature.ownerTypeSignature;
135 		}
136 		return list.toArray(new ClassTypeSignature[list.size()]);
137 	}
138 
139 	@Override
140 	public String toString() {
141 		return getSignature();
142 	}
143 
144 	public boolean isClassType() {
145 		return true;
146 	}
147 
148 }