Search in sources :

Example 11 with StringLiteral

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));
    })));
}
Also used : StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) SingleMemberAnnotation(org.eclipse.jdt.core.dom.SingleMemberAnnotation) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 12 with StringLiteral

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!");
}
Also used : BooleanLiteral(org.eclipse.jdt.core.dom.BooleanLiteral) SimpleName(org.eclipse.jdt.core.dom.SimpleName) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) Assignment(org.eclipse.jdt.core.dom.Assignment) ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) VariableReference(org.evosuite.testcase.VariableReference) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) PrimitiveExpression(org.evosuite.testcase.PrimitiveExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ArrayCreation(org.eclipse.jdt.core.dom.ArrayCreation) CastExpression(org.eclipse.jdt.core.dom.CastExpression) NullLiteral(org.eclipse.jdt.core.dom.NullLiteral) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral) CharacterLiteral(org.eclipse.jdt.core.dom.CharacterLiteral)

Example 13 with StringLiteral

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));
    }
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) CastExpression(org.eclipse.jdt.core.dom.CastExpression) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess)

Example 14 with StringLiteral

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;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) LinkedCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)

Example 15 with StringLiteral

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;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) LinkedCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)

Aggregations

StringLiteral (org.eclipse.jdt.core.dom.StringLiteral)54 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)20 Expression (org.eclipse.jdt.core.dom.Expression)16 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)16 ASTNode (org.eclipse.jdt.core.dom.ASTNode)11 CastExpression (org.eclipse.jdt.core.dom.CastExpression)11 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)10 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)8 AST (org.eclipse.jdt.core.dom.AST)7 MemberValuePair (org.eclipse.jdt.core.dom.MemberValuePair)7 SimpleName (org.eclipse.jdt.core.dom.SimpleName)7 CharacterLiteral (org.eclipse.jdt.core.dom.CharacterLiteral)6 NumberLiteral (org.eclipse.jdt.core.dom.NumberLiteral)6 SuperMethodInvocation (org.eclipse.jdt.core.dom.SuperMethodInvocation)6 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)6 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)6 ArrayList (java.util.ArrayList)5 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)5 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)5 Name (org.eclipse.jdt.core.dom.Name)5