use of org.eclipse.jdt.core.dom.ArrayInitializer in project eclipse.jdt.ls by eclipse.
the class CodeAnalyzer method checkSelectedNodes.
@Override
protected final void checkSelectedNodes() {
super.checkSelectedNodes();
RefactoringStatus status = getStatus();
if (status.hasFatalError()) {
return;
}
ASTNode node = getFirstSelectedNode();
if (node instanceof ArrayInitializer) {
status.addFatalError(RefactoringCoreMessages.CodeAnalyzer_array_initializer, JavaStatusContext.create(fCUnit, node));
}
}
use of org.eclipse.jdt.core.dom.ArrayInitializer in project whole by wholeplatform.
the class ActionFactoryBuilder method addAction.
public void addAction(String componentType) {
if (!componentTypes.add(componentType))
return;
ArrayInitializer arrayInitializer = ast.newArrayInitializer();
arrayInitializer.expressions().add(newMethodInvocation("pf", "insertFeatureType"));
arrayInitializer.expressions().add(newLiteral(componentType));
arrayInitializer.expressions().add(newLiteral(componentType));
addActionsList.add(arrayInitializer);
}
use of org.eclipse.jdt.core.dom.ArrayInitializer in project whole by wholeplatform.
the class CommandFactoryBuilder method addOverCompositeConstraint.
public void addOverCompositeConstraint(String componentType, String compositeType) {
ArrayInitializer arrayInitializer = ast.newArrayInitializer();
arrayInitializer.expressions().add(newTypeLiteral(componentType));
arrayInitializer.expressions().add(newTypeLiteral(compositeType));
addOverCompositeConstraintsList.add(arrayInitializer);
}
use of org.eclipse.jdt.core.dom.ArrayInitializer in project flux by eclipse.
the class ASTResolving method getPossibleReferenceBinding.
private static ITypeBinding getPossibleReferenceBinding(ASTNode node) {
ASTNode parent = node.getParent();
switch(parent.getNodeType()) {
case ASTNode.ASSIGNMENT:
Assignment assignment = (Assignment) parent;
if (node.equals(assignment.getLeftHandSide())) {
// field write access: xx= expression
return assignment.getRightHandSide().resolveTypeBinding();
}
// read access
return assignment.getLeftHandSide().resolveTypeBinding();
case ASTNode.INFIX_EXPRESSION:
InfixExpression infix = (InfixExpression) parent;
InfixExpression.Operator op = infix.getOperator();
if (op == InfixExpression.Operator.CONDITIONAL_AND || op == InfixExpression.Operator.CONDITIONAL_OR) {
//$NON-NLS-1$
return infix.getAST().resolveWellKnownType("boolean");
} else if (op == InfixExpression.Operator.LEFT_SHIFT || op == InfixExpression.Operator.RIGHT_SHIFT_UNSIGNED || op == InfixExpression.Operator.RIGHT_SHIFT_SIGNED) {
//$NON-NLS-1$
return infix.getAST().resolveWellKnownType("int");
}
if (node.equals(infix.getLeftOperand())) {
// xx operation expression
ITypeBinding rigthHandBinding = infix.getRightOperand().resolveTypeBinding();
if (rigthHandBinding != null) {
return rigthHandBinding;
}
} else {
// expression operation xx
ITypeBinding leftHandBinding = infix.getLeftOperand().resolveTypeBinding();
if (leftHandBinding != null) {
return leftHandBinding;
}
}
if (op != InfixExpression.Operator.EQUALS && op != InfixExpression.Operator.NOT_EQUALS) {
//$NON-NLS-1$
return infix.getAST().resolveWellKnownType("int");
}
break;
case ASTNode.INSTANCEOF_EXPRESSION:
InstanceofExpression instanceofExpression = (InstanceofExpression) parent;
return instanceofExpression.getRightOperand().resolveBinding();
case ASTNode.VARIABLE_DECLARATION_FRAGMENT:
VariableDeclarationFragment frag = (VariableDeclarationFragment) parent;
if (frag.getInitializer().equals(node)) {
return frag.getName().resolveTypeBinding();
}
break;
case ASTNode.SUPER_METHOD_INVOCATION:
SuperMethodInvocation superMethodInvocation = (SuperMethodInvocation) parent;
IMethodBinding superMethodBinding = ASTNodes.getMethodBinding(superMethodInvocation.getName());
if (superMethodBinding != null) {
return getParameterTypeBinding(node, superMethodInvocation.arguments(), superMethodBinding);
}
break;
case ASTNode.METHOD_INVOCATION:
MethodInvocation methodInvocation = (MethodInvocation) parent;
IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
if (methodBinding != null) {
return getParameterTypeBinding(node, methodInvocation.arguments(), methodBinding);
}
break;
case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
{
SuperConstructorInvocation superInvocation = (SuperConstructorInvocation) parent;
IMethodBinding superBinding = superInvocation.resolveConstructorBinding();
if (superBinding != null) {
return getParameterTypeBinding(node, superInvocation.arguments(), superBinding);
}
break;
}
case ASTNode.CONSTRUCTOR_INVOCATION:
{
ConstructorInvocation constrInvocation = (ConstructorInvocation) parent;
IMethodBinding constrBinding = constrInvocation.resolveConstructorBinding();
if (constrBinding != null) {
return getParameterTypeBinding(node, constrInvocation.arguments(), constrBinding);
}
break;
}
case ASTNode.CLASS_INSTANCE_CREATION:
{
ClassInstanceCreation creation = (ClassInstanceCreation) parent;
IMethodBinding creationBinding = creation.resolveConstructorBinding();
if (creationBinding != null) {
return getParameterTypeBinding(node, creation.arguments(), creationBinding);
}
break;
}
case ASTNode.PARENTHESIZED_EXPRESSION:
return guessBindingForReference(parent);
case ASTNode.ARRAY_ACCESS:
if (((ArrayAccess) parent).getIndex().equals(node)) {
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("int");
} else {
ITypeBinding parentBinding = getPossibleReferenceBinding(parent);
if (parentBinding == null) {
//$NON-NLS-1$
parentBinding = parent.getAST().resolveWellKnownType("java.lang.Object");
}
return parentBinding.createArrayType(1);
}
case ASTNode.ARRAY_CREATION:
if (((ArrayCreation) parent).dimensions().contains(node)) {
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("int");
}
break;
case ASTNode.ARRAY_INITIALIZER:
ASTNode initializerParent = parent.getParent();
int dim = 1;
while (initializerParent instanceof ArrayInitializer) {
initializerParent = initializerParent.getParent();
dim++;
}
Type creationType = null;
if (initializerParent instanceof ArrayCreation) {
creationType = ((ArrayCreation) initializerParent).getType();
} else if (initializerParent instanceof VariableDeclaration) {
VariableDeclaration varDecl = (VariableDeclaration) initializerParent;
creationType = ASTNodes.getType(varDecl);
dim -= varDecl.getExtraDimensions();
} else if (initializerParent instanceof MemberValuePair) {
String name = ((MemberValuePair) initializerParent).getName().getIdentifier();
IMethodBinding annotMember = findAnnotationMember((Annotation) initializerParent.getParent(), name);
if (annotMember != null) {
return getReducedDimensionBinding(annotMember.getReturnType(), dim);
}
}
if (creationType instanceof ArrayType) {
ITypeBinding creationTypeBinding = ((ArrayType) creationType).resolveBinding();
if (creationTypeBinding != null) {
return Bindings.getComponentType(creationTypeBinding, dim);
}
}
break;
case ASTNode.CONDITIONAL_EXPRESSION:
ConditionalExpression expression = (ConditionalExpression) parent;
if (node.equals(expression.getExpression())) {
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("boolean");
}
if (node.equals(expression.getElseExpression())) {
return expression.getThenExpression().resolveTypeBinding();
}
return expression.getElseExpression().resolveTypeBinding();
case ASTNode.POSTFIX_EXPRESSION:
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("int");
case ASTNode.PREFIX_EXPRESSION:
if (((PrefixExpression) parent).getOperator() == PrefixExpression.Operator.NOT) {
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("boolean");
}
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("int");
case ASTNode.IF_STATEMENT:
case ASTNode.WHILE_STATEMENT:
case ASTNode.DO_STATEMENT:
if (node instanceof Expression) {
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("boolean");
}
break;
case ASTNode.SWITCH_STATEMENT:
if (((SwitchStatement) parent).getExpression().equals(node)) {
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("int");
}
break;
case ASTNode.RETURN_STATEMENT:
MethodDeclaration decl = ASTResolving.findParentMethodDeclaration(parent);
if (decl != null && !decl.isConstructor()) {
return decl.getReturnType2().resolveBinding();
}
LambdaExpression lambdaExpr = ASTResolving.findEnclosingLambdaExpression(parent);
if (lambdaExpr != null) {
IMethodBinding lambdaMethodBinding = lambdaExpr.resolveMethodBinding();
if (lambdaMethodBinding != null && lambdaMethodBinding.getReturnType() != null) {
return lambdaMethodBinding.getReturnType();
}
}
break;
case ASTNode.CAST_EXPRESSION:
return ((CastExpression) parent).getType().resolveBinding();
case ASTNode.THROW_STATEMENT:
case ASTNode.CATCH_CLAUSE:
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("java.lang.Exception");
case ASTNode.FIELD_ACCESS:
if (node.equals(((FieldAccess) parent).getName())) {
return getPossibleReferenceBinding(parent);
}
break;
case ASTNode.SUPER_FIELD_ACCESS:
return getPossibleReferenceBinding(parent);
case ASTNode.QUALIFIED_NAME:
if (node.equals(((QualifiedName) parent).getName())) {
return getPossibleReferenceBinding(parent);
}
break;
case ASTNode.SWITCH_CASE:
if (node.equals(((SwitchCase) parent).getExpression()) && parent.getParent() instanceof SwitchStatement) {
return ((SwitchStatement) parent.getParent()).getExpression().resolveTypeBinding();
}
break;
case ASTNode.ASSERT_STATEMENT:
if (node.getLocationInParent() == AssertStatement.EXPRESSION_PROPERTY) {
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("boolean");
}
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("java.lang.String");
case ASTNode.SINGLE_MEMBER_ANNOTATION:
{
//$NON-NLS-1$
IMethodBinding annotMember = findAnnotationMember((Annotation) parent, "value");
if (annotMember != null) {
return annotMember.getReturnType();
}
break;
}
case ASTNode.MEMBER_VALUE_PAIR:
{
String name = ((MemberValuePair) parent).getName().getIdentifier();
IMethodBinding annotMember = findAnnotationMember((Annotation) parent.getParent(), name);
if (annotMember != null) {
return annotMember.getReturnType();
}
break;
}
default:
}
return null;
}
use of org.eclipse.jdt.core.dom.ArrayInitializer in project flux by eclipse.
the class QuickAssistProcessor method getSplitVariableProposals.
private static boolean getSplitVariableProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
VariableDeclarationFragment fragment;
if (node instanceof VariableDeclarationFragment) {
fragment = (VariableDeclarationFragment) node;
} else if (node.getLocationInParent() == VariableDeclarationFragment.NAME_PROPERTY) {
fragment = (VariableDeclarationFragment) node.getParent();
} else {
return false;
}
if (fragment.getInitializer() == null) {
return false;
}
Statement statement;
ASTNode fragParent = fragment.getParent();
if (fragParent instanceof VariableDeclarationStatement) {
statement = (VariableDeclarationStatement) fragParent;
} else if (fragParent instanceof VariableDeclarationExpression) {
if (fragParent.getLocationInParent() == TryStatement.RESOURCES_PROPERTY) {
return false;
}
statement = (Statement) fragParent.getParent();
} else {
return false;
}
// statement is ForStatement or VariableDeclarationStatement
ASTNode statementParent = statement.getParent();
StructuralPropertyDescriptor property = statement.getLocationInParent();
if (!property.isChildListProperty()) {
return false;
}
List<? extends ASTNode> list = ASTNodes.getChildListProperty(statementParent, (ChildListPropertyDescriptor) property);
if (resultingCollections == null) {
return true;
}
AST ast = statement.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
String label = CorrectionMessages.QuickAssistProcessor_splitdeclaration_description;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_LOCAL);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.SPLIT_VARIABLE_DECLARATION);
boolean commandConflict = false;
for (Iterator<ICommandAccess> iterator = resultingCollections.iterator(); iterator.hasNext(); ) {
Object completionProposal = iterator.next();
if (completionProposal instanceof ChangeCorrectionProposal) {
if (SPLIT_JOIN_VARIABLE_DECLARATION_ID.equals(((ChangeCorrectionProposal) completionProposal).getCommandId())) {
commandConflict = true;
}
}
}
if (!commandConflict) {
proposal.setCommandId(SPLIT_JOIN_VARIABLE_DECLARATION_ID);
}
Statement newStatement;
int insertIndex = list.indexOf(statement);
Expression placeholder = (Expression) rewrite.createMoveTarget(fragment.getInitializer());
ITypeBinding binding = fragment.getInitializer().resolveTypeBinding();
if (placeholder instanceof ArrayInitializer && binding != null && binding.isArray()) {
ArrayCreation creation = ast.newArrayCreation();
creation.setInitializer((ArrayInitializer) placeholder);
final ITypeBinding componentType = binding.getElementType();
Type type = null;
if (componentType.isPrimitive())
type = ast.newPrimitiveType(PrimitiveType.toCode(componentType.getName()));
else
type = ast.newSimpleType(ast.newSimpleName(componentType.getName()));
creation.setType(ast.newArrayType(type, binding.getDimensions()));
placeholder = creation;
}
Assignment assignment = ast.newAssignment();
assignment.setRightHandSide(placeholder);
assignment.setLeftHandSide(ast.newSimpleName(fragment.getName().getIdentifier()));
if (statement instanceof VariableDeclarationStatement) {
newStatement = ast.newExpressionStatement(assignment);
// add after declaration
insertIndex += 1;
} else {
rewrite.replace(fragment.getParent(), assignment, null);
VariableDeclarationFragment newFrag = ast.newVariableDeclarationFragment();
newFrag.setName(ast.newSimpleName(fragment.getName().getIdentifier()));
newFrag.extraDimensions().addAll(DimensionRewrite.copyDimensions(fragment.extraDimensions(), rewrite));
VariableDeclarationExpression oldVarDecl = (VariableDeclarationExpression) fragParent;
VariableDeclarationStatement newVarDec = ast.newVariableDeclarationStatement(newFrag);
newVarDec.setType((Type) rewrite.createCopyTarget(oldVarDecl.getType()));
newVarDec.modifiers().addAll(ASTNodeFactory.newModifiers(ast, oldVarDecl.getModifiers()));
newStatement = newVarDec;
}
ListRewrite listRewriter = rewrite.getListRewrite(statementParent, (ChildListPropertyDescriptor) property);
listRewriter.insertAt(newStatement, insertIndex, null);
resultingCollections.add(proposal);
return true;
}
Aggregations