use of org.eclipse.jdt.core.dom.ParameterizedType in project bndtools by bndtools.
the class NewTypeWizardPage method superInterfacesChanged.
/**
* Hook method that gets called when the list of super interface has changed. The method validates the super
* interfaces and returns the status of the validation.
* <p>
* Subclasses may extend this method to perform their own validation.
* </p>
*
* @return the status of the validation
*/
protected IStatus superInterfacesChanged() {
StatusInfo status = new StatusInfo();
IPackageFragmentRoot root = getPackageFragmentRoot();
fSuperInterfacesDialogField.enableButton(0, root != null);
if (root != null) {
List<InterfaceWrapper> elements = fSuperInterfacesDialogField.getElements();
int nElements = elements.size();
for (int i = 0; i < nElements; i++) {
String intfname = elements.get(i).interfaceName;
Type type = TypeContextChecker.parseSuperInterface(intfname);
if (type == null) {
status.setError(Messages.format(NewWizardMessages.NewTypeWizardPage_error_InvalidSuperInterfaceName, BasicElementLabels.getJavaElementName(intfname)));
return status;
}
if (type instanceof ParameterizedType && !JavaModelUtil.is50OrHigher(root.getJavaProject())) {
status.setError(Messages.format(NewWizardMessages.NewTypeWizardPage_error_SuperInterfaceNotParameterized, BasicElementLabels.getJavaElementName(intfname)));
return status;
}
}
}
return status;
}
use of org.eclipse.jdt.core.dom.ParameterizedType in project bayou by capergroup.
the class Type method isAssignableFrom.
// same semantics as Class.isAssignableFrom for our type system but with generics
// NOTE: assumes that the argument is a concretized type
public boolean isAssignableFrom(Type type) {
if (!ClassUtils.isAssignable(type.C(), this.C()))
return false;
if (// this type is not yet concretized or not parametric
t == null || !t.isParameterizedType())
return true;
if (!type.T().isParameterizedType())
return false;
// sanity check
ParameterizedType pt1 = (ParameterizedType) T();
ParameterizedType pt2 = (ParameterizedType) type.T();
int n1 = pt1.typeArguments().size();
int n2 = pt2.typeArguments().size();
if (n1 != n2)
throw new SynthesisException(SynthesisException.GenericTypeVariableMismatch);
for (int i = 0; i < n1; i++) {
Type t1 = new Type((org.eclipse.jdt.core.dom.Type) pt1.typeArguments().get(i));
Type t2 = new Type((org.eclipse.jdt.core.dom.Type) pt2.typeArguments().get(i));
// for example, a List<Dog> cannot be a List<Animal> even if Dog extends Animal
if (!t1.isInvariant(t2))
return false;
}
return true;
}
use of org.eclipse.jdt.core.dom.ParameterizedType in project bayou by capergroup.
the class Type method concretizeType.
public void concretizeType(Environment env) {
if (t != null)
return;
AST ast = env.ast;
if (c.isPrimitive())
t = ast.newPrimitiveType(PrimitiveType.toCode(c.getSimpleName()));
else if (c.isArray()) {
int dimensions = 0;
for (Class cls = c; cls.isArray(); cls = cls.getComponentType()) dimensions++;
Type componentType = new Type(c.getComponentType());
componentType.concretizeType(env);
t = ast.newArrayType(componentType.T(), dimensions);
} else if (// simple type
c.getTypeParameters().length == 0)
t = ast.newSimpleType(ast.newName(c.getCanonicalName()));
else {
// generic type
concretization = new HashMap<>();
org.eclipse.jdt.core.dom.Type rawType = ast.newSimpleType(ast.newName(c.getCanonicalName()));
t = ast.newParameterizedType(rawType);
// search for a type from the environment and add it as a concretization
for (TypeVariable tvar : c.getTypeParameters()) {
String name = tvar.getName();
Type type = env.searchType();
((ParameterizedType) t).typeArguments().add(ASTNode.copySubtree(ast, type.T()));
concretization.put(name, type);
}
}
}
use of org.eclipse.jdt.core.dom.ParameterizedType in project bayou by capergroup.
the class Type method autoConcretize.
private void autoConcretize() {
concretization = new HashMap<>();
// check sanity of types
if (!t.isParameterizedType()) {
if (c.getTypeParameters().length > 0) {
// commenting this check, as there are cases where synthesis succeeds even if there is a
// mismatch here (e.g., when there is a variable in scope that has already resolved the type).
// In other cases when this check would have failed, the failure would now occur elsewhere,
// typically in the getConcretization(..) case that handles TypeVariable.
// throw new SynthesisException(SynthesisException.GenericTypeVariableMismatch);
}
if (t.isArrayType() && !c.isArray())
throw new SynthesisException(SynthesisException.InvalidKindOfType);
return;
}
ParameterizedType pType = (ParameterizedType) t;
int n1 = pType.typeArguments().size();
int n2 = c.getTypeParameters().length;
if (n1 != n2)
throw new SynthesisException(SynthesisException.GenericTypeVariableMismatch);
// unify generic names with their actual types
for (int i = 0; i < n1; i++) {
String name = c.getTypeParameters()[i].getName();
Type type = new Type((org.eclipse.jdt.core.dom.Type) pType.typeArguments().get(i));
concretization.put(name, type);
}
}
use of org.eclipse.jdt.core.dom.ParameterizedType in project bayou by capergroup.
the class Type method simpleT.
/**
* Returns a simple representation of the current type (i.e., without fully qualified names)
*
* @param ast the AST node that should own the simple type
* @param env if provided (not null), then add imports of classes in generic type
* @return a DOM type representing the simple type
*/
public org.eclipse.jdt.core.dom.Type simpleT(AST ast, Environment env) {
if (t.isPrimitiveType())
return ast.newPrimitiveType(((PrimitiveType) t).getPrimitiveTypeCode());
if (t.isSimpleType() || t.isQualifiedType()) {
Name name = t.isSimpleType() ? ((SimpleType) t).getName() : ((QualifiedType) t).getName();
SimpleName simple;
if (name.isSimpleName())
simple = ast.newSimpleName(((SimpleName) name).getIdentifier());
else
simple = ast.newSimpleName(((QualifiedName) name).getName().getIdentifier());
return ast.newSimpleType(simple);
}
if (t.isParameterizedType()) {
org.eclipse.jdt.core.dom.Type baseType = ((ParameterizedType) t).getType();
Name name = baseType.isSimpleType() ? ((SimpleType) baseType).getName() : ((QualifiedType) baseType).getName();
SimpleName simple;
if (name.isSimpleName())
simple = ast.newSimpleName(((SimpleName) name).getIdentifier());
else
simple = ast.newSimpleName(((QualifiedName) name).getName().getIdentifier());
ParameterizedType pType = ast.newParameterizedType(ast.newSimpleType(simple));
for (Object o : ((ParameterizedType) t).typeArguments()) {
Type p = new Type((org.eclipse.jdt.core.dom.Type) o);
if (env != null)
env.addImport(p.C());
pType.typeArguments().add(p.simpleT(ast, env));
}
return pType;
}
if (t.isArrayType()) {
org.eclipse.jdt.core.dom.Type elementType = ((ArrayType) t).getElementType();
org.eclipse.jdt.core.dom.Type simpleElementType = new Type(elementType).simpleT(ast, env);
return ast.newArrayType((org.eclipse.jdt.core.dom.Type) ASTNode.copySubtree(ast, simpleElementType), ((ArrayType) t).getDimensions());
}
throw new SynthesisException(SynthesisException.InvalidKindOfType);
}
Aggregations