Search in sources :

Example 61 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral in project eclipse.jdt.ls by eclipse.

the class LocalCorrectionsSubProcessor method getThrowForUnexpectedDefault.

private static ThrowStatement getThrowForUnexpectedDefault(Expression switchExpression, AST ast, ASTRewrite astRewrite) {
    ThrowStatement newThrowStatement = ast.newThrowStatement();
    ClassInstanceCreation newCic = ast.newClassInstanceCreation();
    // $NON-NLS-1$
    newCic.setType(ast.newSimpleType(ast.newSimpleName("IllegalArgumentException")));
    InfixExpression newInfixExpr = ast.newInfixExpression();
    StringLiteral newStringLiteral = ast.newStringLiteral();
    // $NON-NLS-1$
    newStringLiteral.setLiteralValue("Unexpected value: ");
    newInfixExpr.setLeftOperand(newStringLiteral);
    newInfixExpr.setOperator(InfixExpression.Operator.PLUS);
    newInfixExpr.setRightOperand((Expression) astRewrite.createCopyTarget(switchExpression));
    newCic.arguments().add(newInfixExpr);
    newThrowStatement.setExpression(newCic);
    return newThrowStatement;
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ThrowStatement(org.eclipse.jdt.core.dom.ThrowStatement)

Example 62 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral in project eclipse.jdt.ls by eclipse.

the class LocalCorrectionsSubProcessor method getThrowForUnsupportedCase.

private static ThrowStatement getThrowForUnsupportedCase(Expression switchExpr, AST ast, ASTRewrite astRewrite) {
    ThrowStatement newThrowStatement = ast.newThrowStatement();
    ClassInstanceCreation newCic = ast.newClassInstanceCreation();
    // $NON-NLS-1$
    newCic.setType(ast.newSimpleType(ast.newSimpleName("UnsupportedOperationException")));
    InfixExpression newInfixExpr = ast.newInfixExpression();
    StringLiteral newStringLiteral = ast.newStringLiteral();
    // $NON-NLS-1$
    newStringLiteral.setLiteralValue("Unimplemented case: ");
    newInfixExpr.setLeftOperand(newStringLiteral);
    newInfixExpr.setOperator(InfixExpression.Operator.PLUS);
    newInfixExpr.setRightOperand((Expression) astRewrite.createCopyTarget(switchExpr));
    newCic.arguments().add(newInfixExpr);
    newThrowStatement.setExpression(newCic);
    return newThrowStatement;
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ThrowStatement(org.eclipse.jdt.core.dom.ThrowStatement)

Example 63 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral in project whole by wholeplatform.

the class CompilationUnitBuilder method newLiteral.

public StringLiteral newLiteral(String text) {
    StringLiteral str = ast.newStringLiteral();
    str.setLiteralValue(text);
    return str;
}
Also used : StringLiteral(org.eclipse.jdt.core.dom.StringLiteral)

Example 64 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral in project lsp4jakarta by eclipse.

the class ModifyAnnotationProposal method getRewrite.

@SuppressWarnings("unchecked")
@Override
protected ASTRewrite getRewrite() throws CoreException {
    CompilationUnit fInvocationNode = getInvocationNode();
    IBinding fBinding = getBinding();
    String[] annotations = getAnnotations();
    // get short name of annotations
    String[] annotationShortNames = new String[annotations.length];
    for (int i = 0; i < annotations.length; i++) {
        String shortName = annotations[i].substring(annotations[i].lastIndexOf(".") + 1, annotations[i].length());
        annotationShortNames[i] = shortName;
    }
    ASTNode declNode = null;
    ASTNode boundNode = fInvocationNode.findDeclaringNode(fBinding);
    CompilationUnit newRoot = fInvocationNode;
    if (boundNode != null) {
        // is same CU
        declNode = boundNode;
    } else {
        newRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
        declNode = newRoot.findDeclaringNode(fBinding.getKey());
    }
    ImportRewrite imports = createImportRewrite(newRoot);
    boolean isField = declNode instanceof VariableDeclarationFragment;
    if (isField) {
        declNode = declNode.getParent();
    }
    if (declNode.getNodeType() == ASTNode.FIELD_DECLARATION) {
        AST ast = declNode.getAST();
        ASTRewrite rewrite = ASTRewrite.create(ast);
        ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(declNode, imports);
        List<Annotation> existingAnnotations = new ArrayList<Annotation>();
        List<? extends ASTNode> children = (List<? extends ASTNode>) declNode.getStructuralProperty(FieldDeclaration.MODIFIERS2_PROPERTY);
        // for all existing annotations (that are the annotation we want)
        for (ASTNode child : children) {
            if (child instanceof Annotation) {
                Annotation annotation = (Annotation) child;
                boolean containsAnnotation = Arrays.stream(annotationShortNames).anyMatch(annotation.getTypeName().toString()::contains);
                // check if current child annotation has all attributes to add already or any to remove
                if (containsAnnotation && child instanceof NormalAnnotation) {
                    List<String> existingValues = (List<String>) ((NormalAnnotation) child).values().stream().map(mvp -> ((MemberValuePair) mvp).getName().toString()).collect(toList());
                    boolean containsAllToAdd = this.attributesToAdd.stream().allMatch(attr -> existingValues.stream().anyMatch(v -> v.equals(attr)));
                    boolean containsAnyToRemove = this.attributesToRemove.stream().anyMatch(attr -> existingValues.stream().anyMatch(v -> v.equals(attr)));
                    if (!containsAllToAdd || containsAnyToRemove) {
                        existingAnnotations.add(annotation);
                        rewrite.remove(child, null);
                    }
                }
            }
        }
        // add new annotations to proposal (restoring those that were removed)
        for (Annotation a : existingAnnotations) {
            if (a instanceof NormalAnnotation) {
                NormalAnnotation marker = ast.newNormalAnnotation();
                marker.setTypeName(ast.newName(imports.addImport(a.getTypeName().toString(), importRewriteContext)));
                List<MemberValuePair> values = marker.values();
                // add existing attributes to annotation
                List<MemberValuePair> existingValues = ((NormalAnnotation) a).values();
                for (MemberValuePair mvp : existingValues) {
                    boolean removeAttribute = this.attributesToRemove.contains(mvp.getName().getFullyQualifiedName());
                    // do not add attributes to be removed
                    if (!removeAttribute) {
                        MemberValuePair memberValuePair = ast.newMemberValuePair();
                        memberValuePair.setName(ast.newSimpleName(mvp.getName().getFullyQualifiedName()));
                        StringLiteral stringValue = ast.newStringLiteral();
                        if (mvp.getValue() instanceof StringLiteral) {
                            StringLiteral stringLiteral = (StringLiteral) mvp.getValue();
                            stringValue.setLiteralValue(stringLiteral.getLiteralValue());
                        } else {
                            stringValue.setLiteralValue("");
                        }
                        memberValuePair.setValue(stringValue);
                        values.add(memberValuePair);
                    }
                }
                // add new attributes
                for (String newAttr : this.attributesToAdd) {
                    // dont add duplicate attributes to an annotation
                    if (values.stream().noneMatch(v -> v.getName().toString().equals(newAttr))) {
                        MemberValuePair memberValuePair = ast.newMemberValuePair();
                        memberValuePair.setName(ast.newSimpleName(newAttr));
                        StringLiteral stringValue = ast.newStringLiteral();
                        stringValue.setLiteralValue("");
                        memberValuePair.setValue(stringValue);
                        values.add(memberValuePair);
                    }
                }
                rewrite.getListRewrite(declNode, isField ? FieldDeclaration.MODIFIERS2_PROPERTY : TypeDeclaration.MODIFIERS2_PROPERTY).insertFirst(marker, null);
            }
        }
        return rewrite;
    } else if (declNode instanceof TypeDeclaration || isField) {
        AST ast = declNode.getAST();
        ASTRewrite rewrite = ASTRewrite.create(ast);
        ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(declNode, imports);
        List<Annotation> existingAnnotations = new ArrayList<Annotation>();
        List<? extends ASTNode> children = (List<? extends ASTNode>) declNode.getStructuralProperty(TypeDeclaration.MODIFIERS2_PROPERTY);
        // find and save existing annotation, then remove it from ast
        for (ASTNode child : children) {
            if (child instanceof Annotation) {
                Annotation annotation = (Annotation) child;
                boolean containsAnnotation = Arrays.stream(annotationShortNames).anyMatch(annotation.getTypeName().toString()::contains);
                if (containsAnnotation) {
                    existingAnnotations.add(annotation);
                    rewrite.remove(child, null);
                }
            }
        }
        // add new annotation with fields from existing annotation
        for (String annotation : annotations) {
            NormalAnnotation marker = ast.newNormalAnnotation();
            marker.setTypeName(ast.newName(imports.addImport(annotation, importRewriteContext)));
            List<MemberValuePair> values = marker.values();
            if (!existingAnnotations.isEmpty()) {
                for (Annotation a : existingAnnotations) {
                    if (a instanceof NormalAnnotation) {
                        List<MemberValuePair> existingValues = ((NormalAnnotation) a).values();
                        for (MemberValuePair mvp : existingValues) {
                            boolean removeAttribute = this.attributesToRemove.contains(mvp.getName().getFullyQualifiedName());
                            // do not add attribute to be removed
                            if (!removeAttribute) {
                                MemberValuePair memberValuePair = ast.newMemberValuePair();
                                memberValuePair.setName(ast.newSimpleName(mvp.getName().getFullyQualifiedName()));
                                StringLiteral stringValue = ast.newStringLiteral();
                                if (mvp.getValue() instanceof StringLiteral) {
                                    StringLiteral stringLiteral = (StringLiteral) mvp.getValue();
                                    stringValue.setLiteralValue(stringLiteral.getLiteralValue());
                                } else {
                                    stringValue.setLiteralValue("");
                                }
                                memberValuePair.setValue(stringValue);
                                values.add(memberValuePair);
                            }
                        }
                    }
                }
            }
            // add new String attributes
            for (String newAttr : this.attributesToAdd) {
                MemberValuePair memberValuePair = ast.newMemberValuePair();
                memberValuePair.setName(ast.newSimpleName(newAttr));
                StringLiteral stringValue = ast.newStringLiteral();
                stringValue.setLiteralValue("");
                memberValuePair.setValue(stringValue);
                values.add(memberValuePair);
            }
            rewrite.getListRewrite(declNode, isField ? FieldDeclaration.MODIFIERS2_PROPERTY : TypeDeclaration.MODIFIERS2_PROPERTY).insertFirst(marker, null);
        }
        return rewrite;
    }
    return null;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Arrays(java.util.Arrays) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) CoreException(org.eclipse.core.runtime.CoreException) MemberValuePair(org.eclipse.jdt.core.dom.MemberValuePair) IBinding(org.eclipse.jdt.core.dom.IBinding) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Annotation(org.eclipse.jdt.core.dom.Annotation) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) AST(org.eclipse.jdt.core.dom.AST) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) NormalAnnotation(org.eclipse.jdt.core.dom.NormalAnnotation) ASTResolving(org.eclipse.jdt.internal.core.manipulation.dom.ASTResolving) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) IBinding(org.eclipse.jdt.core.dom.IBinding) ArrayList(java.util.ArrayList) MemberValuePair(org.eclipse.jdt.core.dom.MemberValuePair) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) NormalAnnotation(org.eclipse.jdt.core.dom.NormalAnnotation) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) AST(org.eclipse.jdt.core.dom.AST) Annotation(org.eclipse.jdt.core.dom.Annotation) NormalAnnotation(org.eclipse.jdt.core.dom.NormalAnnotation) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 65 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral in project evosuite-plus-plus by llmhyy.

the class CodeGenerator method createUnobservedInitStmt.

@SuppressWarnings("unchecked")
private void createUnobservedInitStmt(final int logRecNo, final Block methodBlock, final AST ast) {
    // NOTE: PLAIN INIT: has always one non-null param
    // TODO: use primitives
    final int oid = this.log.objectIds.get(logRecNo);
    final String type = this.log.oidClassNames.get(this.log.oidRecMapping.get(oid));
    final Object value = this.log.params.get(logRecNo)[0];
    this.isXStreamNeeded = true;
    final VariableDeclarationFragment vd = ast.newVariableDeclarationFragment();
    // handling because there must always be a new instantiation statement for pseudo inits
    this.oidToVarMapping.remove(oid);
    vd.setName(ast.newSimpleName(this.createNewVarName(oid, type)));
    final MethodInvocation methodInvocation = ast.newMethodInvocation();
    final Name name = ast.newSimpleName("XSTREAM");
    methodInvocation.setExpression(name);
    methodInvocation.setName(ast.newSimpleName("fromXML"));
    final StringLiteral xmlParam = ast.newStringLiteral();
    xmlParam.setLiteralValue((String) value);
    methodInvocation.arguments().add(xmlParam);
    final CastExpression castExpr = ast.newCastExpression();
    castExpr.setType(this.createAstType(type, ast));
    castExpr.setExpression(methodInvocation);
    vd.setInitializer(castExpr);
    final VariableDeclarationStatement vs = ast.newVariableDeclarationStatement(vd);
    vs.setType(this.createAstType(type, ast));
    methodBlock.statements().add(vs);
}
Also used : StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) CastExpression(org.eclipse.jdt.core.dom.CastExpression) Name(org.eclipse.jdt.core.dom.Name)

Aggregations

StringLiteral (org.eclipse.jdt.core.dom.StringLiteral)74 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)31 Expression (org.eclipse.jdt.core.dom.Expression)22 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)20 CastExpression (org.eclipse.jdt.core.dom.CastExpression)19 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)18 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)16 ASTNode (org.eclipse.jdt.core.dom.ASTNode)12 SimpleName (org.eclipse.jdt.core.dom.SimpleName)11 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)11 CharacterLiteral (org.eclipse.jdt.core.dom.CharacterLiteral)10 ArrayList (java.util.ArrayList)9 AST (org.eclipse.jdt.core.dom.AST)9 BooleanLiteral (org.eclipse.jdt.core.dom.BooleanLiteral)9 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)9 MemberValuePair (org.eclipse.jdt.core.dom.MemberValuePair)9 NumberLiteral (org.eclipse.jdt.core.dom.NumberLiteral)9 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)9 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)8 NormalAnnotation (org.eclipse.jdt.core.dom.NormalAnnotation)8