Search in sources :

Example 26 with Assignment

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

the class UnusedCodeFix method hasSideEffect.

private static boolean hasSideEffect(SimpleName reference) {
    ASTNode parent = reference.getParent();
    while (parent instanceof QualifiedName) {
        parent = parent.getParent();
    }
    if (parent instanceof FieldAccess) {
        parent = parent.getParent();
    }
    ASTNode node = null;
    int nameParentType = parent.getNodeType();
    if (nameParentType == ASTNode.ASSIGNMENT) {
        Assignment assignment = (Assignment) parent;
        node = assignment.getRightHandSide();
    } else if (nameParentType == ASTNode.SINGLE_VARIABLE_DECLARATION) {
        SingleVariableDeclaration decl = (SingleVariableDeclaration) parent;
        node = decl.getInitializer();
        if (node == null) {
            return false;
        }
    } else if (nameParentType == ASTNode.VARIABLE_DECLARATION_FRAGMENT) {
        node = parent;
    } else {
        return false;
    }
    ArrayList<Expression> sideEffects = new ArrayList<>();
    node.accept(new SideEffectFinder(sideEffects));
    return sideEffects.size() > 0;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ArrayList(java.util.ArrayList) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess)

Example 27 with Assignment

use of org.eclipse.jdt.core.dom.Assignment in project jbosstools-hibernate by jbosstools.

the class ProcessEntityInfo method visit.

@SuppressWarnings("unchecked")
public boolean visit(TypeDeclaration node) {
    ITypeBinding typeBinding = node.resolveBinding();
    String nodeName = typeBinding == null ? null : typeBinding.getBinaryName();
    if (nodeName == null) {
        return false;
    }
    entityInfo = entityInfos.getEntityInfo(nodeName);
    if (entityInfo == null) {
        return false;
    }
    if (entityInfo.isAddMappedSuperclassFlag()) {
        MarkerAnnotation matd = rewriter.getAST().newMarkerAnnotation();
        matd.setTypeName(rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_MAPPEDSUPERCLASS));
        ListRewrite lrw = rewriter.getListRewrite(node, TypeDeclaration.MODIFIERS2_PROPERTY);
        lrw.insertFirst(matd, null);
    }
    if (entityInfo.isAddEntityFlag()) {
        MarkerAnnotation matd = rewriter.getAST().newMarkerAnnotation();
        matd.setTypeName(rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_ENTITY));
        ListRewrite lrw = rewriter.getListRewrite(node, TypeDeclaration.MODIFIERS2_PROPERTY);
        lrw.insertFirst(matd, null);
    }
    /**
     * /
     *		if (!entityInfo.isImplicitConstructorFlag() && !entityInfo.isDefaultConstructorFlag() &&
     *				entityInfo.isAddSerializableInterfaceFlag()) {
     *			// add serializable interface
     *			SimpleName sn = null;
     *			//if (!entityInfo.isAddSerializableInterfaceImportFlag()) {
     *				sn = rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_SERIALIZABLE);
     *			//}
     *			//else {
     *			//	sn = rewriter.getAST().newSimpleName(JPAConst.IMPORT_SERIALIZABLE);
     *			//}
     *			SimpleType st = rewriter.getAST().newSimpleType(sn);
     *			ListRewrite lrw = rewriter.getListRewrite(node, TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY);
     *			lrw.insertFirst(st, null);
     *			// add "private static final long serialVersionUID = 1L;"
     *			// ...
     *		}
     *		/*
     */
    if (!entityInfo.isImplicitConstructorFlag() && !entityInfo.isDefaultConstructorFlag() && entityInfo.isAddSerializableInterfaceFlag()) {
        MethodDeclaration md = rewriter.getAST().newMethodDeclaration();
        md.setConstructor(true);
        Modifier modifier = rewriter.getAST().newModifier(Modifier.ModifierKeyword.PROTECTED_KEYWORD);
        md.modifiers().add(modifier);
        Block body = rewriter.getAST().newBlock();
        md.setBody(body);
        SimpleName sn = rewriter.getAST().newSimpleName(entityInfo.getName());
        md.setName(sn);
        ListRewrite lrw = rewriter.getListRewrite(node, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
        List<?> list = lrw.getOriginalList();
        MethodDeclaration insertBeforeNode = null;
        Iterator<?> it = list.iterator();
        while (it.hasNext()) {
            Object obj = it.next();
            if (obj instanceof MethodDeclaration) {
                insertBeforeNode = (MethodDeclaration) obj;
                break;
            }
        }
        if (insertBeforeNode == null) {
            lrw.insertLast(md, null);
        } else {
            lrw.insertBefore(md, insertBeforeNode, null);
        }
    }
    if (enableOptLock && entityInfo.isAddVersionFlag() && !entityInfo.hasVersionAnnotation()) {
        // add property "version", add getter/setter getVersion/setVersion,
        // add annotation for the property or for the getter
        // 
        // $NON-NLS-1$
        final String version = "version";
        // $NON-NLS-1$
        final String versionType = "Integer";
        // 
        VariableDeclarationFragment vdFragment = rewriter.getAST().newVariableDeclarationFragment();
        SimpleName variableName = rewriter.getAST().newSimpleName(version);
        vdFragment.setName(variableName);
        FieldDeclaration fieldVersion = rewriter.getAST().newFieldDeclaration(vdFragment);
        Modifier modifier = rewriter.getAST().newModifier(Modifier.ModifierKeyword.PROTECTED_KEYWORD);
        fieldVersion.modifiers().add(modifier);
        Name typeName = rewriter.getAST().newName(versionType);
        SimpleType type = rewriter.getAST().newSimpleType(typeName);
        fieldVersion.setType(type);
        // 
        MethodDeclaration mdGetter = rewriter.getAST().newMethodDeclaration();
        modifier = rewriter.getAST().newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
        mdGetter.modifiers().add(modifier);
        Block body = rewriter.getAST().newBlock();
        ReturnStatement returnVersion = rewriter.getAST().newReturnStatement();
        variableName = rewriter.getAST().newSimpleName(version);
        returnVersion.setExpression(variableName);
        body.statements().add(returnVersion);
        mdGetter.setBody(body);
        // $NON-NLS-1$
        SimpleName sn = rewriter.getAST().newSimpleName("getVersion");
        mdGetter.setName(sn);
        typeName = rewriter.getAST().newName(versionType);
        type = rewriter.getAST().newSimpleType(typeName);
        mdGetter.setReturnType2(type);
        // 
        MethodDeclaration mdSetter = rewriter.getAST().newMethodDeclaration();
        modifier = rewriter.getAST().newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
        mdSetter.modifiers().add(modifier);
        body = rewriter.getAST().newBlock();
        Assignment assignment = rewriter.getAST().newAssignment();
        FieldAccess fieldAccess = rewriter.getAST().newFieldAccess();
        ThisExpression thisExpression = rewriter.getAST().newThisExpression();
        fieldAccess.setExpression(thisExpression);
        variableName = rewriter.getAST().newSimpleName(version);
        fieldAccess.setName(variableName);
        assignment.setLeftHandSide(fieldAccess);
        variableName = rewriter.getAST().newSimpleName(version);
        assignment.setRightHandSide(variableName);
        ExpressionStatement expressionStatement = rewriter.getAST().newExpressionStatement(assignment);
        body.statements().add(expressionStatement);
        mdSetter.setBody(body);
        // $NON-NLS-1$
        sn = rewriter.getAST().newSimpleName("setVersion");
        mdSetter.setName(sn);
        SingleVariableDeclaration svd = rewriter.getAST().newSingleVariableDeclaration();
        variableName = rewriter.getAST().newSimpleName(version);
        svd.setName(variableName);
        typeName = rewriter.getAST().newName(versionType);
        type = rewriter.getAST().newSimpleType(typeName);
        svd.setType(type);
        mdSetter.parameters().add(svd);
        // 
        ListRewrite lrw = rewriter.getListRewrite(node, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
        if (entityInfo.getVersionFieldGetter() != FieldGetterType.FIELD && entityInfo.getVersionFieldGetter() != FieldGetterType.FIELD_GETTER) {
            lrw.insertLast(fieldVersion, null);
        }
        if (entityInfo.getVersionFieldGetter() != FieldGetterType.GETTER && entityInfo.getVersionFieldGetter() != FieldGetterType.FIELD_GETTER) {
            lrw.insertLast(mdGetter, null);
            lrw.insertLast(mdSetter, null);
        }
        if (annotationStyle == AnnotStyle.FIELDS) {
            MarkerAnnotation matd = rewriter.getAST().newMarkerAnnotation();
            matd.setTypeName(rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_VERSION));
            lrw = rewriter.getListRewrite(fieldVersion, FieldDeclaration.MODIFIERS2_PROPERTY);
            lrw.insertFirst(matd, null);
        } else if (annotationStyle == AnnotStyle.GETTERS) {
            MarkerAnnotation matd = rewriter.getAST().newMarkerAnnotation();
            matd.setTypeName(rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_VERSION));
            lrw = rewriter.getListRewrite(mdGetter, MethodDeclaration.MODIFIERS2_PROPERTY);
            lrw.insertFirst(matd, null);
        }
    }
    return true;
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Assignment(org.eclipse.jdt.core.dom.Assignment) SimpleType(org.eclipse.jdt.core.dom.SimpleType) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) MarkerAnnotation(org.eclipse.jdt.core.dom.MarkerAnnotation) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) Modifier(org.eclipse.jdt.core.dom.Modifier)

Example 28 with Assignment

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

the class CompilationUnitBuilder method newSetterMethodWithNotification.

public MethodDeclaration newSetterMethodWithNotification(String featuresEnum, String fType, String fName, String name, boolean isReference, boolean useQualifiedType) {
    MethodDeclaration method = ast.newMethodDeclaration();
    method.setConstructor(false);
    method.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
    method.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID));
    method.setName(ast.newSimpleName(StringUtils.setterName(StringUtils.isJavaKeyword(name) ? name : fName)));
    method.parameters().add(newSingleVariableDeclaration(useQualifiedType ? newQualifiedType(fType) : newType(fType), fName));
    if (!isInterface) {
        Block body = newBlock();
        MethodInvocation callExp = ast.newMethodInvocation();
        callExp.setName(ast.newSimpleName("notifyChanged"));
        if (featuresEnum != null)
            callExp.arguments().add(newFieldAccess(featuresEnum, fName));
        FieldAccess fieldAcc = ast.newFieldAccess();
        fieldAcc.setExpression(ast.newThisExpression());
        fieldAcc.setName(ast.newSimpleName(fName));
        callExp.arguments().add(fieldAcc);
        fieldAcc = ast.newFieldAccess();
        fieldAcc.setExpression(ast.newThisExpression());
        fieldAcc.setName(ast.newSimpleName(fName));
        Assignment eq = ast.newAssignment();
        eq.setLeftHandSide(fieldAcc);
        eq.setRightHandSide(ast.newSimpleName(fName));
        callExp.arguments().add(eq);
        if (isReference)
            callExp.arguments().add(newLiteral(false));
        body.statements().add(ast.newExpressionStatement(callExp));
        method.setBody(body);
    }
    return method;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) Block(org.eclipse.jdt.core.dom.Block) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess)

Example 29 with Assignment

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

the class PartFactoryVisitorBuilder method addPartFactoryVisitMethod.

public MethodDeclaration addPartFactoryVisitMethod(String typeName, int size) {
    MethodDeclaration method = addVisitMethod(typeName);
    if (!isInterface) {
        ClassInstanceCreation partInstance;
        if (typeName.equals(((LanguageGenerator) generator).entityVariableName()))
            partInstance = newClassInstanceCreation(VariablePartName);
        else
            partInstance = newClassInstanceCreation(size < 5 ? SimplePartName : SimpleFoldablePartName, newMethodInvocation("entity", "wGetEntityDescriptor"));
        Assignment eq = newAssignment(ast.newSimpleName("part"), partInstance);
        method.getBody().statements().add(newExpressionStatement(eq));
    }
    return method;
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) Assignment(org.eclipse.jdt.core.dom.Assignment) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration)

Example 30 with Assignment

use of org.eclipse.jdt.core.dom.Assignment in project evosuite by EvoSuite.

the class TestExtractingVisitor method retrieveResultReference.

private VariableReference retrieveResultReference(SuperMethodInvocation superMethodInvocation) {
    // TODO Duplicate code from retrieveResultReference(MethodInvocation)
    // too bad they don't have a common matching interface
    VariableReference result = calleeResultMap.get(superMethodInvocation.toString());
    if (result != null) {
        return result;
    }
    ASTNode parent = superMethodInvocation.getParent();
    if (parent instanceof VariableDeclarationFragment) {
        return retrieveVariableReference(parent, null);
    }
    if (parent instanceof Assignment) {
        Assignment assignment = (Assignment) parent;
        return retrieveVariableReference(assignment.getLeftHandSide(), null);
    }
    IMethodBinding methodBinding = superMethodInvocation.resolveMethodBinding();
    result = retrieveVariableReference(methodBinding.getReturnType(), null);
    calleeResultMap.put(superMethodInvocation.toString(), result);
    return result;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) VariableReference(org.evosuite.testcase.VariableReference) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Aggregations

Assignment (org.eclipse.jdt.core.dom.Assignment)96 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)42 Expression (org.eclipse.jdt.core.dom.Expression)41 SimpleName (org.eclipse.jdt.core.dom.SimpleName)37 ASTNode (org.eclipse.jdt.core.dom.ASTNode)33 CastExpression (org.eclipse.jdt.core.dom.CastExpression)29 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)29 AST (org.eclipse.jdt.core.dom.AST)28 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)26 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)25 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)24 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)24 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)23 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)21 Statement (org.eclipse.jdt.core.dom.Statement)21 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)19 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)19 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)18 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)18 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)18