use of org.eclipse.jdt.core.dom.ITypeBinding in project che by eclipse.
the class UnresolvedElementsSubProcessor method addMissingCastParentsProposal.
private static void addMissingCastParentsProposal(ICompilationUnit cu, MethodInvocation invocationNode, Collection<ICommandAccess> proposals) {
Expression sender = invocationNode.getExpression();
if (sender instanceof ThisExpression) {
return;
}
ITypeBinding senderBinding = sender.resolveTypeBinding();
if (senderBinding == null || Modifier.isFinal(senderBinding.getModifiers())) {
return;
}
if (sender instanceof Name && ((Name) sender).resolveBinding() instanceof ITypeBinding) {
// static access
return;
}
ASTNode parent = invocationNode.getParent();
while (parent instanceof Expression && parent.getNodeType() != ASTNode.CAST_EXPRESSION) {
parent = parent.getParent();
}
boolean hasCastProposal = false;
if (parent instanceof CastExpression) {
// (TestCase) x.getName() -> ((TestCase) x).getName
hasCastProposal = useExistingParentCastProposal(cu, (CastExpression) parent, sender, invocationNode.getName(), getArgumentTypes(invocationNode.arguments()), proposals);
}
if (!hasCastProposal) {
// x.getName() -> ((TestCase) x).getName
Expression target = sender;
while (target instanceof ParenthesizedExpression) {
target = ((ParenthesizedExpression) target).getExpression();
}
String label;
if (target.getNodeType() != ASTNode.CAST_EXPRESSION) {
String targetName = null;
if (target.getLength() <= 18) {
targetName = ASTNodes.asString(target);
}
if (targetName == null) {
label = CorrectionMessages.UnresolvedElementsSubProcessor_methodtargetcast_description;
} else {
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_methodtargetcast2_description, BasicElementLabels.getJavaCodeString(targetName));
}
} else {
String targetName = null;
if (target.getLength() <= 18) {
targetName = ASTNodes.asString(((CastExpression) target).getExpression());
}
if (targetName == null) {
label = CorrectionMessages.UnresolvedElementsSubProcessor_changemethodtargetcast_description;
} else {
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changemethodtargetcast2_description, BasicElementLabels.getJavaCodeString(targetName));
}
}
proposals.add(new CastCorrectionProposal(label, cu, target, (ITypeBinding) null, IProposalRelevance.CHANGE_CAST));
}
}
use of org.eclipse.jdt.core.dom.ITypeBinding in project che by eclipse.
the class UnresolvedElementsSubProcessor method hasMethodWithName.
private static boolean hasMethodWithName(ITypeBinding typeBinding, String name) {
IVariableBinding[] fields = typeBinding.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
if (fields[i].getName().equals(name)) {
return true;
}
}
ITypeBinding superclass = typeBinding.getSuperclass();
if (superclass != null) {
return hasMethodWithName(superclass, name);
}
return false;
}
use of org.eclipse.jdt.core.dom.ITypeBinding in project flux by eclipse.
the class ASTNodeFactory method newCreationType.
public static Type newCreationType(AST ast, ITypeBinding typeBinding, ImportRewrite importRewrite, ImportRewriteContext importContext) {
if (typeBinding.isParameterizedType()) {
Type baseType = newCreationType(ast, typeBinding.getTypeDeclaration(), importRewrite, importContext);
ParameterizedType parameterizedType = ast.newParameterizedType(baseType);
for (ITypeBinding typeArgument : typeBinding.getTypeArguments()) {
parameterizedType.typeArguments().add(newCreationType(ast, typeArgument, importRewrite, importContext));
}
return parameterizedType;
} else if (typeBinding.isParameterizedType()) {
Type elementType = newCreationType(ast, typeBinding.getElementType(), importRewrite, importContext);
ArrayType arrayType = ast.newArrayType(elementType, 0);
while (typeBinding.isArray()) {
Dimension dimension = ast.newDimension();
IAnnotationBinding[] typeAnnotations = typeBinding.getTypeAnnotations();
for (IAnnotationBinding typeAnnotation : typeAnnotations) {
dimension.annotations().add(importRewrite.addAnnotation(typeAnnotation, ast, importContext));
}
arrayType.dimensions().add(dimension);
typeBinding = typeBinding.getComponentType();
}
return arrayType;
} else if (typeBinding.isWildcardType()) {
ITypeBinding bound = typeBinding.getBound();
typeBinding = (bound != null) ? bound : typeBinding.getErasure();
return newCreationType(ast, typeBinding, importRewrite, importContext);
} else {
return importRewrite.addImport(typeBinding, ast, importContext);
}
}
use of org.eclipse.jdt.core.dom.ITypeBinding in project flux by eclipse.
the class ASTNodeFactory method newType.
/**
* Returns the new type node corresponding to the type of the given declaration
* including the extra dimensions. If the type is a {@link UnionType}, use the LUB type.
* If the <code>importRewrite</code> is <code>null</code>, the type may be fully-qualified.
*
* @param ast The AST to create the resulting type with.
* @param declaration The variable declaration to get the type from
* @param importRewrite the import rewrite to use, or <code>null</code>
* @param context the import rewrite context, or <code>null</code>
* @return a new type node created with the given AST.
*
* @since 3.7.1
*/
public static Type newType(AST ast, VariableDeclaration declaration, ImportRewrite importRewrite, ImportRewriteContext context) {
if (declaration instanceof VariableDeclarationFragment && declaration.getParent() instanceof LambdaExpression) {
return newType((LambdaExpression) declaration.getParent(), (VariableDeclarationFragment) declaration, ast, importRewrite, context);
}
Type type = ASTNodes.getType(declaration);
if (declaration instanceof SingleVariableDeclaration) {
Type type2 = ((SingleVariableDeclaration) declaration).getType();
if (type2 instanceof UnionType) {
ITypeBinding typeBinding = type2.resolveBinding();
if (typeBinding != null) {
if (importRewrite != null) {
type = importRewrite.addImport(typeBinding, ast, context);
return type;
} else {
String qualifiedName = typeBinding.getQualifiedName();
if (qualifiedName.length() > 0) {
type = ast.newSimpleType(ast.newName(qualifiedName));
return type;
}
}
}
// XXX: fallback for intersection types or unresolved types: take first type of union
type = (Type) ((UnionType) type2).types().get(0);
return type;
}
}
type = (Type) ASTNode.copySubtree(ast, type);
List<Dimension> extraDimensions = declaration.extraDimensions();
if (!extraDimensions.isEmpty()) {
ArrayType arrayType;
if (type instanceof ArrayType) {
arrayType = (ArrayType) type;
} else {
arrayType = ast.newArrayType(type, 0);
type = arrayType;
}
arrayType.dimensions().addAll(ASTNode.copySubtrees(ast, extraDimensions));
}
return type;
}
use of org.eclipse.jdt.core.dom.ITypeBinding in project flux by eclipse.
the class ASTNodes method getReceiverTypeBinding.
/**
* Returns the receiver's type binding of the given method invocation.
*
* @param invocation method invocation to resolve type of
* @return the type binding of the receiver
*/
public static ITypeBinding getReceiverTypeBinding(MethodInvocation invocation) {
ITypeBinding result = null;
Expression exp = invocation.getExpression();
if (exp != null) {
return exp.resolveTypeBinding();
} else {
AbstractTypeDeclaration type = (AbstractTypeDeclaration) getParent(invocation, AbstractTypeDeclaration.class);
if (type != null)
return type.resolveBinding();
}
return result;
}
Aggregations