use of org.eclipse.jdt.core.dom.ArrayInitializer in project flux by eclipse.
the class QuickAssistProcessor method getArrayInitializerToArrayCreation.
private static boolean getArrayInitializerToArrayCreation(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
if (!(node instanceof ArrayInitializer)) {
return false;
}
ArrayInitializer initializer = (ArrayInitializer) node;
ASTNode parent = initializer.getParent();
while (parent instanceof ArrayInitializer) {
initializer = (ArrayInitializer) parent;
parent = parent.getParent();
}
ITypeBinding typeBinding = initializer.resolveTypeBinding();
if (!(parent instanceof VariableDeclaration) || typeBinding == null || !typeBinding.isArray()) {
return false;
}
if (resultingCollections == null) {
return true;
}
AST ast = node.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
String label = CorrectionMessages.QuickAssistProcessor_typetoarrayInitializer_description;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.ADD_TYPE_TO_ARRAY_INITIALIZER);
ImportRewrite imports = proposal.createImportRewrite(context.getASTRoot());
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(node, imports);
String typeName = imports.addImport(typeBinding, importRewriteContext);
ArrayCreation creation = ast.newArrayCreation();
creation.setInitializer((ArrayInitializer) rewrite.createMoveTarget(initializer));
creation.setType((ArrayType) ASTNodeFactory.newType(ast, typeName));
rewrite.replace(initializer, creation, null);
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.core.dom.ArrayInitializer in project che by eclipse.
the class MissingAnnotationAttributesProposal method newDefaultExpression.
private Expression newDefaultExpression(AST ast, ITypeBinding type, ImportRewriteContext context) {
if (type.isPrimitive()) {
String name = type.getName();
if ("boolean".equals(name)) {
//$NON-NLS-1$
return ast.newBooleanLiteral(false);
} else {
//$NON-NLS-1$
return ast.newNumberLiteral("0");
}
}
if (type == ast.resolveWellKnownType("java.lang.String")) {
//$NON-NLS-1$
return ast.newStringLiteral();
}
if (type.isArray()) {
ArrayInitializer initializer = ast.newArrayInitializer();
initializer.expressions().add(newDefaultExpression(ast, type.getElementType(), context));
return initializer;
}
if (type.isAnnotation()) {
MarkerAnnotation annotation = ast.newMarkerAnnotation();
annotation.setTypeName(ast.newName(getImportRewrite().addImport(type, context)));
return annotation;
}
return ast.newNullLiteral();
}
use of org.eclipse.jdt.core.dom.ArrayInitializer 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.ArrayInitializer in project che by eclipse.
the class PromoteTempToFieldRefactoring method getTempInitializerCopy.
private Expression getTempInitializerCopy(ASTRewrite rewrite) {
final Expression initializer = (Expression) rewrite.createCopyTarget(getTempInitializer());
if (initializer instanceof ArrayInitializer && ASTNodes.getDimensions(fTempDeclarationNode) > 0) {
ArrayCreation arrayCreation = rewrite.getAST().newArrayCreation();
arrayCreation.setType((ArrayType) ASTNodeFactory.newType(rewrite.getAST(), fTempDeclarationNode));
arrayCreation.setInitializer((ArrayInitializer) initializer);
return arrayCreation;
}
return initializer;
}
use of org.eclipse.jdt.core.dom.ArrayInitializer 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()));
}
}
Aggregations