use of org.eclipse.jdt.core.dom.StringLiteral in project xtext-xtend by eclipse.
the class ASTFlattenerUtils method canConvertToRichText.
public boolean canConvertToRichText(final InfixExpression node) {
final FieldDeclaration parentFieldDecl = this.<FieldDeclaration>findParentOfType(node, FieldDeclaration.class);
if ((parentFieldDecl != null)) {
final TypeDeclaration typeDeclr = this.<TypeDeclaration>findParentOfType(parentFieldDecl, TypeDeclaration.class);
if ((typeDeclr.isInterface() || (this.isFinal(parentFieldDecl.modifiers()) && this.isStatic(parentFieldDecl.modifiers())))) {
return false;
}
}
final SingleMemberAnnotation parentSingleMemberAnnotation = this.<SingleMemberAnnotation>findParentOfType(node, SingleMemberAnnotation.class);
if ((parentSingleMemberAnnotation != null)) {
return false;
}
final Iterable<StringLiteral> nodes = this.collectCompatibleNodes(node);
return ((!IterableExtensions.isEmpty(nodes)) && IterableExtensions.<StringLiteral>forall(nodes, ((Function1<StringLiteral, Boolean>) (StringLiteral it) -> {
return Boolean.valueOf(this.canTranslate(it));
})));
}
use of org.eclipse.jdt.core.dom.StringLiteral 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!");
}
use of org.eclipse.jdt.core.dom.StringLiteral in project evosuite by EvoSuite.
the class CodeGenerator method createFieldWriteAccessStmt.
@SuppressWarnings("unchecked")
private void createFieldWriteAccessStmt(final String packageName, final int logRecNo, final Block methodBlock, final AST ast) {
// assumption: all necessary statements are created and there is one variable for reach referenced object
final Object[] methodArgs = this.log.params.get(logRecNo);
final String methodName = this.log.methodNames.get(logRecNo);
final int oid = this.log.objectIds.get(logRecNo);
final int captureId = this.log.captureIds.get(logRecNo);
final String fieldName = this.log.namesOfAccessedFields.get(captureId);
final String typeName = this.log.oidClassNames.get(this.log.oidRecMapping.get(oid));
Class<?> type = getClassForName(typeName);
// try {
// type = Class.forName(typeName);
// } catch (ClassNotFoundException e) {
// throw new RuntimeException(e);
// }
final int fieldTypeModifiers = this.getFieldModifiers(type, fieldName);
final boolean isPublic = java.lang.reflect.Modifier.isPublic(fieldTypeModifiers);
// TODO might be nicer...
final boolean haveSamePackage = type.getPackage().getName().equals(packageName);
final boolean isReflectionAccessNeeded = !isPublic && !haveSamePackage;
if (isReflectionAccessNeeded) {
this.isSetFieldMethodNeeded = true;
final String varName = this.oidToVarMapping.get(oid);
final MethodInvocation setFieldCall = ast.newMethodInvocation();
setFieldCall.setName(ast.newSimpleName("setField"));
StringLiteral stringLiteral = ast.newStringLiteral();
stringLiteral.setLiteralValue(typeName);
// class name
setFieldCall.arguments().add(stringLiteral);
stringLiteral = ast.newStringLiteral();
stringLiteral.setLiteralValue(fieldName);
// field name
setFieldCall.arguments().add(stringLiteral);
// receiver
setFieldCall.arguments().add(ast.newSimpleName(varName));
final Integer arg = (Integer) methodArgs[0];
if (arg == null) {
// value
setFieldCall.arguments().add(ast.newNullLiteral());
} else {
// value
setFieldCall.arguments().add(ast.newSimpleName(this.oidToVarMapping.get(arg)));
}
methodBlock.statements().add(ast.newExpressionStatement(setFieldCall));
} else {
FieldAccess fa = ast.newFieldAccess();
if (CaptureLog.PUTSTATIC.equals(methodName)) {
// final String type = this.log.oidClassNames.get(this.log.oidRecMapping.get(oid));
// .split("\\.")));
fa.setExpression(ast.newName(typeName));
} else {
final String varName = this.oidToVarMapping.get(oid);
fa.setExpression(ast.newSimpleName(varName));
}
fa.setName(ast.newSimpleName(fieldName));
final Assignment assignment = ast.newAssignment();
assignment.setLeftHandSide(fa);
final Integer arg = (Integer) methodArgs[0];
if (arg == null) {
assignment.setRightHandSide(ast.newNullLiteral());
} else {
final Class<?> argType = this.oidToTypeMapping.get(arg);
final String fieldDesc = this.log.descList.get(logRecNo);
final Class<?> fieldType = CaptureUtil.getClassFromDesc(fieldDesc);
if (fieldType.isAssignableFrom(argType)) {
assignment.setRightHandSide(ast.newSimpleName(this.oidToVarMapping.get(arg)));
} else {
// we need an up-cast
final CastExpression cast = ast.newCastExpression();
cast.setType(ast.newSimpleType(ast.newName(fieldType.getName())));
cast.setExpression(ast.newSimpleName(this.oidToVarMapping.get(arg)));
assignment.setRightHandSide(cast);
}
}
methodBlock.statements().add(ast.newExpressionStatement(assignment));
}
}
use of org.eclipse.jdt.core.dom.StringLiteral in project flux by eclipse.
the class AdvancedQuickAssistProcessor method getPickOutStringProposals.
private static boolean getPickOutStringProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
// we work with String's
if (!(node instanceof StringLiteral)) {
return false;
}
// user should select part of String
int selectionPos = context.getSelectionOffset();
int selectionLen = context.getSelectionLength();
if (selectionLen == 0) {
return false;
}
int valueStart = node.getStartPosition() + 1;
int valueEnd = node.getStartPosition() + node.getLength() - 1;
// selection must be inside node and the quotes and not contain the full value
if (selectionPos < valueStart || selectionPos + selectionLen > valueEnd || valueEnd - valueStart == selectionLen) {
return false;
}
// prepare string parts positions
StringLiteral stringLiteral = (StringLiteral) node;
String stringValue = stringLiteral.getEscapedValue();
int firstPos = selectionPos - node.getStartPosition();
int secondPos = firstPos + selectionLen;
// prepare new string literals
AST ast = node.getAST();
StringLiteral leftLiteral = ast.newStringLiteral();
StringLiteral centerLiteral = ast.newStringLiteral();
StringLiteral rightLiteral = ast.newStringLiteral();
try {
leftLiteral.setEscapedValue('"' + stringValue.substring(1, firstPos) + '"');
centerLiteral.setEscapedValue('"' + stringValue.substring(firstPos, secondPos) + '"');
rightLiteral.setEscapedValue('"' + stringValue.substring(secondPos, stringValue.length() - 1) + '"');
} catch (IllegalArgumentException e) {
return false;
}
if (resultingCollections == null) {
return true;
}
ASTRewrite rewrite = ASTRewrite.create(ast);
// prepare new expression instead of StringLiteral
InfixExpression expression = ast.newInfixExpression();
expression.setOperator(InfixExpression.Operator.PLUS);
if (firstPos != 1) {
expression.setLeftOperand(leftLiteral);
}
if (firstPos == 1) {
expression.setLeftOperand(centerLiteral);
} else {
expression.setRightOperand(centerLiteral);
}
if (secondPos < stringValue.length() - 1) {
if (firstPos == 1) {
expression.setRightOperand(rightLiteral);
} else {
expression.extendedOperands().add(rightLiteral);
}
}
// use new expression instead of old StirngLiteral
rewrite.replace(stringLiteral, expression, null);
// add correction proposal
String label = CorrectionMessages.AdvancedQuickAssistProcessor_pickSelectedString;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.PICK_SELECTED_STRING);
// $NON-NLS-1$
proposal.addLinkedPosition(rewrite.track(centerLiteral), true, "CENTER_STRING");
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.core.dom.StringLiteral in project flux by eclipse.
the class AdvancedQuickAssistProcessor method getCombineStringProposals.
private static boolean getCombineStringProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
// we work with InfixExpressions
InfixExpression infixExpression;
if (node instanceof InfixExpression) {
infixExpression = (InfixExpression) node;
} else if (node.getParent() instanceof InfixExpression) {
infixExpression = (InfixExpression) node.getParent();
} else {
return false;
}
// only + is valid for combining strings
if (!(infixExpression.getOperator().equals(InfixExpression.Operator.PLUS))) {
return false;
}
// all expressions must be strings
Expression leftOperand = infixExpression.getLeftOperand();
Expression rightOperand = infixExpression.getRightOperand();
if (!(leftOperand instanceof StringLiteral && rightOperand instanceof StringLiteral)) {
return false;
}
StringLiteral leftString = (StringLiteral) leftOperand;
StringLiteral rightString = (StringLiteral) rightOperand;
if (resultingCollections == null) {
return true;
}
// begin building combined string
StringBuilder stringBuilder = new StringBuilder(leftString.getLiteralValue());
stringBuilder.append(rightString.getLiteralValue());
// append extended string literals
for (Object operand : infixExpression.extendedOperands()) {
if (!(operand instanceof StringLiteral))
return false;
StringLiteral stringLiteral = (StringLiteral) operand;
stringBuilder.append(stringLiteral.getLiteralValue());
}
// prepare new string literal
AST ast = node.getAST();
StringLiteral combinedStringLiteral = ast.newStringLiteral();
combinedStringLiteral.setLiteralValue(stringBuilder.toString());
ASTRewrite rewrite = ASTRewrite.create(ast);
rewrite.replace(infixExpression, combinedStringLiteral, null);
// add correction proposal
String label = CorrectionMessages.AdvancedQuickAssistProcessor_combineSelectedStrings;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.COMBINE_STRINGS);
resultingCollections.add(proposal);
return true;
}
Aggregations