use of org.eclipse.jdt.ls.core.internal.corext.refactoring.ParameterInfo in project eclipse.jdt.ls by eclipse.
the class ExtractMethodRefactoring method checkParameterNames.
/**
* Checks if the parameter names are valid.
*
* @return validation status
*/
public RefactoringStatus checkParameterNames() {
RefactoringStatus result = new RefactoringStatus();
for (Iterator<ParameterInfo> iter = fParameterInfos.iterator(); iter.hasNext(); ) {
ParameterInfo parameter = iter.next();
result.merge(Checks.checkIdentifier(parameter.getNewName(), fCUnit));
for (Iterator<ParameterInfo> others = fParameterInfos.iterator(); others.hasNext(); ) {
ParameterInfo other = others.next();
if (parameter != other && other.getNewName().equals(parameter.getNewName())) {
result.addError(Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_error_sameParameter, BasicElementLabels.getJavaElementName(other.getNewName())));
return result;
}
}
if (parameter.isRenamed() && fUsedNames.contains(parameter.getNewName())) {
result.addError(Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_error_nameInUse, BasicElementLabels.getJavaElementName(parameter.getNewName())));
return result;
}
}
return result;
}
use of org.eclipse.jdt.ls.core.internal.corext.refactoring.ParameterInfo in project eclipse.jdt.ls 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 (!"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, TypeLocation.EXCEPTION));
}
return result;
}
Aggregations