use of org.eclipse.jdt.core.dom.SingleVariableDeclaration in project che by eclipse.
the class ConvertAnonymousToNestedRefactoring method createNewConstructor.
private MethodDeclaration createNewConstructor(CompilationUnitRewrite rewrite, IVariableBinding[] bindings, String[] fieldNames) throws JavaModelException {
ClassInstanceCreation instanceCreation = (ClassInstanceCreation) fAnonymousInnerClassNode.getParent();
if (instanceCreation.arguments().isEmpty() && bindings.length == 0)
return null;
IJavaProject project = fCu.getJavaProject();
AST ast = rewrite.getAST();
ImportRewrite importRewrite = rewrite.getImportRewrite();
ASTRewrite astRewrite = rewrite.getASTRewrite();
MethodDeclaration newConstructor = ast.newMethodDeclaration();
newConstructor.setConstructor(true);
newConstructor.setJavadoc(null);
newConstructor.modifiers().addAll(ASTNodeFactory.newModifiers(ast, fVisibility));
newConstructor.setName(ast.newSimpleName(fClassName));
addLinkedPosition(KEY_TYPE_NAME, newConstructor.getName(), astRewrite, false);
newConstructor.setBody(ast.newBlock());
List<Statement> newStatements = newConstructor.getBody().statements();
List<SingleVariableDeclaration> newParameters = newConstructor.parameters();
List<String> newParameterNames = new ArrayList<String>();
// add parameters for elements passed with the instance creation
if (!instanceCreation.arguments().isEmpty()) {
IMethodBinding constructorBinding = getSuperConstructorBinding();
if (constructorBinding != null) {
SuperConstructorInvocation superConstructorInvocation = ast.newSuperConstructorInvocation();
ITypeBinding[] parameterTypes = constructorBinding.getParameterTypes();
String[][] parameterNames = StubUtility.suggestArgumentNamesWithProposals(project, constructorBinding);
for (int i = 0; i < parameterNames.length; i++) {
String[] nameProposals = parameterNames[i];
String paramName = nameProposals[0];
SingleVariableDeclaration param = newParameterDeclaration(ast, importRewrite, paramName, parameterTypes[i]);
newParameters.add(param);
newParameterNames.add(paramName);
SimpleName newSIArgument = ast.newSimpleName(paramName);
superConstructorInvocation.arguments().add(newSIArgument);
if (fLinkedProposalModel != null) {
LinkedProposalPositionGroup positionGroup = fLinkedProposalModel.getPositionGroup(KEY_PARAM_NAME_CONST + String.valueOf(i), true);
positionGroup.addPosition(astRewrite.track(param.getName()), false);
positionGroup.addPosition(astRewrite.track(newSIArgument), false);
for (int k = 0; k < nameProposals.length; k++) {
positionGroup.addProposal(nameProposals[k], null, nameProposals.length - k);
}
}
}
newStatements.add(superConstructorInvocation);
}
}
// add parameters for all outer variables used
boolean useThisAccess = useThisForFieldAccess();
for (int i = 0; i < bindings.length; i++) {
String baseName = StubUtility.getBaseName(bindings[i], project);
String[] paramNameProposals = StubUtility.getVariableNameSuggestions(NamingConventions.VK_PARAMETER, project, baseName, 0, newParameterNames, true);
String paramName = paramNameProposals[0];
SingleVariableDeclaration param = newParameterDeclaration(ast, importRewrite, paramName, bindings[i].getType());
newParameters.add(param);
newParameterNames.add(paramName);
String fieldName = fieldNames[i];
SimpleName fieldNameNode = ast.newSimpleName(fieldName);
SimpleName paramNameNode = ast.newSimpleName(paramName);
newStatements.add(newFieldAssignment(ast, fieldNameNode, paramNameNode, useThisAccess || newParameterNames.contains(fieldName)));
if (fLinkedProposalModel != null) {
LinkedProposalPositionGroup positionGroup = fLinkedProposalModel.getPositionGroup(KEY_PARAM_NAME_EXT + String.valueOf(i), true);
positionGroup.addPosition(astRewrite.track(param.getName()), false);
positionGroup.addPosition(astRewrite.track(paramNameNode), false);
for (int k = 0; k < paramNameProposals.length; k++) {
positionGroup.addProposal(paramNameProposals[k], null, paramNameProposals.length - k);
}
fLinkedProposalModel.getPositionGroup(KEY_FIELD_NAME_EXT + i, true).addPosition(astRewrite.track(fieldNameNode), false);
}
}
addExceptionsToNewConstructor(newConstructor, importRewrite);
if (doAddComments()) {
try {
String[] allParamNames = newParameterNames.toArray(new String[newParameterNames.size()]);
String string = CodeGeneration.getMethodComment(fCu, fClassName, fClassName, allParamNames, new String[0], null, new String[0], null, StubUtility.getLineDelimiterUsed(fCu));
if (string != null) {
Javadoc javadoc = (Javadoc) astRewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
newConstructor.setJavadoc(javadoc);
}
} catch (CoreException exception) {
throw new JavaModelException(exception);
}
}
return newConstructor;
}
use of org.eclipse.jdt.core.dom.SingleVariableDeclaration in project che by eclipse.
the class FullConstraintCreator method create.
/* (non-Javadoc)
* @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.MethodDeclaration)
*/
@Override
public ITypeConstraint[] create(MethodDeclaration declaration) {
List<ITypeConstraint> result = new ArrayList<ITypeConstraint>();
IMethodBinding methodBinding = declaration.resolveBinding();
if (methodBinding == null)
return new ITypeConstraint[0];
ITypeConstraint[] constraints = fTypeConstraintFactory.createDefinesConstraint(fConstraintVariableFactory.makeDeclaringTypeVariable(methodBinding), fConstraintVariableFactory.makeRawBindingVariable(methodBinding.getDeclaringClass()));
result.addAll(Arrays.asList(constraints));
if (!methodBinding.isConstructor() && !methodBinding.getReturnType().isPrimitive()) {
ConstraintVariable returnTypeBindingVariable = fConstraintVariableFactory.makeReturnTypeVariable(methodBinding);
ConstraintVariable returnTypeVariable = fConstraintVariableFactory.makeTypeVariable(declaration.getReturnType2());
ITypeConstraint[] defines = fTypeConstraintFactory.createDefinesConstraint(returnTypeBindingVariable, returnTypeVariable);
result.addAll(Arrays.asList(defines));
}
for (int i = 0, n = declaration.parameters().size(); i < n; i++) {
SingleVariableDeclaration paramDecl = (SingleVariableDeclaration) declaration.parameters().get(i);
ConstraintVariable parameterTypeVariable = fConstraintVariableFactory.makeParameterTypeVariable(methodBinding, i);
ConstraintVariable parameterNameVariable = fConstraintVariableFactory.makeExpressionOrTypeVariable(paramDecl.getName(), getContext());
ITypeConstraint[] constraint = fTypeConstraintFactory.createDefinesConstraint(parameterTypeVariable, parameterNameVariable);
result.addAll(Arrays.asList(constraint));
}
if (MethodChecks.isVirtual(methodBinding)) {
Collection<ITypeConstraint> constraintsForOverriding = getConstraintsForOverriding(methodBinding);
result.addAll(constraintsForOverriding);
}
return result.toArray(new ITypeConstraint[result.size()]);
}
use of org.eclipse.jdt.core.dom.SingleVariableDeclaration in project che by eclipse.
the class FullConstraintCreator method create.
@Override
public ITypeConstraint[] create(CatchClause node) {
SingleVariableDeclaration exception = node.getException();
ConstraintVariable nameVariable = fConstraintVariableFactory.makeExpressionOrTypeVariable(exception.getName(), getContext());
ITypeConstraint[] defines = fTypeConstraintFactory.createDefinesConstraint(nameVariable, fConstraintVariableFactory.makeTypeVariable(exception.getType()));
//$NON-NLS-1$
ITypeBinding throwable = node.getAST().resolveWellKnownType("java.lang.Throwable");
ITypeConstraint[] catchBound = fTypeConstraintFactory.createSubtypeConstraint(nameVariable, fConstraintVariableFactory.makeRawBindingVariable(throwable));
ArrayList<ITypeConstraint> result = new ArrayList<ITypeConstraint>();
result.addAll(Arrays.asList(defines));
result.addAll(Arrays.asList(catchBound));
return result.toArray(new ITypeConstraint[result.size()]);
}
use of org.eclipse.jdt.core.dom.SingleVariableDeclaration in project che by eclipse.
the class JavadocUtil method addParamJavadoc.
/**
* Adds a "param" javadoc tag for a new last parameter if necessary.
* @param parameterName
* @param methodDeclaration
* @param astRewrite
* @param javaProject
* @param groupDescription
*/
public static void addParamJavadoc(String parameterName, MethodDeclaration methodDeclaration, ASTRewrite astRewrite, IJavaProject javaProject, TextEditGroup groupDescription) {
if (!shouldAddParamJavadoc(methodDeclaration))
return;
ListRewrite tagsRewrite = astRewrite.getListRewrite(methodDeclaration.getJavadoc(), Javadoc.TAGS_PROPERTY);
HashSet<String> leadingNames = new HashSet<String>();
for (Iterator<SingleVariableDeclaration> iter = methodDeclaration.parameters().iterator(); iter.hasNext(); ) {
SingleVariableDeclaration curr = iter.next();
leadingNames.add(curr.getName().getIdentifier());
}
TagElement parameterTag = createParamTag(parameterName, astRewrite.getAST(), javaProject);
JavadocTagsSubProcessor.insertTag(tagsRewrite, parameterTag, leadingNames, groupDescription);
}
use of org.eclipse.jdt.core.dom.SingleVariableDeclaration in project che by eclipse.
the class ReferenceFinderUtil method getTypesUsedInDeclaration.
//set of ITypeBindings
public static Set<ITypeBinding> getTypesUsedInDeclaration(MethodDeclaration methodDeclaration) {
if (methodDeclaration == null)
return new HashSet<ITypeBinding>(0);
Set<ITypeBinding> result = new HashSet<ITypeBinding>();
ITypeBinding binding = null;
Type returnType = methodDeclaration.getReturnType2();
if (returnType != null) {
binding = returnType.resolveBinding();
if (binding != null)
result.add(binding);
}
for (Iterator<SingleVariableDeclaration> iter = methodDeclaration.parameters().iterator(); iter.hasNext(); ) {
binding = iter.next().getType().resolveBinding();
if (binding != null)
result.add(binding);
}
for (Iterator<Type> iter = methodDeclaration.thrownExceptionTypes().iterator(); iter.hasNext(); ) {
binding = iter.next().resolveBinding();
if (binding != null)
result.add(binding);
}
return result;
}
Aggregations