use of org.eclipse.jdt.core.dom.ClassInstanceCreation in project flux by eclipse.
the class ASTResolving method getPossibleReferenceBinding.
private static ITypeBinding getPossibleReferenceBinding(ASTNode node) {
ASTNode parent = node.getParent();
switch(parent.getNodeType()) {
case ASTNode.ASSIGNMENT:
Assignment assignment = (Assignment) parent;
if (node.equals(assignment.getLeftHandSide())) {
// field write access: xx= expression
return assignment.getRightHandSide().resolveTypeBinding();
}
// read access
return assignment.getLeftHandSide().resolveTypeBinding();
case ASTNode.INFIX_EXPRESSION:
InfixExpression infix = (InfixExpression) parent;
InfixExpression.Operator op = infix.getOperator();
if (op == InfixExpression.Operator.CONDITIONAL_AND || op == InfixExpression.Operator.CONDITIONAL_OR) {
//$NON-NLS-1$
return infix.getAST().resolveWellKnownType("boolean");
} else if (op == InfixExpression.Operator.LEFT_SHIFT || op == InfixExpression.Operator.RIGHT_SHIFT_UNSIGNED || op == InfixExpression.Operator.RIGHT_SHIFT_SIGNED) {
//$NON-NLS-1$
return infix.getAST().resolveWellKnownType("int");
}
if (node.equals(infix.getLeftOperand())) {
// xx operation expression
ITypeBinding rigthHandBinding = infix.getRightOperand().resolveTypeBinding();
if (rigthHandBinding != null) {
return rigthHandBinding;
}
} else {
// expression operation xx
ITypeBinding leftHandBinding = infix.getLeftOperand().resolveTypeBinding();
if (leftHandBinding != null) {
return leftHandBinding;
}
}
if (op != InfixExpression.Operator.EQUALS && op != InfixExpression.Operator.NOT_EQUALS) {
//$NON-NLS-1$
return infix.getAST().resolveWellKnownType("int");
}
break;
case ASTNode.INSTANCEOF_EXPRESSION:
InstanceofExpression instanceofExpression = (InstanceofExpression) parent;
return instanceofExpression.getRightOperand().resolveBinding();
case ASTNode.VARIABLE_DECLARATION_FRAGMENT:
VariableDeclarationFragment frag = (VariableDeclarationFragment) parent;
if (frag.getInitializer().equals(node)) {
return frag.getName().resolveTypeBinding();
}
break;
case ASTNode.SUPER_METHOD_INVOCATION:
SuperMethodInvocation superMethodInvocation = (SuperMethodInvocation) parent;
IMethodBinding superMethodBinding = ASTNodes.getMethodBinding(superMethodInvocation.getName());
if (superMethodBinding != null) {
return getParameterTypeBinding(node, superMethodInvocation.arguments(), superMethodBinding);
}
break;
case ASTNode.METHOD_INVOCATION:
MethodInvocation methodInvocation = (MethodInvocation) parent;
IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
if (methodBinding != null) {
return getParameterTypeBinding(node, methodInvocation.arguments(), methodBinding);
}
break;
case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
{
SuperConstructorInvocation superInvocation = (SuperConstructorInvocation) parent;
IMethodBinding superBinding = superInvocation.resolveConstructorBinding();
if (superBinding != null) {
return getParameterTypeBinding(node, superInvocation.arguments(), superBinding);
}
break;
}
case ASTNode.CONSTRUCTOR_INVOCATION:
{
ConstructorInvocation constrInvocation = (ConstructorInvocation) parent;
IMethodBinding constrBinding = constrInvocation.resolveConstructorBinding();
if (constrBinding != null) {
return getParameterTypeBinding(node, constrInvocation.arguments(), constrBinding);
}
break;
}
case ASTNode.CLASS_INSTANCE_CREATION:
{
ClassInstanceCreation creation = (ClassInstanceCreation) parent;
IMethodBinding creationBinding = creation.resolveConstructorBinding();
if (creationBinding != null) {
return getParameterTypeBinding(node, creation.arguments(), creationBinding);
}
break;
}
case ASTNode.PARENTHESIZED_EXPRESSION:
return guessBindingForReference(parent);
case ASTNode.ARRAY_ACCESS:
if (((ArrayAccess) parent).getIndex().equals(node)) {
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("int");
} else {
ITypeBinding parentBinding = getPossibleReferenceBinding(parent);
if (parentBinding == null) {
//$NON-NLS-1$
parentBinding = parent.getAST().resolveWellKnownType("java.lang.Object");
}
return parentBinding.createArrayType(1);
}
case ASTNode.ARRAY_CREATION:
if (((ArrayCreation) parent).dimensions().contains(node)) {
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("int");
}
break;
case ASTNode.ARRAY_INITIALIZER:
ASTNode initializerParent = parent.getParent();
int dim = 1;
while (initializerParent instanceof ArrayInitializer) {
initializerParent = initializerParent.getParent();
dim++;
}
Type creationType = null;
if (initializerParent instanceof ArrayCreation) {
creationType = ((ArrayCreation) initializerParent).getType();
} else if (initializerParent instanceof VariableDeclaration) {
VariableDeclaration varDecl = (VariableDeclaration) initializerParent;
creationType = ASTNodes.getType(varDecl);
dim -= varDecl.getExtraDimensions();
} else if (initializerParent instanceof MemberValuePair) {
String name = ((MemberValuePair) initializerParent).getName().getIdentifier();
IMethodBinding annotMember = findAnnotationMember((Annotation) initializerParent.getParent(), name);
if (annotMember != null) {
return getReducedDimensionBinding(annotMember.getReturnType(), dim);
}
}
if (creationType instanceof ArrayType) {
ITypeBinding creationTypeBinding = ((ArrayType) creationType).resolveBinding();
if (creationTypeBinding != null) {
return Bindings.getComponentType(creationTypeBinding, dim);
}
}
break;
case ASTNode.CONDITIONAL_EXPRESSION:
ConditionalExpression expression = (ConditionalExpression) parent;
if (node.equals(expression.getExpression())) {
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("boolean");
}
if (node.equals(expression.getElseExpression())) {
return expression.getThenExpression().resolveTypeBinding();
}
return expression.getElseExpression().resolveTypeBinding();
case ASTNode.POSTFIX_EXPRESSION:
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("int");
case ASTNode.PREFIX_EXPRESSION:
if (((PrefixExpression) parent).getOperator() == PrefixExpression.Operator.NOT) {
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("boolean");
}
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("int");
case ASTNode.IF_STATEMENT:
case ASTNode.WHILE_STATEMENT:
case ASTNode.DO_STATEMENT:
if (node instanceof Expression) {
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("boolean");
}
break;
case ASTNode.SWITCH_STATEMENT:
if (((SwitchStatement) parent).getExpression().equals(node)) {
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("int");
}
break;
case ASTNode.RETURN_STATEMENT:
MethodDeclaration decl = ASTResolving.findParentMethodDeclaration(parent);
if (decl != null && !decl.isConstructor()) {
return decl.getReturnType2().resolveBinding();
}
LambdaExpression lambdaExpr = ASTResolving.findEnclosingLambdaExpression(parent);
if (lambdaExpr != null) {
IMethodBinding lambdaMethodBinding = lambdaExpr.resolveMethodBinding();
if (lambdaMethodBinding != null && lambdaMethodBinding.getReturnType() != null) {
return lambdaMethodBinding.getReturnType();
}
}
break;
case ASTNode.CAST_EXPRESSION:
return ((CastExpression) parent).getType().resolveBinding();
case ASTNode.THROW_STATEMENT:
case ASTNode.CATCH_CLAUSE:
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("java.lang.Exception");
case ASTNode.FIELD_ACCESS:
if (node.equals(((FieldAccess) parent).getName())) {
return getPossibleReferenceBinding(parent);
}
break;
case ASTNode.SUPER_FIELD_ACCESS:
return getPossibleReferenceBinding(parent);
case ASTNode.QUALIFIED_NAME:
if (node.equals(((QualifiedName) parent).getName())) {
return getPossibleReferenceBinding(parent);
}
break;
case ASTNode.SWITCH_CASE:
if (node.equals(((SwitchCase) parent).getExpression()) && parent.getParent() instanceof SwitchStatement) {
return ((SwitchStatement) parent.getParent()).getExpression().resolveTypeBinding();
}
break;
case ASTNode.ASSERT_STATEMENT:
if (node.getLocationInParent() == AssertStatement.EXPRESSION_PROPERTY) {
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("boolean");
}
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("java.lang.String");
case ASTNode.SINGLE_MEMBER_ANNOTATION:
{
//$NON-NLS-1$
IMethodBinding annotMember = findAnnotationMember((Annotation) parent, "value");
if (annotMember != null) {
return annotMember.getReturnType();
}
break;
}
case ASTNode.MEMBER_VALUE_PAIR:
{
String name = ((MemberValuePair) parent).getName().getIdentifier();
IMethodBinding annotMember = findAnnotationMember((Annotation) parent.getParent(), name);
if (annotMember != null) {
return annotMember.getReturnType();
}
break;
}
default:
}
return null;
}
use of org.eclipse.jdt.core.dom.ClassInstanceCreation in project flux by eclipse.
the class QuickAssistProcessor method getConvertAnonymousToNestedProposal.
private static boolean getConvertAnonymousToNestedProposal(IInvocationContext context, final ASTNode node, Collection<ICommandAccess> proposals) throws CoreException {
if (!(node instanceof Name))
return false;
ASTNode normalized = ASTNodes.getNormalizedNode(node);
if (normalized.getLocationInParent() != ClassInstanceCreation.TYPE_PROPERTY)
return false;
final AnonymousClassDeclaration anonymTypeDecl = ((ClassInstanceCreation) normalized.getParent()).getAnonymousClassDeclaration();
if (anonymTypeDecl == null || anonymTypeDecl.resolveBinding() == null) {
return false;
}
if (proposals == null) {
return true;
}
final ICompilationUnit cu = context.getCompilationUnit();
//final ConvertAnonymousToNestedRefactoring refactoring= new ConvertAnonymousToNestedRefactoring(anonymTypeDecl);
String extTypeName = ASTNodes.getSimpleNameIdentifier((Name) node);
ITypeBinding anonymTypeBinding = anonymTypeDecl.resolveBinding();
String className;
if (anonymTypeBinding.getInterfaces().length == 0) {
className = Messages.format(CorrectionMessages.QuickAssistProcessor_name_extension_from_interface, extTypeName);
} else {
className = Messages.format(CorrectionMessages.QuickAssistProcessor_name_extension_from_class, extTypeName);
}
String[][] existingTypes = ((IType) anonymTypeBinding.getJavaElement()).resolveType(className);
int i = 1;
while (existingTypes != null) {
i++;
existingTypes = ((IType) anonymTypeBinding.getJavaElement()).resolveType(className + i);
}
// }
return false;
}
use of org.eclipse.jdt.core.dom.ClassInstanceCreation in project che by eclipse.
the class TypeContextChecker method fillWithTypeStubs.
private static void fillWithTypeStubs(final StringBuffer bufBefore, final StringBuffer bufAfter, final int focalPosition, List<? extends BodyDeclaration> types) {
StringBuffer buf;
for (Iterator<? extends BodyDeclaration> iter = types.iterator(); iter.hasNext(); ) {
BodyDeclaration bodyDeclaration = iter.next();
if (!(bodyDeclaration instanceof AbstractTypeDeclaration)) {
//account for local classes:
if (!(bodyDeclaration instanceof MethodDeclaration))
continue;
int bodyStart = bodyDeclaration.getStartPosition();
int bodyEnd = bodyDeclaration.getStartPosition() + bodyDeclaration.getLength();
if (!(bodyStart < focalPosition && focalPosition < bodyEnd))
continue;
MethodDeclaration methodDeclaration = (MethodDeclaration) bodyDeclaration;
buf = bufBefore;
appendModifiers(buf, methodDeclaration.modifiers());
appendTypeParameters(buf, methodDeclaration.typeParameters());
//$NON-NLS-1$
buf.append(" void ");
buf.append(methodDeclaration.getName().getIdentifier());
//$NON-NLS-1$
buf.append("(){\n");
Block body = methodDeclaration.getBody();
body.accept(new HierarchicalASTVisitor() {
@Override
public boolean visit(AbstractTypeDeclaration node) {
fillWithTypeStubs(bufBefore, bufAfter, focalPosition, Collections.singletonList(node));
return false;
}
@Override
public boolean visit(ClassInstanceCreation node) {
AnonymousClassDeclaration anonDecl = node.getAnonymousClassDeclaration();
if (anonDecl == null)
// could be in CIC parameter list
return true;
int anonStart = anonDecl.getStartPosition();
int anonEnd = anonDecl.getStartPosition() + anonDecl.getLength();
if (!(anonStart < focalPosition && focalPosition < anonEnd))
return false;
//$NON-NLS-1$
bufBefore.append(" new ");
bufBefore.append(node.getType().toString());
//$NON-NLS-1$
bufBefore.append("(){\n");
fillWithTypeStubs(bufBefore, bufAfter, focalPosition, anonDecl.bodyDeclarations());
//$NON-NLS-1$
bufAfter.append("};\n");
return false;
}
});
buf = bufAfter;
//$NON-NLS-1$
buf.append("}\n");
continue;
}
AbstractTypeDeclaration decl = (AbstractTypeDeclaration) bodyDeclaration;
buf = decl.getStartPosition() < focalPosition ? bufBefore : bufAfter;
appendModifiers(buf, decl.modifiers());
if (decl instanceof TypeDeclaration) {
TypeDeclaration type = (TypeDeclaration) decl;
//$NON-NLS-1$//$NON-NLS-2$
buf.append(type.isInterface() ? "interface " : "class ");
buf.append(type.getName().getIdentifier());
appendTypeParameters(buf, type.typeParameters());
if (type.getSuperclassType() != null) {
//$NON-NLS-1$
buf.append(" extends ");
buf.append(ASTNodes.asString(type.getSuperclassType()));
}
List<Type> superInterfaces = type.superInterfaceTypes();
appendSuperInterfaces(buf, superInterfaces);
} else if (decl instanceof AnnotationTypeDeclaration) {
AnnotationTypeDeclaration annotation = (AnnotationTypeDeclaration) decl;
//$NON-NLS-1$
buf.append("@interface ");
buf.append(annotation.getName().getIdentifier());
} else if (decl instanceof EnumDeclaration) {
EnumDeclaration enumDecl = (EnumDeclaration) decl;
//$NON-NLS-1$
buf.append("enum ");
buf.append(enumDecl.getName().getIdentifier());
List<Type> superInterfaces = enumDecl.superInterfaceTypes();
appendSuperInterfaces(buf, superInterfaces);
}
//$NON-NLS-1$
buf.append("{\n");
if (decl instanceof EnumDeclaration)
//$NON-NLS-1$
buf.append(";\n");
fillWithTypeStubs(bufBefore, bufAfter, focalPosition, decl.bodyDeclarations());
buf = decl.getStartPosition() + decl.getLength() < focalPosition ? bufBefore : bufAfter;
//$NON-NLS-1$
buf.append("}\n");
}
}
use of org.eclipse.jdt.core.dom.ClassInstanceCreation in project che by eclipse.
the class ExtractTempRefactoring method checkSelection.
private RefactoringStatus checkSelection(IProgressMonitor pm) throws JavaModelException {
try {
//$NON-NLS-1$
pm.beginTask("", 8);
IExpressionFragment selectedExpression = getSelectedExpression();
if (selectedExpression == null) {
String message = RefactoringCoreMessages.ExtractTempRefactoring_select_expression;
return CodeRefactoringUtil.checkMethodSyntaxErrors(fSelectionStart, fSelectionLength, fCompilationUnitNode, message);
}
pm.worked(1);
if (isUsedInExplicitConstructorCall())
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_explicit_constructor);
pm.worked(1);
ASTNode associatedNode = selectedExpression.getAssociatedNode();
if (getEnclosingBodyNode() == null || ASTNodes.getParent(associatedNode, Annotation.class) != null)
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_expr_in_method_or_initializer);
pm.worked(1);
if (associatedNode instanceof Name && associatedNode.getParent() instanceof ClassInstanceCreation && associatedNode.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY)
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_name_in_new);
pm.worked(1);
RefactoringStatus result = new RefactoringStatus();
result.merge(checkExpression());
if (result.hasFatalError())
return result;
pm.worked(1);
result.merge(checkExpressionFragmentIsRValue());
if (result.hasFatalError())
return result;
pm.worked(1);
if (isUsedInForInitializerOrUpdater(getSelectedExpression().getAssociatedExpression()))
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_for_initializer_updater);
pm.worked(1);
if (isReferringToLocalVariableFromFor(getSelectedExpression().getAssociatedExpression()))
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_refers_to_for_variable);
pm.worked(1);
return result;
} finally {
pm.done();
}
}
use of org.eclipse.jdt.core.dom.ClassInstanceCreation in project che by eclipse.
the class ConvertAnonymousToNestedRefactoring method createNewClassInstanceCreation.
private ASTNode createNewClassInstanceCreation(CompilationUnitRewrite rewrite, ITypeBinding[] parameters) {
AST ast = fAnonymousInnerClassNode.getAST();
ClassInstanceCreation newClassCreation = ast.newClassInstanceCreation();
newClassCreation.setAnonymousClassDeclaration(null);
Type type = null;
SimpleName newNameNode = ast.newSimpleName(fClassName);
if (parameters.length > 0) {
final ParameterizedType parameterized = ast.newParameterizedType(ast.newSimpleType(newNameNode));
for (int index = 0; index < parameters.length; index++) parameterized.typeArguments().add(ast.newSimpleType(ast.newSimpleName(parameters[index].getName())));
type = parameterized;
} else
type = ast.newSimpleType(newNameNode);
newClassCreation.setType(type);
copyArguments(rewrite, newClassCreation);
addArgumentsForLocalsUsedInInnerClass(newClassCreation);
addLinkedPosition(KEY_TYPE_NAME, newNameNode, rewrite.getASTRewrite(), true);
return newClassCreation;
}
Aggregations