use of org.eclipse.jdt.core.dom.ParameterizedType in project bayou by capergroup.
the class Type method getConcretization.
public Type getConcretization(java.lang.reflect.Type type) {
AST ast = t.getAST();
if (type instanceof java.lang.reflect.ParameterizedType) {
// substitute generic names with their types (recursively)
java.lang.reflect.ParameterizedType pType = (java.lang.reflect.ParameterizedType) type;
java.lang.reflect.Type rawType_ = pType.getRawType();
org.eclipse.jdt.core.dom.Type rawType = ast.newSimpleType(ast.newName(((Class) rawType_).getCanonicalName()));
ParameterizedType retType = ast.newParameterizedType(rawType);
for (java.lang.reflect.Type arg : pType.getActualTypeArguments()) {
try {
org.eclipse.jdt.core.dom.Type argType = getConcretization(arg).T();
retType.typeArguments().add(ASTNode.copySubtree(ast, argType));
} catch (SynthesisException e) {
return new Type(rawType, (Class) rawType_);
}
}
return new Type(retType, (Class) rawType_);
} else if (type instanceof TypeVariable) {
// return the type the generic name was concretized to
String name = ((TypeVariable) type).getName();
// FIXME: Add support for wildcard types and concretizing without a base parameterized type (e.g., Collections)
if (concretization == null)
throw new SynthesisException(SynthesisException.InvalidKindOfType);
if (!concretization.containsKey(name))
throw new SynthesisException(SynthesisException.GenericTypeVariableMismatch);
return concretization.get(name);
} else if (type instanceof Class) {
Class cls = (Class) type;
if (cls.isArray()) {
if (// no support for multidim arrays
cls.getComponentType().isArray())
throw new SynthesisException(SynthesisException.InvalidKindOfType);
Type componentType = getConcretization(cls.getComponentType());
return new Type(ast.newArrayType(componentType.T(), 1), cls);
} else if (cls.isPrimitive()) {
return new Type(ast.newPrimitiveType(PrimitiveType.toCode(cls.getSimpleName())), cls);
} else {
// no generics, just return a simple type with the class
org.eclipse.jdt.core.dom.Type retType = ast.newSimpleType(ast.newName(cls.getCanonicalName()));
return new Type(retType, cls);
}
} else
throw new SynthesisException(SynthesisException.InvalidKindOfType);
}
use of org.eclipse.jdt.core.dom.ParameterizedType in project bayou by capergroup.
the class Type method releaseBinding.
// make a DOM type independent of its bindings, because when copying subtrees bindings don't copy over
org.eclipse.jdt.core.dom.Type releaseBinding(org.eclipse.jdt.core.dom.Type type, AST ast) {
ITypeBinding binding = type.resolveBinding();
if (type.isPrimitiveType())
return ast.newPrimitiveType(((PrimitiveType) type).getPrimitiveTypeCode());
else if (type.isSimpleType())
return ast.newSimpleType(ast.newName(binding.getQualifiedName()));
else if (binding.isParameterizedType()) {
ITypeBinding erasure = binding.getErasure();
SimpleType baseType = ast.newSimpleType(ast.newName(erasure.getQualifiedName()));
ParameterizedType retType = ast.newParameterizedType(baseType);
for (Object o : ((ParameterizedType) type).typeArguments()) {
org.eclipse.jdt.core.dom.Type arg = (org.eclipse.jdt.core.dom.Type) o;
org.eclipse.jdt.core.dom.Type argType = releaseBinding(arg, ast);
retType.typeArguments().add(argType);
}
return retType;
} else if (type.isArrayType()) {
ArrayType arrayType = (ArrayType) type;
org.eclipse.jdt.core.dom.Type elementType = releaseBinding(arrayType.getElementType(), ast);
int dimensions = arrayType.getDimensions();
return ast.newArrayType(elementType, dimensions);
} else {
throw new SynthesisException(SynthesisException.InvalidKindOfType);
}
}
Aggregations