use of org.eclipse.jdt.core.dom.MethodDeclaration in project che by eclipse.
the class ExtractMethodRefactoring method createNewMethod.
private MethodDeclaration createNewMethod(ASTNode[] selectedNodes, String lineDelimiter, TextEditGroup substitute) throws CoreException {
MethodDeclaration result = createNewMethodDeclaration();
result.setBody(createMethodBody(selectedNodes, substitute, result.getModifiers()));
if (fGenerateJavadoc) {
AbstractTypeDeclaration enclosingType = (AbstractTypeDeclaration) ASTNodes.getParent(fAnalyzer.getEnclosingBodyDeclaration(), AbstractTypeDeclaration.class);
String string = CodeGeneration.getMethodComment(fCUnit, enclosingType.getName().getIdentifier(), result, null, lineDelimiter);
if (string != null) {
Javadoc javadoc = (Javadoc) fRewriter.createStringPlaceholder(string, ASTNode.JAVADOC);
result.setJavadoc(javadoc);
}
}
return result;
}
use of org.eclipse.jdt.core.dom.MethodDeclaration in project che by eclipse.
the class ExtractMethodRefactoring method createNewMethodDeclaration.
private MethodDeclaration createNewMethodDeclaration() {
MethodDeclaration result = fAST.newMethodDeclaration();
int modifiers = fVisibility;
BodyDeclaration enclosingBodyDeclaration = fAnalyzer.getEnclosingBodyDeclaration();
boolean isDestinationInterface = isDestinationInterface();
if (isDestinationInterface && !(enclosingBodyDeclaration instanceof MethodDeclaration && enclosingBodyDeclaration.getParent() == fDestination && Modifier.isPublic(enclosingBodyDeclaration.getModifiers()))) {
modifiers = Modifier.NONE;
}
boolean shouldBeStatic = false;
ASTNode currentParent = enclosingBodyDeclaration;
do {
if (currentParent instanceof BodyDeclaration) {
shouldBeStatic = shouldBeStatic || JdtFlags.isStatic((BodyDeclaration) currentParent);
}
currentParent = currentParent.getParent();
} while (!shouldBeStatic && currentParent != null && currentParent != fDestination);
if (shouldBeStatic || fAnalyzer.getForceStatic() || forceStatic()) {
modifiers |= Modifier.STATIC;
} else if (isDestinationInterface) {
modifiers |= Modifier.DEFAULT;
}
ITypeBinding[] typeVariables = computeLocalTypeVariables(modifiers);
List<TypeParameter> typeParameters = result.typeParameters();
for (int i = 0; i < typeVariables.length; i++) {
TypeParameter parameter = fAST.newTypeParameter();
parameter.setName(fAST.newSimpleName(typeVariables[i].getName()));
ITypeBinding[] bounds = typeVariables[i].getTypeBounds();
for (int j = 0; j < bounds.length; j++) if (//$NON-NLS-1$
!"java.lang.Object".equals(bounds[j].getQualifiedName()))
parameter.typeBounds().add(fImportRewriter.addImport(bounds[j], fAST));
typeParameters.add(parameter);
}
result.modifiers().addAll(ASTNodeFactory.newModifiers(fAST, modifiers));
result.setReturnType2((Type) ASTNode.copySubtree(fAST, fAnalyzer.getReturnType()));
result.setName(fAST.newSimpleName(fMethodName));
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(enclosingBodyDeclaration, fImportRewriter);
List<SingleVariableDeclaration> parameters = result.parameters();
for (int i = 0; i < fParameterInfos.size(); i++) {
ParameterInfo info = fParameterInfos.get(i);
VariableDeclaration infoDecl = getVariableDeclaration(info);
SingleVariableDeclaration parameter = fAST.newSingleVariableDeclaration();
parameter.modifiers().addAll(ASTNodeFactory.newModifiers(fAST, ASTNodes.getModifiers(infoDecl)));
parameter.setType(ASTNodeFactory.newType(fAST, infoDecl, fImportRewriter, context));
parameter.setName(fAST.newSimpleName(info.getNewName()));
parameter.setVarargs(info.isNewVarargs());
parameters.add(parameter);
}
List<Type> exceptions = result.thrownExceptionTypes();
ITypeBinding[] exceptionTypes = fAnalyzer.getExceptions(fThrowRuntimeExceptions);
for (int i = 0; i < exceptionTypes.length; i++) {
ITypeBinding exceptionType = exceptionTypes[i];
exceptions.add(fImportRewriter.addImport(exceptionType, fAST, context));
}
return result;
}
use of org.eclipse.jdt.core.dom.MethodDeclaration in project che by eclipse.
the class ExtractMethodRefactoring method initializeDestinations.
private void initializeDestinations() {
List<ASTNode> result = new ArrayList<ASTNode>();
BodyDeclaration decl = fAnalyzer.getEnclosingBodyDeclaration();
ASTNode current = ASTResolving.findParentType(decl.getParent());
if (fAnalyzer.isValidDestination(current)) {
result.add(current);
}
if (current != null && (decl instanceof MethodDeclaration || decl instanceof Initializer || decl instanceof FieldDeclaration)) {
ITypeBinding binding = ASTNodes.getEnclosingType(current);
ASTNode next = ASTResolving.findParentType(current.getParent());
while (next != null && binding != null && binding.isNested()) {
if (fAnalyzer.isValidDestination(next)) {
result.add(next);
}
current = next;
binding = ASTNodes.getEnclosingType(current);
next = ASTResolving.findParentType(next.getParent());
}
}
fDestinations = result.toArray(new ASTNode[result.size()]);
fDestination = fDestinations[fDestinationIndex];
}
use of org.eclipse.jdt.core.dom.MethodDeclaration in project che by eclipse.
the class ExtractMethodAnalyzer method computeTypeVariables.
private ITypeBinding[] computeTypeVariables(ITypeBinding[] bindings) {
Selection selection = getSelection();
Set<ITypeBinding> result = new HashSet<ITypeBinding>();
// first remove all type variables that come from outside of the method
// or are covered by the selection
CompilationUnit compilationUnit = (CompilationUnit) fEnclosingBodyDeclaration.getRoot();
for (int i = 0; i < bindings.length; i++) {
ASTNode decl = compilationUnit.findDeclaringNode(bindings[i]);
if (decl == null || (!selection.covers(decl) && decl.getParent() instanceof MethodDeclaration))
result.add(bindings[i]);
}
// all all type variables which are needed since a local variable uses it
for (int i = 0; i < fArguments.length; i++) {
IVariableBinding arg = fArguments[i];
ITypeBinding type = arg.getType();
if (type != null && type.isTypeVariable()) {
ASTNode decl = compilationUnit.findDeclaringNode(type);
if (decl == null || (!selection.covers(decl) && decl.getParent() instanceof MethodDeclaration))
result.add(type);
}
}
return result.toArray(new ITypeBinding[result.size()]);
}
use of org.eclipse.jdt.core.dom.MethodDeclaration in project che by eclipse.
the class ConvertAnonymousToNestedRefactoring method mustInnerClassBeStatic.
public boolean mustInnerClassBeStatic() {
ITypeBinding typeBinding = ((AbstractTypeDeclaration) ASTNodes.getParent(fAnonymousInnerClassNode, AbstractTypeDeclaration.class)).resolveBinding();
ASTNode current = fAnonymousInnerClassNode.getParent();
boolean ans = false;
while (current != null) {
switch(current.getNodeType()) {
case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
case ASTNode.CONSTRUCTOR_INVOCATION:
return true;
case ASTNode.ANONYMOUS_CLASS_DECLARATION:
{
AnonymousClassDeclaration enclosingAnonymousClassDeclaration = (AnonymousClassDeclaration) current;
ITypeBinding binding = enclosingAnonymousClassDeclaration.resolveBinding();
if (binding != null && Bindings.isSuperType(typeBinding, binding.getSuperclass())) {
return false;
}
break;
}
case ASTNode.FIELD_DECLARATION:
{
FieldDeclaration enclosingFieldDeclaration = (FieldDeclaration) current;
if (Modifier.isStatic(enclosingFieldDeclaration.getModifiers())) {
ans = true;
}
break;
}
case ASTNode.METHOD_DECLARATION:
{
MethodDeclaration enclosingMethodDeclaration = (MethodDeclaration) current;
if (Modifier.isStatic(enclosingMethodDeclaration.getModifiers())) {
ans = true;
}
break;
}
case ASTNode.TYPE_DECLARATION:
{
return ans;
}
}
current = current.getParent();
}
return ans;
}
Aggregations