use of org.eclipse.jdt.core.dom.ArrayInitializer in project eclipse-pmd by acanda.
the class SuppressWarningsQuickFix method createArrayInitializerAndCopyExpressions.
@SuppressWarnings("unchecked")
private ArrayInitializer createArrayInitializerAndCopyExpressions(final AST ast, final ArrayInitializer existingArray) {
final ArrayInitializer array;
array = (ArrayInitializer) ast.createInstance(ArrayInitializer.class);
final List<Expression> expressions = array.expressions();
final List<Expression> existingExpressions = existingArray.expressions();
for (final Expression existingExpression : existingExpressions) {
expressions.add(ASTUtil.copy(existingExpression));
}
return array;
}
use of org.eclipse.jdt.core.dom.ArrayInitializer in project eclipse.jdt.ls by eclipse.
the class TypeMismatchSubProcessor method addTypeMismatchProposals.
public static void addTypeMismatchProposals(IInvocationContext context, IProblemLocation problem, Collection<CUCorrectionProposal> proposals) throws CoreException {
ICompilationUnit cu = context.getCompilationUnit();
CompilationUnit astRoot = context.getASTRoot();
AST ast = astRoot.getAST();
ASTNode selectedNode = problem.getCoveredNode(astRoot);
if (!(selectedNode instanceof Expression)) {
return;
}
Expression nodeToCast = (Expression) selectedNode;
Name receiverNode = null;
ITypeBinding castTypeBinding = null;
int parentNodeType = selectedNode.getParent().getNodeType();
if (parentNodeType == ASTNode.ASSIGNMENT) {
Assignment assign = (Assignment) selectedNode.getParent();
Expression leftHandSide = assign.getLeftHandSide();
if (selectedNode.equals(leftHandSide)) {
nodeToCast = assign.getRightHandSide();
}
castTypeBinding = assign.getLeftHandSide().resolveTypeBinding();
if (leftHandSide instanceof Name) {
receiverNode = (Name) leftHandSide;
} else if (leftHandSide instanceof FieldAccess) {
receiverNode = ((FieldAccess) leftHandSide).getName();
}
} else if (parentNodeType == ASTNode.VARIABLE_DECLARATION_FRAGMENT) {
VariableDeclarationFragment frag = (VariableDeclarationFragment) selectedNode.getParent();
if (selectedNode.equals(frag.getName()) || selectedNode.equals(frag.getInitializer())) {
nodeToCast = frag.getInitializer();
castTypeBinding = ASTNodes.getType(frag).resolveBinding();
receiverNode = frag.getName();
}
} else if (parentNodeType == ASTNode.MEMBER_VALUE_PAIR) {
receiverNode = ((MemberValuePair) selectedNode.getParent()).getName();
castTypeBinding = ASTResolving.guessBindingForReference(nodeToCast);
} else if (parentNodeType == ASTNode.SINGLE_MEMBER_ANNOTATION) {
// use the type name
receiverNode = ((SingleMemberAnnotation) selectedNode.getParent()).getTypeName();
castTypeBinding = ASTResolving.guessBindingForReference(nodeToCast);
} else {
// try to find the binding corresponding to 'castTypeName'
castTypeBinding = ASTResolving.guessBindingForReference(nodeToCast);
}
if (castTypeBinding == null) {
return;
}
ITypeBinding currBinding = nodeToCast.resolveTypeBinding();
if (currBinding == null && nodeToCast instanceof MethodInvocation) {
IMethodBinding methodBinding = ((MethodInvocation) nodeToCast).resolveMethodBinding();
if (methodBinding != null) {
currBinding = methodBinding.getReturnType();
}
}
if (!(nodeToCast instanceof ArrayInitializer)) {
ITypeBinding castFixType = null;
if (currBinding == null || castTypeBinding.isCastCompatible(currBinding) || nodeToCast instanceof CastExpression) {
castFixType = castTypeBinding;
} else if (JavaModelUtil.is50OrHigher(cu.getJavaProject())) {
ITypeBinding boxUnboxedTypeBinding = boxUnboxPrimitives(castTypeBinding, currBinding, ast);
if (boxUnboxedTypeBinding != castTypeBinding && boxUnboxedTypeBinding.isCastCompatible(currBinding)) {
castFixType = boxUnboxedTypeBinding;
}
}
if (castFixType != null) {
proposals.add(createCastProposal(context, castFixType, nodeToCast, IProposalRelevance.CREATE_CAST));
}
}
// $NON-NLS-1$
boolean nullOrVoid = currBinding == null || "void".equals(currBinding.getName());
// change method return statement to actual type
if (!nullOrVoid && parentNodeType == ASTNode.RETURN_STATEMENT) {
BodyDeclaration decl = ASTResolving.findParentBodyDeclaration(selectedNode);
if (decl instanceof MethodDeclaration) {
MethodDeclaration methodDeclaration = (MethodDeclaration) decl;
currBinding = Bindings.normalizeTypeBinding(currBinding);
if (currBinding == null) {
// $NON-NLS-1$
currBinding = ast.resolveWellKnownType("java.lang.Object");
}
if (currBinding.isWildcardType()) {
currBinding = ASTResolving.normalizeWildcardType(currBinding, true, ast);
}
ASTRewrite rewrite = ASTRewrite.create(ast);
String label = Messages.format(CorrectionMessages.TypeMismatchSubProcessor_changereturntype_description, BasicElementLabels.getJavaElementName(currBinding.getName()));
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.CHANGE_METHOD_RETURN_TYPE);
ImportRewrite imports = proposal.createImportRewrite(astRoot);
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(decl, imports);
Type newReturnType = imports.addImport(currBinding, ast, importRewriteContext, TypeLocation.RETURN_TYPE);
rewrite.replace(methodDeclaration.getReturnType2(), newReturnType, null);
proposals.add(proposal);
}
}
if (!nullOrVoid && receiverNode != null) {
currBinding = Bindings.normalizeTypeBinding(currBinding);
if (currBinding == null) {
// $NON-NLS-1$
currBinding = ast.resolveWellKnownType("java.lang.Object");
}
if (currBinding.isWildcardType()) {
currBinding = ASTResolving.normalizeWildcardType(currBinding, true, ast);
}
addChangeSenderTypeProposals(context, receiverNode, currBinding, true, IProposalRelevance.CHANGE_TYPE_OF_RECEIVER_NODE, proposals);
}
addChangeSenderTypeProposals(context, nodeToCast, castTypeBinding, false, IProposalRelevance.CHANGE_TYPE_OF_NODE_TO_CAST, proposals);
if (castTypeBinding == ast.resolveWellKnownType("boolean") && currBinding != null && !currBinding.isPrimitive() && !Bindings.isVoidType(currBinding)) {
// $NON-NLS-1$
String label = CorrectionMessages.TypeMismatchSubProcessor_insertnullcheck_description;
ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST());
InfixExpression expression = ast.newInfixExpression();
expression.setLeftOperand((Expression) rewrite.createMoveTarget(nodeToCast));
expression.setRightOperand(ast.newNullLiteral());
expression.setOperator(InfixExpression.Operator.NOT_EQUALS);
rewrite.replace(nodeToCast, expression, null);
proposals.add(new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INSERT_NULL_CHECK));
}
}
use of org.eclipse.jdt.core.dom.ArrayInitializer in project whole by wholeplatform.
the class ActionFactoryBuilder method replaceAction.
private void replaceAction(String concreteType) {
String transformer;
if (StringUtils.toSimpleName(generator.baseFragmentName()).equals(concreteType)) {
transformer = "fragment";
} else if (StringUtils.toSimpleName(generator.metaFragmentName()).equals(concreteType)) {
transformer = "fragment";
} else {
transformer = "copy";
}
ArrayInitializer arrayInitializer = ast.newArrayInitializer();
arrayInitializer.expressions().add(newMethodInvocation("pf", "replaceFeatureType"));
arrayInitializer.expressions().add(newLiteral(concreteType));
arrayInitializer.expressions().add(newLiteral(concreteType));
arrayInitializer.expressions().add(ast.newSimpleName(transformer));
replaceActionsList.add(arrayInitializer);
}
use of org.eclipse.jdt.core.dom.ArrayInitializer in project whole by wholeplatform.
the class CommandFactoryBuilder method addOverSimpleConstraint.
public void addOverSimpleConstraint(String dndType, String simpleType) {
ArrayInitializer arrayInitializer = ast.newArrayInitializer();
arrayInitializer.expressions().add(newTypeLiteral(dndType));
arrayInitializer.expressions().add(newTypeLiteral(simpleType));
addOverSimpleConstraintsList.add(arrayInitializer);
}
use of org.eclipse.jdt.core.dom.ArrayInitializer in project evosuite by EvoSuite.
the class CodeGenerator method createCallMethodOrNewInstanceCallStmt.
@SuppressWarnings("unchecked")
private MethodInvocation createCallMethodOrNewInstanceCallStmt(final boolean isConstructorCall, final AST ast, final String varName, final String targetTypeName, final String methodName, final Object[] methodArgs, final org.objectweb.asm.Type[] paramTypes) {
// -- construct getField() call
final MethodInvocation callMethodCall = ast.newMethodInvocation();
if (isConstructorCall) {
callMethodCall.setName(ast.newSimpleName("newInstance"));
} else {
callMethodCall.setName(ast.newSimpleName("callMethod"));
}
StringLiteral stringLiteral = ast.newStringLiteral();
stringLiteral.setLiteralValue(targetTypeName);
// class name
callMethodCall.arguments().add(stringLiteral);
if (!isConstructorCall) {
stringLiteral = ast.newStringLiteral();
stringLiteral.setLiteralValue(methodName);
// method name
callMethodCall.arguments().add(stringLiteral);
if (varName == null) {
// static call -> no receiver
callMethodCall.arguments().add(ast.newNullLiteral());
} else {
// receiver
callMethodCall.arguments().add(ast.newSimpleName(varName));
}
}
// method arguments
ArrayCreation arrCreation = ast.newArrayCreation();
arrCreation.setType(ast.newArrayType(ast.newSimpleType(ast.newSimpleName("Object"))));
ArrayInitializer arrInit = ast.newArrayInitializer();
arrCreation.setInitializer(arrInit);
callMethodCall.arguments().add(arrCreation);
// is either an oid or null
Integer arg;
for (int i = 0; i < methodArgs.length; i++) {
arg = (Integer) methodArgs[i];
if (arg == null) {
arrInit.expressions().add(ast.newNullLiteral());
} else {
arrInit.expressions().add(ast.newSimpleName(this.oidToVarMapping.get(arg)));
}
}
// paramTypes
arrCreation = ast.newArrayCreation();
arrCreation.setType(ast.newArrayType(ast.newSimpleType(ast.newSimpleName("Class"))));
arrInit = ast.newArrayInitializer();
arrCreation.setInitializer(arrInit);
callMethodCall.arguments().add(arrCreation);
org.objectweb.asm.Type type;
for (int i = 0; i < paramTypes.length; i++) {
type = paramTypes[i];
if (type.equals(org.objectweb.asm.Type.BOOLEAN_TYPE)) {
FieldAccess facc = ast.newFieldAccess();
facc.setName(ast.newSimpleName("TYPE"));
facc.setExpression(ast.newSimpleName("Boolean"));
arrInit.expressions().add(facc);
} else if (type.equals(org.objectweb.asm.Type.BYTE_TYPE)) {
FieldAccess facc = ast.newFieldAccess();
facc.setName(ast.newSimpleName("TYPE"));
facc.setExpression(ast.newSimpleName("Byte"));
arrInit.expressions().add(facc);
} else if (type.equals(org.objectweb.asm.Type.CHAR_TYPE)) {
FieldAccess facc = ast.newFieldAccess();
facc.setName(ast.newSimpleName("TYPE"));
facc.setExpression(ast.newSimpleName("Character"));
arrInit.expressions().add(facc);
} else if (type.equals(org.objectweb.asm.Type.DOUBLE_TYPE)) {
FieldAccess facc = ast.newFieldAccess();
facc.setName(ast.newSimpleName("TYPE"));
facc.setExpression(ast.newSimpleName("Double"));
arrInit.expressions().add(facc);
} else if (type.equals(org.objectweb.asm.Type.FLOAT_TYPE)) {
FieldAccess facc = ast.newFieldAccess();
facc.setName(ast.newSimpleName("TYPE"));
facc.setExpression(ast.newSimpleName("Float"));
arrInit.expressions().add(facc);
} else if (type.equals(org.objectweb.asm.Type.INT_TYPE)) {
FieldAccess facc = ast.newFieldAccess();
facc.setName(ast.newSimpleName("TYPE"));
facc.setExpression(ast.newSimpleName("Integer"));
arrInit.expressions().add(facc);
} else if (type.equals(org.objectweb.asm.Type.LONG_TYPE)) {
FieldAccess facc = ast.newFieldAccess();
facc.setName(ast.newSimpleName("TYPE"));
facc.setExpression(ast.newSimpleName("Long"));
arrInit.expressions().add(facc);
} else if (type.equals(org.objectweb.asm.Type.SHORT_TYPE)) {
FieldAccess facc = ast.newFieldAccess();
facc.setName(ast.newSimpleName("TYPE"));
facc.setExpression(ast.newSimpleName("Short"));
arrInit.expressions().add(facc);
} else {
final TypeLiteral clazz = ast.newTypeLiteral();
clazz.setType(ast.newSimpleType(ast.newName(type.getClassName())));
arrInit.expressions().add(clazz);
}
}
return callMethodCall;
}
Aggregations