|
|
Re: working with the AST [message #639436 is a reply to message #639099] |
Tue, 16 November 2010 14:35 |
Jochen Huck Messages: 8 Registered: August 2010 |
Junior Member |
|
|
Hi,
that's exactly what I actually did. I just wonder why this functionallity isn't already implemented. I would have expected that it is possible to say ITypeBinding bind; Type type = bind.getType() or something like that. Here is the code that does the trick:
public static Type getTypeFormITypeBinding(ITypeBinding binding, AST ast) {
Type type = null;
if (binding.isPrimitive()) {
PrimitiveType.Code code = null;
if (binding.getName().equals("byte")) {
code = PrimitiveType.BYTE;
}
if (binding.getName().equals("short")) {
code = PrimitiveType.SHORT;
}
if (binding.getName().equals("char")) {
code = PrimitiveType.CHAR;
}
if (binding.getName().equals("int")) {
code = PrimitiveType.INT;
}
if (binding.getName().equals("long")) {
code = PrimitiveType.LONG;
}
if (binding.getName().equals("float")) {
code = PrimitiveType.FLOAT;
}
if (binding.getName().equals("double")) {
code = PrimitiveType.DOUBLE;
}
if (binding.getName().equals("boolean")) {
code = PrimitiveType.BOOLEAN;
}
type = ast.newPrimitiveType(code);
}
if (binding.isClass()) {
type = getSimpleTypeFromString(binding.getQualifiedName(), ast);
}
if (binding.isArray()) {
Type elementType = getTypeFormITypeBinding(binding.getElementType(), ast);
type = ast.newArrayType(elementType, binding.getDimensions());
}
if (binding.isWildcardType()) {
// TODO
}
if (binding.isParameterizedType()) {
// TODO
}
return type;
}
public static SimpleType getSimpleTypeFromString(String canonicalName, AST ast) {
String[] names = canonicalName.split("\\.");
SimpleName first = ast.newSimpleName(names[0]);
QualifiedName qName = null;
for (int i = 1; i < names.length; i++) {
SimpleName second = ast.newSimpleName(names[i]);
if (qName == null) {
qName = ast.newQualifiedName(first, second);
} else {
qName = ast.newQualifiedName(qName, second);
}
}
if (qName == null) {
return ast.newSimpleType(first);
} else {
return ast.newSimpleType(qName);
}
}
[Updated on: Tue, 16 November 2010 14:36] Report message to a moderator
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.01928 seconds