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 net.entropysoft.transmorph.signature.formatter.ClassFileTypeSignatureFormatter;
19  
20  /**
21   * Signature for a type argument
22   * 
23   * This class is thread-safe
24   * 
25   * @author Cedric Chabanois (cchabanois at gmail.com)
26   * 
27   */
28  public class TypeArgSignature extends TypeSignature {
29  
30  	public static char NO_WILDCARD = ' '; // <Type>
31  	public static char UNBOUNDED_WILDCARD = '*'; // <?>
32  	public static char UPPERBOUND_WILDCARD = '+'; // <N extends Type>
33  	public static char LOWERBOUND_WILDCARD = '-'; // <N super Type>
34  	private final char wildcard;
35  	private final FieldTypeSignature fieldTypeSignature;
36  	private volatile String signature;
37  
38  	public TypeArgSignature(char wildcard, FieldTypeSignature fieldTypeSignature) {
39  		this.wildcard = wildcard;
40  		this.fieldTypeSignature = fieldTypeSignature;
41  	}
42  
43  	public char getWildcard() {
44  		return wildcard;
45  	}
46  
47  	/**
48  	 * get the signature of the type. Null if unbounded
49  	 * 
50  	 * @return
51  	 */
52  	public FieldTypeSignature getFieldTypeSignature() {
53  		return fieldTypeSignature;
54  	}
55  
56  	public String getSignature() {
57  		if (signature == null) {
58  			ClassFileTypeSignatureFormatter typeSignatureFormatter = new ClassFileTypeSignatureFormatter();
59  			signature = typeSignatureFormatter.formatTypeArgSignature(this);
60  		}
61  		return signature;
62  	}
63  
64  	@Override
65  	public String toString() {
66  		return getSignature();
67  	}
68  
69  	@Override
70  	public boolean isTypeArgument() {
71  		return true;
72  	}
73  
74  	@Override
75  	public FullTypeSignature getTypeErasureSignature() {
76  		if (wildcard == NO_WILDCARD)
77  			return getFieldTypeSignature().getTypeErasureSignature();
78  		if (wildcard == UNBOUNDED_WILDCARD)
79  			return TypeSignatureFactory.getTypeSignature(Object.class);
80  		if (wildcard == UPPERBOUND_WILDCARD)
81  			return getFieldTypeSignature().getTypeErasureSignature();
82  		if (wildcard == LOWERBOUND_WILDCARD)
83  			return TypeSignatureFactory.getTypeSignature(Object.class);
84  		return null;
85  	}
86  
87  }