use of org.eclipse.jdt.core.dom.ParameterizedType in project AutoRefactor by JnRouvignac.
the class AbstractClassSubstituteCleanUp method substituteType.
/**
* Returns the substitute type or null if the class should be the same.
* @param origType The original type
* @param originalExpression The original expression
* @param classesToUseWithImport The classes that should be used with simple
* name.
* @param importsToAdd The imports that need to be added during this
* cleanup.
*
* @return the substitute type or null if the class should be the same.
*/
protected Type substituteType(final Type origType, final ASTNode originalExpression, final Set<String> classesToUseWithImport, final Set<String> importsToAdd) {
ASTNodeFactory ast = cuRewrite.getASTBuilder();
ITypeBinding origTypeBinding = origType.resolveBinding();
if (origTypeBinding == null) {
return null;
}
String origRawType = origTypeBinding.getErasure().getQualifiedName();
String substitutingClassName = getSubstitutingClassName(origRawType);
if (substitutingClassName != null) {
if (classesToUseWithImport.contains(substitutingClassName)) {
importsToAdd.add(substitutingClassName);
substitutingClassName = getSimpleName(substitutingClassName);
}
TypeNameDecider typeNameDecider = new TypeNameDecider(originalExpression);
if (origTypeBinding.isParameterizedType()) {
ITypeBinding[] origTypeArgs = origTypeBinding.getTypeArguments();
Type[] newTypes;
if (((ParameterizedType) origType).typeArguments().isEmpty()) {
newTypes = new Type[0];
} else {
newTypes = new Type[origTypeArgs.length];
for (int i = 0; i < origTypeArgs.length; i++) {
newTypes[i] = ast.toType(origTypeArgs[i], typeNameDecider);
}
}
return ast.newParameterizedType(substitutingClassName, newTypes);
}
return ast.type(substitutingClassName);
}
return null;
}
use of org.eclipse.jdt.core.dom.ParameterizedType in project flow by vaadin.
the class CodeTest method gwtGenerics.
private static void gwtGenerics(File file) throws IOException {
ASTParser parser = ASTParser.newParser(AST.JLS8);
String value = FileUtils.readFileToString(file, UTF_8);
parser.setSource(value.toCharArray());
parser.setKind(ASTParser.K_COMPILATION_UNIT);
final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
cu.accept(new ASTVisitor() {
Set<String> imports = new HashSet<>();
String packageName;
@Override
public boolean visit(PackageDeclaration node) {
packageName = node.getName().toString();
return false;
}
@Override
public boolean visit(ImportDeclaration node) {
imports.add(node.getName().toString());
return false;
}
@Override
public boolean visit(VariableDeclarationStatement node) {
for (Object frament : node.fragments()) {
if (frament instanceof VariableDeclarationFragment) {
VariableDeclarationFragment variableDeclaration = (VariableDeclarationFragment) frament;
Expression expression = variableDeclaration.getInitializer();
if (expression instanceof ClassInstanceCreation) {
ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation) expression;
Class<?> typeClass = getClass(node.getType());
Class<?> instanceClass = getClass(classInstanceCreation.getType());
if (typeClass != instanceClass && typeClass.isAssignableFrom(instanceClass)) {
fail("Variable type must be the specific implementation in " + node + " in " + file.getName());
}
}
}
}
return false;
}
private Class<?> getClass(Type type) {
if (type instanceof ArrayType) {
type = ((ArrayType) type).getElementType();
}
if (type instanceof ParameterizedType) {
type = ((ParameterizedType) type).getType();
}
String className = type.toString();
if (className.indexOf('.') == -1) {
String dotPrefix = '.' + className;
for (String i : imports) {
if (i.endsWith(dotPrefix)) {
className = i;
break;
}
}
}
Class<?> clas = getClass(className);
if (clas != null) {
return clas;
}
clas = getClass("java.lang." + className);
if (clas != null) {
return clas;
}
try {
String fileName = file.getName();
fileName = fileName.substring(0, fileName.lastIndexOf('.'));
if (fileName.equals(className)) {
return Class.forName(packageName + '.' + fileName);
}
clas = getClass(packageName + '.' + className);
if (clas != null) {
return clas;
}
return Class.forName(packageName + '.' + fileName + '$' + className);
} catch (ClassNotFoundException e) {
fail("Could not load class " + e);
return null;
}
}
private Class<?> getClass(String className) {
try {
return ClassUtils.getClass(className);
} catch (ClassNotFoundException e) {
return null;
}
}
});
}
use of org.eclipse.jdt.core.dom.ParameterizedType in project evosuite by EvoSuite.
the class TestExtractingVisitor method retrieveTypeClass.
/**
* <p>
* retrieveTypeClass
* </p>
*
* @param argument
* a {@link java.lang.Object} object.
* @return a {@link java.lang.Class} object.
*/
protected Class<?> retrieveTypeClass(Object argument) {
assert argument != null;
if (argument instanceof SimpleType) {
SimpleType simpleType = (SimpleType) argument;
return retrieveTypeClass(simpleType);
}
if (argument instanceof ITypeBinding) {
ITypeBinding binding = (ITypeBinding) argument;
return retrieveTypeClass(binding);
}
if (argument instanceof IVariableBinding) {
IVariableBinding variableBinding = (IVariableBinding) argument;
return retrieveTypeClass(variableBinding.getType());
}
if (argument instanceof SimpleName) {
SimpleName simpleName = (SimpleName) argument;
return retrieveTypeClass(simpleName.resolveBinding());
}
if (argument instanceof StringLiteral) {
return String.class;
}
if (argument instanceof NumberLiteral) {
return retrieveTypeClass((NumberLiteral) argument);
}
if (argument instanceof PrimitiveType) {
PrimitiveType primitiveType = (PrimitiveType) argument;
String typeCode = primitiveType.getPrimitiveTypeCode().toString();
Class<?> result = PRIMITIVE_TYPECODE_MAPPING.get(typeCode);
assert result != null : "Could not resolve typecode " + typeCode + ".";
return result;
}
if (argument instanceof ArrayType) {
ArrayType arrayType = (ArrayType) argument;
return retrieveTypeClass(arrayType);
}
if (argument instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) argument;
return retrieveTypeClass(parameterizedType.getType());
}
if (argument instanceof VariableDeclarationFragment) {
VariableDeclarationFragment varDeclFrgmnt = (VariableDeclarationFragment) argument;
return retrieveTypeClass(varDeclFrgmnt.resolveBinding());
}
if (argument instanceof InfixExpression) {
InfixExpression infixExpr = (InfixExpression) argument;
ITypeBinding refTypeBinding = infixExpr.resolveTypeBinding();
if (refTypeBinding != null) {
return retrieveTypeClass(refTypeBinding);
} else {
throw new RuntimeException("Could not determine type class of infix expression '" + infixExpr + "'.");
}
}
if (argument instanceof MethodInvocation) {
MethodInvocation methodInvocation = (MethodInvocation) argument;
ITypeBinding typeBinding = methodInvocation.resolveTypeBinding();
if (typeBinding != null) {
return retrieveTypeClass(typeBinding);
}
Expression typeExpression = methodInvocation.getExpression();
if (typeExpression instanceof MethodInvocation) {
MethodInvocation parentMethodInvocation = (MethodInvocation) typeExpression;
IMethodBinding parentMethodBinding = parentMethodInvocation.resolveMethodBinding();
return retrieveTypeClass(parentMethodBinding.getDeclaringClass());
} else {
return retrieveTypeClass(typeExpression);
}
}
if (argument instanceof ArrayAccess) {
ArrayAccess arrayAccess = (ArrayAccess) argument;
return retrieveTypeClass(arrayAccess.getArray());
}
if (argument instanceof Class<?>) {
return (Class<?>) argument;
}
if (argument instanceof ClassInstanceCreation) {
return retrieveTypeClass(((ClassInstanceCreation) argument).resolveTypeBinding());
}
if (argument instanceof BooleanLiteral) {
return Boolean.TYPE;
}
throw new UnsupportedOperationException("Retrieval of type " + argument.getClass() + " not implemented yet!");
}
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 bndtools by bndtools.
the class NewTypeWizardPage method superClassChanged.
/**
* Hook method that gets called when the superclass name has changed. The method validates the superclass name 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 superClassChanged() {
StatusInfo status = new StatusInfo();
IPackageFragmentRoot root = getPackageFragmentRoot();
fSuperClassDialogField.enableButton(root != null);
fSuperClassStubTypeContext = null;
String sclassName = getSuperClass();
if (sclassName.length() == 0) {
// accept the empty field (stands for java.lang.Object)
return status;
}
if (root != null) {
Type type = TypeContextChecker.parseSuperClass(sclassName);
if (type == null) {
status.setError(NewWizardMessages.NewTypeWizardPage_error_InvalidSuperClassName);
return status;
}
if (type instanceof ParameterizedType && !JavaModelUtil.is50OrHigher(root.getJavaProject())) {
status.setError(NewWizardMessages.NewTypeWizardPage_error_SuperClassNotParameterized);
return status;
}
} else {
// $NON-NLS-1$
status.setError("");
}
return status;
}
Aggregations