| 1 |
/* |
| 2 |
* JBoss, Home of Professional Open Source. |
| 3 |
* |
| 4 |
* Copyright 2012 Red Hat, Inc. |
| 5 |
* |
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
* you may not use this file except in compliance with the License. |
| 8 |
* You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
*/ |
| 18 |
package org.jboss.classfilewriter.util; |
| 19 |
|
| 20 |
import java.lang.reflect.ParameterizedType; |
| 21 |
import java.lang.reflect.Type; |
| 22 |
import java.lang.reflect.WildcardType; |
| 23 |
|
| 24 |
/** |
| 25 |
* Generates the contents of a Signature attribute from a java type. |
| 26 |
* <p> |
| 27 |
* TODO: this is all wrong, I'm to tired to read the pathetic excuse for a spec properly |
| 28 |
* |
| 29 |
* @author Stuart Douglas |
| 30 |
* |
| 31 |
*/ |
| 32 |
public class SignatureBuilder { |
| 33 |
|
| 34 |
/** |
| 35 |
* Generates the field signiture for a field of the given type |
| 36 |
* |
| 37 |
* @param type |
| 38 |
* @return the signiture, or null if no signure is required (e.g. for Class types) |
| 39 |
*/ |
| 40 |
public static String fieldAttribute(Type type) { |
| 41 |
StringBuilder builder = new StringBuilder(); |
| 42 |
fieldAttribute(type, builder); |
| 43 |
return builder.toString(); |
| 44 |
|
| 45 |
} |
| 46 |
|
| 47 |
private static void fieldAttribute(Type type, StringBuilder builder) { |
| 48 |
if (type instanceof Class<?>) { |
| 49 |
classType((Class<?>) type, builder); |
| 50 |
} else if (type instanceof ParameterizedType) { |
| 51 |
ParameterizedType ptype = (ParameterizedType) type; |
| 52 |
parametizedType(ptype, builder); |
| 53 |
} else if (type instanceof WildcardType) { |
| 54 |
WildcardType ptype = (WildcardType) type; |
| 55 |
wildcardType(ptype, builder); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
private static void wildcardType(WildcardType type, StringBuilder builder) { |
| 60 |
// WRONG |
| 61 |
builder.append('*'); |
| 62 |
} |
| 63 |
|
| 64 |
public static void parametizedType(ParameterizedType type, StringBuilder builder) { |
| 65 |
fieldAttribute(type.getRawType(), builder); //write the owner type |
| 66 |
//now write the type arguments |
| 67 |
builder.append('<'); |
| 68 |
for(Type t : type.getActualTypeArguments()) { |
| 69 |
fieldAttribute(t, builder); |
| 70 |
builder.append(';'); |
| 71 |
} |
| 72 |
builder.append(">;"); |
| 73 |
} |
| 74 |
|
| 75 |
private static void classType(Class<?> clazz, StringBuilder builder) { |
| 76 |
if (clazz.isMemberClass()) { |
| 77 |
classType(clazz.getDeclaringClass(), builder); |
| 78 |
builder.append('.'); |
| 79 |
builder.append(clazz.getSimpleName()); |
| 80 |
} else { |
| 81 |
builder.append("L"); |
| 82 |
builder.append(clazz.getName().replace('.', '/')); |
| 83 |
} |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
private SignatureBuilder() { |
| 88 |
} |
| 89 |
} |