use of org.eclipse.jdt.core.dom.MethodDeclaration in project che by eclipse.
the class CodeRefactoringUtil method checkMethodSyntaxErrors.
public static RefactoringStatus checkMethodSyntaxErrors(int selectionStart, int selectionLength, CompilationUnit cuNode, String invalidSelectionMessage) {
SelectionAnalyzer analyzer = new SelectionAnalyzer(Selection.createFromStartLength(selectionStart, selectionLength), true);
cuNode.accept(analyzer);
ASTNode coveringNode = analyzer.getLastCoveringNode();
if (!(coveringNode instanceof Block) || !(coveringNode.getParent() instanceof MethodDeclaration))
return RefactoringStatus.createFatalErrorStatus(invalidSelectionMessage);
if (ASTNodes.getMessages(coveringNode, ASTNodes.NODE_ONLY).length == 0)
return RefactoringStatus.createFatalErrorStatus(invalidSelectionMessage);
MethodDeclaration methodDecl = (MethodDeclaration) coveringNode.getParent();
String message = Messages.format(RefactoringCoreMessages.CodeRefactoringUtil_error_message, BasicElementLabels.getJavaElementName(methodDecl.getName().getIdentifier()));
return RefactoringStatus.createFatalErrorStatus(message);
}
use of org.eclipse.jdt.core.dom.MethodDeclaration in project che by eclipse.
the class ConvertAnonymousToNestedRefactoring method getTypeParameters.
private ITypeBinding[] getTypeParameters() {
final List<ITypeBinding> parameters = new ArrayList<ITypeBinding>(4);
final ClassInstanceCreation creation = (ClassInstanceCreation) fAnonymousInnerClassNode.getParent();
if (fDeclareStatic) {
final TypeVariableFinder finder = new TypeVariableFinder();
creation.accept(finder);
return finder.getResult();
} else {
final MethodDeclaration declaration = getEnclosingMethodDeclaration(creation);
if (declaration != null) {
ITypeBinding binding = null;
TypeParameter parameter = null;
for (final Iterator<TypeParameter> iterator = declaration.typeParameters().iterator(); iterator.hasNext(); ) {
parameter = iterator.next();
binding = parameter.resolveBinding();
if (binding != null)
parameters.add(binding);
}
}
}
final TypeVariableFinder finder = new TypeVariableFinder();
creation.accept(finder);
final ITypeBinding[] variables = finder.getResult();
final List<ITypeBinding> remove = new ArrayList<ITypeBinding>(4);
boolean match = false;
ITypeBinding binding = null;
ITypeBinding variable = null;
for (final Iterator<ITypeBinding> iterator = parameters.iterator(); iterator.hasNext(); ) {
match = false;
binding = iterator.next();
for (int index = 0; index < variables.length; index++) {
variable = variables[index];
if (variable.equals(binding))
match = true;
}
if (!match)
remove.add(binding);
}
parameters.removeAll(remove);
final ITypeBinding[] result = new ITypeBinding[parameters.size()];
parameters.toArray(result);
return result;
}
use of org.eclipse.jdt.core.dom.MethodDeclaration in project che by eclipse.
the class SurroundWithAnalyzer method endVisit.
@Override
public void endVisit(CompilationUnit node) {
postProcessSelectedNodes(internalGetSelectedNodes());
BodyDeclaration enclosingNode = null;
superCall: {
if (getStatus().hasFatalError())
break superCall;
if (!hasSelectedNodes()) {
ASTNode coveringNode = getLastCoveringNode();
if (coveringNode instanceof Block) {
Block block = (Block) coveringNode;
Message[] messages = ASTNodes.getMessages(block, ASTNodes.NODE_ONLY);
if (messages.length > 0) {
invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_compile_errors, JavaStatusContext.create(getCompilationUnit(), block));
break superCall;
}
}
invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_doesNotCover);
break superCall;
}
enclosingNode = (BodyDeclaration) ASTNodes.getParent(getFirstSelectedNode(), BodyDeclaration.class);
if (!(enclosingNode instanceof MethodDeclaration) && !(enclosingNode instanceof Initializer)) {
invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_doesNotContain);
break superCall;
}
if (!onlyStatements()) {
invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_onlyStatements);
}
fLocals = LocalDeclarationAnalyzer.perform(enclosingNode, getSelection());
}
super.endVisit(node);
}
use of org.eclipse.jdt.core.dom.MethodDeclaration in project che by eclipse.
the class IntroduceFactoryRefactoring method getTargetNode.
/**
* Finds and returns the <code>ASTNode</code> for the given source text
* selection, if it is an entire constructor call or the class name portion
* of a constructor call or constructor declaration, or null otherwise.
* @param unit The compilation unit in which the selection was made
* @param offset The textual offset of the start of the selection
* @param length The length of the selection in characters
* @return ClassInstanceCreation or MethodDeclaration
*/
private ASTNode getTargetNode(ICompilationUnit unit, int offset, int length) {
ASTNode node = ASTNodes.getNormalizedNode(NodeFinder.perform(fCU, offset, length));
if (node.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION)
return node;
if (node.getNodeType() == ASTNode.METHOD_DECLARATION && ((MethodDeclaration) node).isConstructor())
return node;
// we have some sub node. Make sure its the right child of the parent
StructuralPropertyDescriptor location = node.getLocationInParent();
ASTNode parent = node.getParent();
if (location == ClassInstanceCreation.TYPE_PROPERTY) {
return parent;
} else if (location == MethodDeclaration.NAME_PROPERTY && ((MethodDeclaration) parent).isConstructor()) {
return parent;
}
return null;
}
use of org.eclipse.jdt.core.dom.MethodDeclaration in project che by eclipse.
the class IntroduceFactoryRefactoring method getCtorCallAt.
/**
* Look "in the vicinity" of the given range to find the <code>ClassInstanceCreation</code>
* node that this search hit identified. Necessary because the <code>SearchEngine</code>
* doesn't always cough up text extents that <code>NodeFinder.perform()</code> agrees with.
* @param start
* @param length
* @param unitAST
* @return return a {@link ClassInstanceCreation} or a {@link MethodRef} or <code>null</code> if this is really a constructor->constructor call (e.g. "this(...)")
* @throws CoreException
*/
private ASTNode getCtorCallAt(int start, int length, CompilationUnit unitAST) throws CoreException {
ICompilationUnit unitHandle = ASTCreator.getCu(unitAST);
ASTNode node = NodeFinder.perform(unitAST, start, length);
if (node == null)
throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, Messages.format(RefactoringCoreMessages.IntroduceFactory_noASTNodeForConstructorSearchHit, new Object[] { Integer.toString(start), Integer.toString(start + length), BasicElementLabels.getJavaCodeString(unitHandle.getSource().substring(start, start + length)), BasicElementLabels.getFileName(unitHandle) }), null));
if (node instanceof ClassInstanceCreation) {
if (((ClassInstanceCreation) node).getAnonymousClassDeclaration() != null) {
// Cannot replace anonymous inner class, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=250660
fConstructorVisibility = Modifier.PROTECTED;
return null;
}
return node;
} else if (node instanceof VariableDeclaration) {
Expression init = ((VariableDeclaration) node).getInitializer();
if (init instanceof ClassInstanceCreation) {
return init;
} else if (init != null)
throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, Messages.format(RefactoringCoreMessages.IntroduceFactory_unexpectedInitializerNodeType, new Object[] { BasicElementLabels.getJavaCodeString(init.toString()), BasicElementLabels.getFileName(unitHandle) }), null));
else
throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, Messages.format(RefactoringCoreMessages.IntroduceFactory_noConstructorCallNodeInsideFoundVarbleDecl, BasicElementLabels.getJavaCodeString(node.toString())), null));
} else if (node instanceof ConstructorInvocation) {
// to another flavor on the same class.
return null;
} else if (node instanceof SuperConstructorInvocation) {
// This is a call we can bypass; it's from one constructor flavor
// to another flavor on the same class.
fConstructorVisibility = Modifier.PROTECTED;
return null;
} else if (node instanceof ExpressionStatement) {
Expression expr = ((ExpressionStatement) node).getExpression();
if (expr instanceof ClassInstanceCreation)
return expr;
else
throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, Messages.format(RefactoringCoreMessages.IntroduceFactory_unexpectedASTNodeTypeForConstructorSearchHit, new Object[] { BasicElementLabels.getJavaCodeString(expr.toString()), BasicElementLabels.getFileName(unitHandle) }), null));
} else if (node instanceof SimpleName && (node.getParent() instanceof MethodDeclaration || node.getParent() instanceof AbstractTypeDeclaration)) {
// We seem to have been given a hit for an implicit call to the base-class constructor.
// Do nothing with this (implicit) call, but have to make sure we make the derived class
// doesn't lose access to the base-class constructor (so make it 'protected', not 'private').
fConstructorVisibility = Modifier.PROTECTED;
return null;
} else if (node instanceof MethodRef) {
return node;
} else
throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, Messages.format(RefactoringCoreMessages.IntroduceFactory_unexpectedASTNodeTypeForConstructorSearchHit, new Object[] { BasicElementLabels.getJavaElementName(node.getClass().getName() + "('" + node.toString() + "')"), BasicElementLabels.getFileName(unitHandle) }), //$NON-NLS-1$ //$NON-NLS-2$
null));
}
Aggregations