use of org.eclipse.jdt.core.dom.IMethodBinding in project che by eclipse.
the class ConvertAnonymousToNestedRefactoring method copyArguments.
private void copyArguments(CompilationUnitRewrite rewrite, ClassInstanceCreation newClassCreation) {
Iterator<Expression> iter = ((ClassInstanceCreation) fAnonymousInnerClassNode.getParent()).arguments().iterator();
if (!iter.hasNext())
return;
IMethodBinding superConstructorBinding = getSuperConstructorBinding();
ITypeBinding[] parameterTypes = superConstructorBinding.getParameterTypes();
List<Expression> arguments = newClassCreation.arguments();
ASTRewrite astRewrite = rewrite.getASTRewrite();
int last = parameterTypes.length - 1;
for (int i = 0; i < last; i++) {
arguments.add((Expression) astRewrite.createCopyTarget(iter.next()));
}
if (superConstructorBinding.isVarargs()) {
AST ast = astRewrite.getAST();
ArrayCreation arrayCreation = ast.newArrayCreation();
arrayCreation.setType((ArrayType) rewrite.getImportRewrite().addImport(parameterTypes[last], ast));
ArrayInitializer initializer = ast.newArrayInitializer();
arrayCreation.setInitializer(initializer);
arguments.add(arrayCreation);
arguments = initializer.expressions();
}
while (iter.hasNext()) {
arguments.add((Expression) astRewrite.createCopyTarget(iter.next()));
}
}
use of org.eclipse.jdt.core.dom.IMethodBinding in project che by eclipse.
the class ConvertAnonymousToNestedRefactoring method validateInput.
public RefactoringStatus validateInput() {
RefactoringStatus result = Checks.checkTypeName(fClassName, fCu);
if (result.hasFatalError())
return result;
if (fClassNamesUsed.contains(fClassName))
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ConvertAnonymousToNestedRefactoring_type_exists);
IMethodBinding superConstructorBinding = getSuperConstructorBinding();
if (superConstructorBinding == null)
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ConvertAnonymousToNestedRefactoring_compile_errors);
if (fClassName.equals(superConstructorBinding.getDeclaringClass().getName()))
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ConvertAnonymousToNestedRefactoring_another_name);
if (classNameHidesEnclosingType())
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ConvertAnonymousToNestedRefactoring_name_hides);
return result;
}
use of org.eclipse.jdt.core.dom.IMethodBinding in project che by eclipse.
the class ConvertAnonymousToNestedRefactoring method getSuperConstructorBinding.
private IMethodBinding getSuperConstructorBinding() {
//workaround for missing java core functionality - finding a
// super constructor for an anonymous class creation
IMethodBinding anonConstr = ((ClassInstanceCreation) fAnonymousInnerClassNode.getParent()).resolveConstructorBinding();
if (anonConstr == null)
return null;
ITypeBinding superClass = anonConstr.getDeclaringClass().getSuperclass();
IMethodBinding[] superMethods = superClass.getDeclaredMethods();
for (int i = 0; i < superMethods.length; i++) {
IMethodBinding superMethod = superMethods[i];
if (superMethod.isConstructor() && parameterTypesMatch(superMethod, anonConstr))
return superMethod;
}
//there's no way - it must be there
Assert.isTrue(false);
return null;
}
use of org.eclipse.jdt.core.dom.IMethodBinding 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.IMethodBinding in project che by eclipse.
the class FullConstraintCreator method getConstraintsForOverriding.
private Collection<ITypeConstraint> getConstraintsForOverriding(IMethodBinding overridingMethod) {
Collection<ITypeConstraint> result = new ArrayList<ITypeConstraint>();
Set<ITypeBinding> declaringSupertypes = getDeclaringSuperTypes(overridingMethod);
for (Iterator<ITypeBinding> iter = declaringSupertypes.iterator(); iter.hasNext(); ) {
ITypeBinding superType = iter.next();
IMethodBinding overriddenMethod = findMethod(overridingMethod, superType);
//because we asked for declaring types
Assert.isNotNull(overriddenMethod);
if (Bindings.equals(overridingMethod, overriddenMethod))
continue;
ITypeConstraint[] returnTypeConstraint = fTypeConstraintFactory.createEqualsConstraint(fConstraintVariableFactory.makeReturnTypeVariable(overriddenMethod), fConstraintVariableFactory.makeReturnTypeVariable(overridingMethod));
result.addAll(Arrays.asList(returnTypeConstraint));
Assert.isTrue(overriddenMethod.getParameterTypes().length == overridingMethod.getParameterTypes().length);
for (int i = 0, n = overriddenMethod.getParameterTypes().length; i < n; i++) {
ITypeConstraint[] parameterTypeConstraint = fTypeConstraintFactory.createEqualsConstraint(fConstraintVariableFactory.makeParameterTypeVariable(overriddenMethod, i), fConstraintVariableFactory.makeParameterTypeVariable(overridingMethod, i));
result.addAll(Arrays.asList(parameterTypeConstraint));
}
ITypeConstraint[] declaringTypeConstraint = fTypeConstraintFactory.createStrictSubtypeConstraint(fConstraintVariableFactory.makeDeclaringTypeVariable(overridingMethod), fConstraintVariableFactory.makeDeclaringTypeVariable(overriddenMethod));
result.addAll(Arrays.asList(declaringTypeConstraint));
}
return result;
}
Aggregations