use of org.eclipse.jdt.core.dom.ASTNode in project che by eclipse.
the class NewCUUsingWizardProposal method getPossibleSuperTypeBinding.
// private NewTypeWizardPage getPage(NewElementWizard wizard) {
// IWizardPage[] pages= wizard.getPages();
// Assert.isTrue(pages.length > 0 && pages[0] instanceof NewTypeWizardPage);
// return (NewTypeWizardPage) pages[0];
// }
//
// private NewElementWizard createWizard(StructuredSelection selection) {
// switch (fTypeKind) {
// case K_CLASS: {
// NewClassWizardPage page= new NewClassWizardPage();
// page.init(selection);
// configureWizardPage(page);
// return new NewClassCreationWizard(page, true);
// }
// case K_INTERFACE: {
// NewInterfaceWizardPage page= new NewInterfaceWizardPage();
// page.init(selection);
// configureWizardPage(page);
// return new NewInterfaceCreationWizard(page, true);
// }
// case K_ENUM: {
// NewEnumWizardPage page= new NewEnumWizardPage();
// page.init(selection);
// configureWizardPage(page);
// return new NewEnumCreationWizard(page, true);
// }
// case K_ANNOTATION: {
// NewAnnotationWizardPage page= new NewAnnotationWizardPage();
// page.init(selection);
// configureWizardPage(page);
// return new NewAnnotationCreationWizard(page, true);
// }
// }
// throw new IllegalArgumentException();
// }
//
// private void configureWizardPage(NewTypeWizardPage page) {
// fillInWizardPageName(page);
// fillInWizardPageSuperTypes(page);
// }
//
// /**
// * Fill-in the "Package" and "Name" fields.
// * @param page the wizard page.
// */
// private void fillInWizardPageName(NewTypeWizardPage page) {
// // allow to edit when there are type parameters
// page.setTypeName(fTypeNameWithParameters, fTypeNameWithParameters.indexOf('<') != -1);
//
// boolean isInEnclosingType= fTypeContainer instanceof IType;
// if (isInEnclosingType) {
// page.setEnclosingType((IType) fTypeContainer, true);
// } else {
// page.setPackageFragment((IPackageFragment) fTypeContainer, true);
// }
// page.setEnclosingTypeSelection(isInEnclosingType, true);
// }
//
// /**
// * Fill-in the "Super Class" and "Super Interfaces" fields.
// * @param page the wizard page.
// */
// private void fillInWizardPageSuperTypes(NewTypeWizardPage page) {
// ITypeBinding type= getPossibleSuperTypeBinding(fNode);
// type= Bindings.normalizeTypeBinding(type);
// if (type != null) {
// if (type.isArray()) {
// type= type.getElementType();
// }
// if (type.isTopLevel() || type.isMember()) {
// if (type.isClass() && (fTypeKind == K_CLASS)) {
// page.setSuperClass(type.getQualifiedName(), true);
// } else if (type.isInterface()) {
// List<String> superInterfaces= new ArrayList<String>();
// superInterfaces.add(type.getQualifiedName());
// page.setSuperInterfaces(superInterfaces, true);
// }
// }
// }
// }
private ITypeBinding getPossibleSuperTypeBinding(ASTNode node) {
if (fTypeKind == K_ANNOTATION) {
return null;
}
AST ast = node.getAST();
node = ASTNodes.getNormalizedNode(node);
ASTNode parent = node.getParent();
switch(parent.getNodeType()) {
case ASTNode.METHOD_DECLARATION:
if (node.getLocationInParent() == MethodDeclaration.THROWN_EXCEPTION_TYPES_PROPERTY) {
//$NON-NLS-1$
return ast.resolveWellKnownType("java.lang.Exception");
}
break;
case ASTNode.THROW_STATEMENT:
//$NON-NLS-1$
return ast.resolveWellKnownType("java.lang.Exception");
case ASTNode.SINGLE_VARIABLE_DECLARATION:
if (parent.getLocationInParent() == CatchClause.EXCEPTION_PROPERTY) {
//$NON-NLS-1$
return ast.resolveWellKnownType("java.lang.Exception");
}
break;
case ASTNode.VARIABLE_DECLARATION_STATEMENT:
case ASTNode.FIELD_DECLARATION:
// no guessing for LHS types, cannot be a supertype of a known type
return null;
case ASTNode.PARAMETERIZED_TYPE:
// Inheritance doesn't help: A<X> z= new A<String>(); ->
return null;
}
ITypeBinding binding = ASTResolving.guessBindingForTypeReference(node);
if (binding != null && !binding.isRecovered()) {
return binding;
}
return null;
}
use of org.eclipse.jdt.core.dom.ASTNode in project che by eclipse.
the class NewMethodCorrectionProposal method evaluateModifiers.
private int evaluateModifiers(ASTNode targetTypeDecl) {
if (getSenderBinding().isAnnotation()) {
return 0;
}
if (getSenderBinding().isInterface()) {
// for interface and annotation members copy the modifiers from an existing field
MethodDeclaration[] methodDecls = ((TypeDeclaration) targetTypeDecl).getMethods();
if (methodDecls.length > 0) {
return methodDecls[0].getModifiers();
}
return 0;
}
ASTNode invocationNode = getInvocationNode();
if (invocationNode instanceof MethodInvocation) {
int modifiers = 0;
Expression expression = ((MethodInvocation) invocationNode).getExpression();
if (expression != null) {
if (expression instanceof Name && ((Name) expression).resolveBinding().getKind() == IBinding.TYPE) {
modifiers |= Modifier.STATIC;
}
} else if (ASTResolving.isInStaticContext(invocationNode)) {
modifiers |= Modifier.STATIC;
}
ASTNode node = ASTResolving.findParentType(invocationNode);
if (targetTypeDecl.equals(node)) {
modifiers |= Modifier.PRIVATE;
} else if (node instanceof AnonymousClassDeclaration && ASTNodes.isParent(node, targetTypeDecl)) {
modifiers |= Modifier.PROTECTED;
if (ASTResolving.isInStaticContext(node) && expression == null) {
modifiers |= Modifier.STATIC;
}
} else {
modifiers |= Modifier.PUBLIC;
}
return modifiers;
}
return Modifier.PUBLIC;
}
use of org.eclipse.jdt.core.dom.ASTNode in project che by eclipse.
the class NewMethodCorrectionProposal method getNewMethodType.
/* (non-Javadoc)
* @see org.eclipse.jdt.internal.ui.text.correction.proposals.AbstractMethodCorrectionProposal#getNewMethodType(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)
*/
@Override
protected Type getNewMethodType(ASTRewrite rewrite) throws CoreException {
ASTNode node = getInvocationNode();
AST ast = rewrite.getAST();
Type newTypeNode = null;
ITypeBinding[] otherProposals = null;
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(node, getImportRewrite());
if (node.getParent() instanceof MethodInvocation) {
MethodInvocation parent = (MethodInvocation) node.getParent();
if (parent.getExpression() == node) {
ITypeBinding[] bindings = ASTResolving.getQualifierGuess(node.getRoot(), parent.getName().getIdentifier(), parent.arguments(), getSenderBinding());
if (bindings.length > 0) {
newTypeNode = getImportRewrite().addImport(bindings[0], ast, importRewriteContext);
otherProposals = bindings;
}
}
}
if (newTypeNode == null) {
ITypeBinding binding = ASTResolving.guessBindingForReference(node);
if (binding != null && binding.isWildcardType()) {
binding = ASTResolving.normalizeWildcardType(binding, false, ast);
}
if (binding != null) {
newTypeNode = getImportRewrite().addImport(binding, ast, importRewriteContext);
} else {
ASTNode parent = node.getParent();
if (parent instanceof ExpressionStatement) {
newTypeNode = ast.newPrimitiveType(PrimitiveType.VOID);
} else {
newTypeNode = ASTResolving.guessTypeForReference(ast, node);
if (newTypeNode == null) {
//$NON-NLS-1$
newTypeNode = ast.newSimpleType(ast.newSimpleName("Object"));
}
}
}
}
addLinkedPosition(rewrite.track(newTypeNode), false, KEY_TYPE);
if (otherProposals != null) {
for (int i = 0; i < otherProposals.length; i++) {
addLinkedPositionProposal(KEY_TYPE, otherProposals[i]);
}
}
return newTypeNode;
}
use of org.eclipse.jdt.core.dom.ASTNode in project che by eclipse.
the class NewMethodCorrectionProposal method getNewName.
/* (non-Javadoc)
* @see org.eclipse.jdt.internal.ui.text.correction.proposals.AbstractMethodCorrectionProposal#getNewName(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)
*/
@Override
protected SimpleName getNewName(ASTRewrite rewrite) {
ASTNode invocationNode = getInvocationNode();
String name;
if (invocationNode instanceof MethodInvocation) {
name = ((MethodInvocation) invocationNode).getName().getIdentifier();
} else if (invocationNode instanceof SuperMethodInvocation) {
name = ((SuperMethodInvocation) invocationNode).getName().getIdentifier();
} else {
// name of the class
name = getSenderBinding().getName();
}
AST ast = rewrite.getAST();
SimpleName newNameNode = ast.newSimpleName(name);
addLinkedPosition(rewrite.track(newNameNode), false, KEY_NAME);
ASTNode invocationName = getInvocationNameNode();
if (invocationName != null && invocationName.getAST() == ast) {
// in the same CU
addLinkedPosition(rewrite.track(invocationName), true, KEY_NAME);
}
return newNameNode;
}
use of org.eclipse.jdt.core.dom.ASTNode in project che by eclipse.
the class NewVariableCorrectionProposal method getDominantNode.
private ASTNode getDominantNode(SimpleName[] names) {
//ASTResolving.findParentStatement(names[0]);
ASTNode dominator = names[0];
for (int i = 1; i < names.length; i++) {
// ASTResolving.findParentStatement(names[i]);
ASTNode curr = names[i];
if (curr != dominator) {
ASTNode parent = getCommonParent(curr, dominator);
if (curr.getStartPosition() < dominator.getStartPosition()) {
dominator = curr;
}
while (dominator.getParent() != parent) {
dominator = dominator.getParent();
}
}
}
int parentKind = dominator.getParent().getNodeType();
if (parentKind != ASTNode.BLOCK && parentKind != ASTNode.FOR_STATEMENT) {
return dominator.getParent();
}
return dominator;
}
Aggregations