use of org.eclipse.jdt.core.dom.ClassInstanceCreation 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.ClassInstanceCreation 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.ClassInstanceCreation in project che by eclipse.
the class ConvertAnonymousToNestedRefactoring method getTypeParameters.
private ITypeBinding[] getTypeParameters() {
final List<ITypeBinding> parameters = new ArrayList<ITypeBinding>(4);
final ClassInstanceCreation creation = (ClassInstanceCreation) fAnonymousInnerClassNode.getParent();
if (fDeclareStatic) {
final TypeVariableFinder finder = new TypeVariableFinder();
creation.accept(finder);
return finder.getResult();
} else {
final MethodDeclaration declaration = getEnclosingMethodDeclaration(creation);
if (declaration != null) {
ITypeBinding binding = null;
TypeParameter parameter = null;
for (final Iterator<TypeParameter> iterator = declaration.typeParameters().iterator(); iterator.hasNext(); ) {
parameter = iterator.next();
binding = parameter.resolveBinding();
if (binding != null)
parameters.add(binding);
}
}
}
final TypeVariableFinder finder = new TypeVariableFinder();
creation.accept(finder);
final ITypeBinding[] variables = finder.getResult();
final List<ITypeBinding> remove = new ArrayList<ITypeBinding>(4);
boolean match = false;
ITypeBinding binding = null;
ITypeBinding variable = null;
for (final Iterator<ITypeBinding> iterator = parameters.iterator(); iterator.hasNext(); ) {
match = false;
binding = iterator.next();
for (int index = 0; index < variables.length; index++) {
variable = variables[index];
if (variable.equals(binding))
match = true;
}
if (!match)
remove.add(binding);
}
parameters.removeAll(remove);
final ITypeBinding[] result = new ITypeBinding[parameters.size()];
parameters.toArray(result);
return result;
}
use of org.eclipse.jdt.core.dom.ClassInstanceCreation in project flux by eclipse.
the class ASTNodes method getTargetType.
/**
* Derives the target type defined at the location of the given expression if the target context
* supports poly expressions.
*
* @param expression the expression at whose location the target type is required
* @return the type binding of the target type defined at the location of the given expression
* if the target context supports poly expressions, or <code>null</code> if the target
* type could not be derived
*
* @since 3.10
*/
public static ITypeBinding getTargetType(Expression expression) {
ASTNode parent = expression.getParent();
StructuralPropertyDescriptor locationInParent = expression.getLocationInParent();
if (locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY || locationInParent == SingleVariableDeclaration.INITIALIZER_PROPERTY) {
return ((VariableDeclaration) parent).getName().resolveTypeBinding();
} else if (locationInParent == Assignment.RIGHT_HAND_SIDE_PROPERTY) {
return ((Assignment) parent).getLeftHandSide().resolveTypeBinding();
} else if (locationInParent == ReturnStatement.EXPRESSION_PROPERTY) {
return getTargetTypeForReturnStmt((ReturnStatement) parent);
} else if (locationInParent == ArrayInitializer.EXPRESSIONS_PROPERTY) {
return getTargetTypeForArrayInitializer((ArrayInitializer) parent);
} else if (locationInParent == MethodInvocation.ARGUMENTS_PROPERTY) {
MethodInvocation methodInvocation = (MethodInvocation) parent;
IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
if (methodBinding != null) {
return getParameterTypeBinding(expression, methodInvocation.arguments(), methodBinding);
}
} else if (locationInParent == SuperMethodInvocation.ARGUMENTS_PROPERTY) {
SuperMethodInvocation superMethodInvocation = (SuperMethodInvocation) parent;
IMethodBinding superMethodBinding = superMethodInvocation.resolveMethodBinding();
if (superMethodBinding != null) {
return getParameterTypeBinding(expression, superMethodInvocation.arguments(), superMethodBinding);
}
} else if (locationInParent == ConstructorInvocation.ARGUMENTS_PROPERTY) {
ConstructorInvocation constructorInvocation = (ConstructorInvocation) parent;
IMethodBinding constructorBinding = constructorInvocation.resolveConstructorBinding();
if (constructorBinding != null) {
return getParameterTypeBinding(expression, constructorInvocation.arguments(), constructorBinding);
}
} else if (locationInParent == SuperConstructorInvocation.ARGUMENTS_PROPERTY) {
SuperConstructorInvocation superConstructorInvocation = (SuperConstructorInvocation) parent;
IMethodBinding superConstructorBinding = superConstructorInvocation.resolveConstructorBinding();
if (superConstructorBinding != null) {
return getParameterTypeBinding(expression, superConstructorInvocation.arguments(), superConstructorBinding);
}
} else if (locationInParent == ClassInstanceCreation.ARGUMENTS_PROPERTY) {
ClassInstanceCreation creation = (ClassInstanceCreation) parent;
IMethodBinding creationBinding = creation.resolveConstructorBinding();
if (creationBinding != null) {
return getParameterTypeBinding(expression, creation.arguments(), creationBinding);
}
} else if (locationInParent == EnumConstantDeclaration.ARGUMENTS_PROPERTY) {
EnumConstantDeclaration enumConstantDecl = (EnumConstantDeclaration) parent;
IMethodBinding enumConstructorBinding = enumConstantDecl.resolveConstructorBinding();
if (enumConstructorBinding != null) {
return getParameterTypeBinding(expression, enumConstantDecl.arguments(), enumConstructorBinding);
}
} else if (locationInParent == LambdaExpression.BODY_PROPERTY) {
IMethodBinding methodBinding = ((LambdaExpression) parent).resolveMethodBinding();
if (methodBinding != null) {
return methodBinding.getReturnType();
}
} else if (locationInParent == ConditionalExpression.THEN_EXPRESSION_PROPERTY || locationInParent == ConditionalExpression.ELSE_EXPRESSION_PROPERTY) {
return getTargetType((ConditionalExpression) parent);
} else if (locationInParent == CastExpression.EXPRESSION_PROPERTY) {
return ((CastExpression) parent).getType().resolveBinding();
} else if (locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) {
return getTargetType((ParenthesizedExpression) parent);
}
return null;
}
use of org.eclipse.jdt.core.dom.ClassInstanceCreation in project flux by eclipse.
the class QuickAssistProcessor method getInferDiamondArgumentsProposal.
public static boolean getInferDiamondArgumentsProposal(IInvocationContext context, ASTNode node, IProblemLocation[] locations, Collection<ICommandAccess> resultingCollections) {
// don't add if already added as quick fix
if (containsMatchingProblem(locations, IProblem.DiamondNotBelow17))
return false;
ParameterizedType createdType = null;
if (node instanceof Name) {
Name name = ASTNodes.getTopMostName((Name) node);
if (name.getLocationInParent() == SimpleType.NAME_PROPERTY || name.getLocationInParent() == NameQualifiedType.NAME_PROPERTY) {
ASTNode type = name.getParent();
if (type.getLocationInParent() == ParameterizedType.TYPE_PROPERTY) {
createdType = (ParameterizedType) type.getParent();
if (createdType.getLocationInParent() != ClassInstanceCreation.TYPE_PROPERTY) {
return false;
}
}
}
} else if (node instanceof ParameterizedType) {
createdType = (ParameterizedType) node;
if (createdType.getLocationInParent() != ClassInstanceCreation.TYPE_PROPERTY) {
return false;
}
} else if (node instanceof ClassInstanceCreation) {
ClassInstanceCreation creation = (ClassInstanceCreation) node;
Type type = creation.getType();
if (type instanceof ParameterizedType) {
createdType = (ParameterizedType) type;
}
}
// }
return true;
}
Aggregations