use of org.eclipse.jdt.internal.corext.refactoring.ParameterInfo in project che by eclipse.
the class ChangeSignatureProcessor method createComment.
private JDTRefactoringDescriptorComment createComment(String project, final String header) throws JavaModelException {
final JDTRefactoringDescriptorComment comment = new JDTRefactoringDescriptorComment(project, this, header);
if (!fMethod.getElementName().equals(fMethodName))
comment.addSetting(Messages.format(RefactoringCoreMessages.ChangeSignatureRefactoring_new_name_pattern, BasicElementLabels.getJavaElementName(fMethodName)));
if (!isVisibilitySameAsInitial()) {
String visibility = JdtFlags.getVisibilityString(fVisibility);
if (//$NON-NLS-1$
"".equals(visibility))
visibility = RefactoringCoreMessages.ChangeSignatureRefactoring_default_visibility;
comment.addSetting(Messages.format(RefactoringCoreMessages.ChangeSignatureRefactoring_new_visibility_pattern, visibility));
}
if (fReturnTypeInfo.isTypeNameChanged())
comment.addSetting(Messages.format(RefactoringCoreMessages.ChangeSignatureRefactoring_new_return_type_pattern, BasicElementLabels.getJavaElementName(fReturnTypeInfo.getNewTypeName())));
List<String> deleted = new ArrayList<String>();
List<String> added = new ArrayList<String>();
List<String> changed = new ArrayList<String>();
for (final Iterator<ParameterInfo> iterator = fParameterInfos.iterator(); iterator.hasNext(); ) {
final ParameterInfo info = iterator.next();
if (info.isDeleted())
deleted.add(Messages.format(RefactoringCoreMessages.ChangeSignatureRefactoring_deleted_parameter_pattern, new String[] { BasicElementLabels.getJavaElementName(info.getOldTypeName()), BasicElementLabels.getJavaElementName(info.getOldName()) }));
else if (info.isAdded())
added.add(Messages.format(RefactoringCoreMessages.ChangeSignatureRefactoring_added_parameter_pattern, new String[] { BasicElementLabels.getJavaElementName(info.getNewTypeName()), BasicElementLabels.getJavaElementName(info.getNewName()) }));
else if (info.isRenamed() || info.isTypeNameChanged() || info.isVarargChanged())
changed.add(Messages.format(RefactoringCoreMessages.ChangeSignatureRefactoring_changed_parameter_pattern, new String[] { BasicElementLabels.getJavaElementName(info.getOldTypeName()), BasicElementLabels.getJavaElementName(info.getOldName()) }));
}
if (!added.isEmpty())
comment.addSetting(JDTRefactoringDescriptorComment.createCompositeSetting(RefactoringCoreMessages.ChangeSignatureRefactoring_added_parameters, added.toArray(new String[added.size()])));
if (!deleted.isEmpty())
comment.addSetting(JDTRefactoringDescriptorComment.createCompositeSetting(RefactoringCoreMessages.ChangeSignatureRefactoring_removed_parameters, deleted.toArray(new String[deleted.size()])));
if (!changed.isEmpty())
comment.addSetting(JDTRefactoringDescriptorComment.createCompositeSetting(RefactoringCoreMessages.ChangeSignatureRefactoring_changed_parameters, changed.toArray(new String[changed.size()])));
added.clear();
deleted.clear();
changed.clear();
for (final Iterator<ExceptionInfo> iterator = fExceptionInfos.iterator(); iterator.hasNext(); ) {
final ExceptionInfo info = iterator.next();
if (info.isAdded())
added.add(info.getElement().getElementName());
else if (info.isDeleted())
deleted.add(info.getElement().getElementName());
}
if (!added.isEmpty())
comment.addSetting(JDTRefactoringDescriptorComment.createCompositeSetting(RefactoringCoreMessages.ChangeSignatureRefactoring_added_exceptions, added.toArray(new String[added.size()])));
if (!deleted.isEmpty())
comment.addSetting(JDTRefactoringDescriptorComment.createCompositeSetting(RefactoringCoreMessages.ChangeSignatureRefactoring_removed_exceptions, deleted.toArray(new String[deleted.size()])));
return comment;
}
use of org.eclipse.jdt.internal.corext.refactoring.ParameterInfo in project che 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.internal.corext.refactoring.ParameterInfo in project che by eclipse.
the class ExtractMethodRefactoring method createCallNodes.
//---- Code generation -----------------------------------------------------------------------
private ASTNode[] createCallNodes(SnippetFinder.Match duplicate, int modifiers) {
List<ASTNode> result = new ArrayList<ASTNode>(2);
IVariableBinding[] locals = fAnalyzer.getCallerLocals();
for (int i = 0; i < locals.length; i++) {
result.add(createDeclaration(locals[i], null));
}
MethodInvocation invocation = fAST.newMethodInvocation();
invocation.setName(fAST.newSimpleName(fMethodName));
ASTNode typeNode = ASTResolving.findParentType(fAnalyzer.getEnclosingBodyDeclaration());
RefactoringStatus status = new RefactoringStatus();
while (fDestination != typeNode) {
fAnalyzer.checkInput(status, fMethodName, typeNode);
if (!status.isOK()) {
SimpleName destinationTypeName = fAST.newSimpleName(ASTNodes.getEnclosingType(fDestination).getName());
if ((modifiers & Modifier.STATIC) == 0) {
ThisExpression thisExpression = fAST.newThisExpression();
thisExpression.setQualifier(destinationTypeName);
invocation.setExpression(thisExpression);
} else {
invocation.setExpression(destinationTypeName);
}
break;
}
typeNode = typeNode.getParent();
}
List<Expression> arguments = invocation.arguments();
for (int i = 0; i < fParameterInfos.size(); i++) {
ParameterInfo parameter = fParameterInfos.get(i);
arguments.add(ASTNodeFactory.newName(fAST, getMappedName(duplicate, parameter)));
}
if (fLinkedProposalModel != null) {
LinkedProposalPositionGroup nameGroup = fLinkedProposalModel.getPositionGroup(KEY_NAME, true);
nameGroup.addPosition(fRewriter.track(invocation.getName()), false);
}
ASTNode call;
int returnKind = fAnalyzer.getReturnKind();
switch(returnKind) {
case ExtractMethodAnalyzer.ACCESS_TO_LOCAL:
IVariableBinding binding = fAnalyzer.getReturnLocal();
if (binding != null) {
VariableDeclarationStatement decl = createDeclaration(getMappedBinding(duplicate, binding), invocation);
call = decl;
} else {
Assignment assignment = fAST.newAssignment();
assignment.setLeftHandSide(ASTNodeFactory.newName(fAST, getMappedBinding(duplicate, fAnalyzer.getReturnValue()).getName()));
assignment.setRightHandSide(invocation);
call = assignment;
}
break;
case ExtractMethodAnalyzer.RETURN_STATEMENT_VALUE:
ReturnStatement rs = fAST.newReturnStatement();
rs.setExpression(invocation);
call = rs;
break;
default:
call = invocation;
}
if (call instanceof Expression && !fAnalyzer.isExpressionSelected()) {
call = fAST.newExpressionStatement((Expression) call);
}
result.add(call);
// return;
if (returnKind == ExtractMethodAnalyzer.RETURN_STATEMENT_VOID && !fAnalyzer.isLastStatementSelected()) {
result.add(fAST.newReturnStatement());
}
return result.toArray(new ASTNode[result.size()]);
}
use of org.eclipse.jdt.internal.corext.refactoring.ParameterInfo in project xtext-xtend by eclipse.
the class ExtractMethodRefactoring method appendMethodSignature.
protected void appendMethodSignature(ISourceAppender appendable) {
if (visibility != JvmVisibility.PUBLIC)
appendable.append(getVisibility().getName().toLowerCase()).append(" ");
appendable.append("def ");
if (isStatic)
appendable.append("static ");
if (!neededTypeParameters.isEmpty()) {
JvmOperation operation = associations.getDirectlyInferredOperation(originalMethod);
if (operation != null) {
appendable.append("<");
boolean isFirst = true;
for (JvmTypeParameter typeParameter : operation.getTypeParameters()) {
if (neededTypeParameters.contains(typeParameter)) {
if (!isFirst)
appendable.append(", ");
isFirst = false;
appendable.append(typeParameter);
}
}
appendable.append("> ");
}
}
if (isExplicitlyDeclareReturnType) {
appendable.append(returnType).append(" ");
}
appendable.append(methodName).append("(");
boolean isFirst = true;
for (ParameterInfo parameterInfo : getParameterInfos()) {
if (!isFirst)
appendable.append(", ");
isFirst = false;
appendable.append(parameter2type.get(parameterInfo)).append(" ").append(parameterInfo.getNewName());
}
appendable.append(")");
}
use of org.eclipse.jdt.internal.corext.refactoring.ParameterInfo in project xtext-xtend by eclipse.
the class ExtractMethodRefactoring method validateParameters.
public RefactoringStatus validateParameters() {
RefactoringStatus status = new RefactoringStatus();
Set<String> namesSoFar = newHashSet();
for (ParameterInfo parameterInfo : parameterInfos) {
String newName = parameterInfo.getNewName();
if (namesSoFar.contains(newName))
status.addError("Duplicate parameter name '" + newName + "'");
if (!equal(newName, parameterInfo.getOldName()) && localFeatureNames.contains(newName))
status.addError("'" + newName + "' is already used as a name in the selected code");
nameUtil.checkNewFeatureName(newName, false, status);
namesSoFar.add(newName);
}
// TODO: vararg order
return status;
}
Aggregations