use of org.eclipse.jdt.core.dom.ArrayCreation 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.ArrayCreation 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.ArrayCreation in project che by eclipse.
the class InlineTempRefactoring method getModifiedInitializerSource.
private Expression getModifiedInitializerSource(CompilationUnitRewrite rewrite, SimpleName reference) throws JavaModelException {
VariableDeclaration varDecl = getVariableDeclaration();
Expression initializer = varDecl.getInitializer();
ASTNode referenceContext = reference.getParent();
if (Invocations.isResolvedTypeInferredFromExpectedType(initializer)) {
if (!(referenceContext instanceof VariableDeclarationFragment || referenceContext instanceof SingleVariableDeclaration || referenceContext instanceof Assignment)) {
ITypeBinding[] typeArguments = Invocations.getInferredTypeArguments(initializer);
if (typeArguments != null) {
String newSource = createParameterizedInvocation(initializer, typeArguments, rewrite);
return (Expression) rewrite.getASTRewrite().createStringPlaceholder(newSource, initializer.getNodeType());
}
}
}
Expression copy = (Expression) rewrite.getASTRewrite().createCopyTarget(initializer);
AST ast = rewrite.getAST();
if (NecessaryParenthesesChecker.needsParentheses(initializer, reference.getParent(), reference.getLocationInParent())) {
ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
parenthesized.setExpression(copy);
copy = parenthesized;
}
ITypeBinding explicitCast = ASTNodes.getExplicitCast(initializer, reference);
if (explicitCast != null) {
CastExpression cast = ast.newCastExpression();
if (NecessaryParenthesesChecker.needsParentheses(copy, cast, CastExpression.EXPRESSION_PROPERTY)) {
ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
parenthesized.setExpression(copy);
copy = parenthesized;
}
cast.setExpression(copy);
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(reference, rewrite.getImportRewrite());
cast.setType(rewrite.getImportRewrite().addImport(explicitCast, ast, context));
copy = cast;
} else if (initializer instanceof ArrayInitializer && ASTNodes.getDimensions(varDecl) > 0) {
ArrayType newType = (ArrayType) ASTNodeFactory.newType(ast, varDecl);
ArrayCreation newArrayCreation = ast.newArrayCreation();
newArrayCreation.setType(newType);
newArrayCreation.setInitializer((ArrayInitializer) copy);
return newArrayCreation;
}
return copy;
}
use of org.eclipse.jdt.core.dom.ArrayCreation in project whole by wholeplatform.
the class CompilationUnitBuilder method newArrayCreation.
public ArrayCreation newArrayCreation(String type, int dimensions) {
ArrayCreation stm = ast.newArrayCreation();
stm.setType(newArrayType(type, dimensions));
return stm;
}
use of org.eclipse.jdt.core.dom.ArrayCreation in project evosuite by EvoSuite.
the class TestExtractingVisitor method retrieveVariableReference.
/**
* <p>
* retrieveVariableReference
* </p>
*
* @param argument
* a {@link java.lang.Object} object.
* @param varType
* a {@link java.lang.Class} object.
* @return a {@link org.evosuite.testcase.VariableReference} object.
*/
protected VariableReference retrieveVariableReference(Object argument, Class<?> varType) {
if (argument instanceof ClassInstanceCreation) {
return retrieveVariableReference((ClassInstanceCreation) argument, varType);
}
if (argument instanceof VariableDeclarationFragment) {
return retrieveVariableReference((VariableDeclarationFragment) argument);
}
if (argument instanceof SimpleName) {
SimpleName simpleName = (SimpleName) argument;
lineNumber = testReader.getLineNumber(simpleName.getStartPosition());
return retrieveVariableReference(simpleName.resolveBinding(), varType);
}
if (argument instanceof IVariableBinding) {
return retrieveVariableReference((IVariableBinding) argument, varType);
}
if (argument instanceof PrefixExpression) {
return retrieveVariableReference((PrefixExpression) argument);
}
if (argument instanceof InfixExpression) {
return retrieveVariableReference((InfixExpression) argument, varType);
}
if (argument instanceof ExpressionStatement) {
ExpressionStatement exprStmt = (ExpressionStatement) argument;
Expression expression = exprStmt.getExpression();
return retrieveVariableReference(expression, varType);
}
if (argument instanceof NullLiteral) {
return retrieveVariableReference((NullLiteral) argument, varType);
}
if (argument instanceof StringLiteral) {
return retrieveVariableReference((StringLiteral) argument);
}
if (argument instanceof NumberLiteral) {
return retrieveVariableReference((NumberLiteral) argument);
}
if (argument instanceof CharacterLiteral) {
return retrieveVariableReference((CharacterLiteral) argument);
}
if (argument instanceof BooleanLiteral) {
return retrieveVariableReference((BooleanLiteral) argument);
}
if (argument instanceof ITypeBinding) {
if (varType != null) {
return new ValidVariableReference(testCase.getReference(), varType);
}
return new ValidVariableReference(testCase.getReference(), retrieveTypeClass(argument));
}
if (argument instanceof QualifiedName) {
return retrieveVariableReference((QualifiedName) argument);
}
if (argument instanceof MethodInvocation) {
MethodInvocation methodInvocation = (MethodInvocation) argument;
VariableReference result = retrieveResultReference(methodInvocation);
nestedCallResults.push(result);
return result;
}
if (argument instanceof ArrayCreation) {
return retrieveVariableReference((ArrayCreation) argument);
}
if (argument instanceof VariableDeclaration) {
return retrieveVariableReference((VariableDeclaration) argument);
}
if (argument instanceof ArrayAccess) {
// argument).getArray(), null);
return retrieveVariableReference((ArrayAccess) argument);
}
if (argument instanceof Assignment) {
return retrieveVariableReference(((Assignment) argument).getLeftHandSide(), null);
}
if (argument instanceof CastExpression) {
CastExpression castExpression = (CastExpression) argument;
VariableReference result = retrieveVariableReference(castExpression.getExpression(), null);
Class<?> castClass = retrieveTypeClass(castExpression.resolveTypeBinding());
assert castClass.isAssignableFrom(toClass(result.getType()));
result.setType(castClass);
return result;
}
throw new UnsupportedOperationException("Argument type " + argument.getClass() + " not implemented!");
}
Aggregations