Search in sources :

Example 26 with NumberLiteral

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

the class TestExtractingVisitor method visit.

/**
 * {@inheritDoc}
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public boolean visit(VariableDeclarationStatement variableDeclStmt) {
    Class<?> varType = retrieveTypeClass(variableDeclStmt.getType());
    if (varType.isPrimitive() || PRIMITIVE_CLASSES.contains(varType)) {
        // Can only happen to primitive types and String,
        // otherwise it is a constructor call which is handled elsewhere
        logger.debug("Variable has not been treated elsewhere...");
        VariableDeclarationFragment varDeclFrgmnt = (VariableDeclarationFragment) variableDeclStmt.fragments().get(0);
        Expression expression = varDeclFrgmnt.getInitializer();
        VariableReference varRef = retrieveVariableReference(expression, null);
        varRef.setOriginalCode(variableDeclStmt.toString());
        // TODO Use the name here as well?
        // String name = varDeclFrgmt.getName().getIdentifier();
        // new BoundVariableReferenceImpl(testCase, varType, name);
        testCase.addVariable(varDeclFrgmnt.resolveBinding(), varRef);
        return true;
    }
    if (varType.isArray()) {
        // if (varType.getComponentType().isPrimitive() ||
        // varType.getComponentType().equals(String.class)) {
        // ... or to primitive and string arrays
        VariableDeclarationFragment varDeclFrgmnt = (VariableDeclarationFragment) variableDeclStmt.fragments().get(0);
        Expression expression = varDeclFrgmnt.getInitializer();
        if (expression instanceof ArrayInitializer) {
            ArrayReference arrayReference = new ValidArrayReference(testCase.getReference(), varType);
            ArrayStatement arrayStatement = new ArrayStatement(testCase.getReference(), arrayReference);
            arrayReference.setOriginalCode(variableDeclStmt.toString());
            testCase.addStatement(arrayStatement);
            testCase.addVariable(varDeclFrgmnt.resolveBinding(), arrayReference);
            ArrayInitializer arrayInitializer = (ArrayInitializer) expression;
            for (int idx = 0; idx < arrayInitializer.expressions().size(); idx++) {
                Expression expr = (Expression) arrayInitializer.expressions().get(idx);
                VariableReference valueRef;
                if (expr instanceof NumberLiteral) {
                    valueRef = retrieveVariableReference((NumberLiteral) expr, varType.getComponentType());
                } else if (expr instanceof PrefixExpression) {
                    valueRef = retrieveVariableReference((PrefixExpression) expr, varType.getComponentType());
                } else {
                    valueRef = retrieveVariableReference(expr, null);
                }
                valueRef.setOriginalCode(expr.toString());
                VariableReference arrayElementRef = new ArrayIndex(testCase.getReference(), arrayReference, idx);
                arrayElementRef.setOriginalCode(expr.toString());
                arrayStatement.getVariableReferences().add(arrayElementRef);
                AssignmentStatement arrayAssignment = new AssignmentStatement(testCase.getReference(), arrayElementRef, valueRef);
                testCase.addStatement(arrayAssignment);
            }
            // }
            return true;
        }
        if (expression instanceof ArrayCreation) {
            ArrayCreation arrayCreation = ((ArrayCreation) expression);
            List paramTypes = new ArrayList();
            for (int idx = 0; idx < arrayCreation.dimensions().size(); idx++) {
                paramTypes.add(int.class);
            }
            List<VariableReference> lengthsVarRefs = convertParams(arrayCreation.dimensions(), paramTypes);
            ArrayReference arrayReference = new ValidArrayReference(testCase.getReference(), varType);
            arrayReference.setOriginalCode(variableDeclStmt.toString());
            ArrayStatement arrayStatement = new ArrayStatement(testCase.getReference(), arrayReference);
            arrayStatement.setLengths(getLengths(variableDeclStmt, lengthsVarRefs));
            testCase.addVariable(varDeclFrgmnt.resolveBinding(), arrayStatement.getReturnValue());
            testCase.addStatement(arrayStatement);
        }
    }
    return true;
}
Also used : ArrayReference(org.evosuite.testcase.ArrayReference) VariableReference(org.evosuite.testcase.VariableReference) ArrayList(java.util.ArrayList) ArrayIndex(org.evosuite.testcase.ArrayIndex) 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) AssignmentStatement(org.evosuite.testcase.AssignmentStatement) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ArrayCreation(org.eclipse.jdt.core.dom.ArrayCreation) ArrayStatement(org.evosuite.testcase.ArrayStatement) ArrayList(java.util.ArrayList) AbstractList(java.util.AbstractList) List(java.util.List) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Example 27 with NumberLiteral

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

the class ProcessEntityInfo method visit.

@SuppressWarnings("unchecked")
public boolean visit(MethodDeclaration node) {
    if (entityInfo == null) {
        return false;
    }
    if (annotationStyle != AnnotStyle.GETTERS) {
        return true;
    }
    if (node.getName().getFullyQualifiedName().compareTo(entityInfo.getName()) == 0 || node.isConstructor()) {
        // this is constructor declaration
        return true;
    }
    // -) is it setter?
    if (// $NON-NLS-1$
    node.getName().getIdentifier().startsWith("set") && node.parameters().size() == 1) {
        // setter - do not process it
        return true;
    }
    // +) is it getter?
    if (!(// $NON-NLS-1$
    node.getName().getIdentifier().startsWith("get") || // $NON-NLS-1$
    node.getName().getIdentifier().startsWith("is")) || node.parameters().size() > 0) {
        // not the getter - do not process it
        return true;
    }
    Type type = node.getReturnType2();
    if (type == null) {
        return true;
    }
    String returnIdentifier = CollectEntityInfo.getReturnIdentifier(node);
    if (type.isSimpleType() || type.isPrimitiveType()) {
        if (entityInfo.isAddGeneratedValueFlag()) {
            String primaryIdName = entityInfo.getPrimaryIdName();
            boolean addGeneratedValueMarker = false;
            if (primaryIdName.equals(returnIdentifier)) {
                addGeneratedValueMarker = true;
            }
            if (addGeneratedValueMarker) {
                MarkerAnnotation matd = rewriter.getAST().newMarkerAnnotation();
                matd.setTypeName(rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_GENERATED_VALUE));
                ListRewrite lrw = rewriter.getListRewrite(node, MethodDeclaration.MODIFIERS2_PROPERTY);
                lrw.insertFirst(matd, null);
            }
        }
        if (entityInfo.isAddPrimaryIdFlag()) {
            String primaryIdName = entityInfo.getPrimaryIdName();
            boolean addIdMarker = false;
            if (primaryIdName.equals(returnIdentifier)) {
                addIdMarker = true;
            }
            if (addIdMarker) {
                MarkerAnnotation matd = rewriter.getAST().newMarkerAnnotation();
                matd.setTypeName(rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_ID));
                ListRewrite lrw = rewriter.getListRewrite(node, MethodDeclaration.MODIFIERS2_PROPERTY);
                lrw.insertFirst(matd, null);
            }
        }
        if (enableOptLock && entityInfo.isAddVersionFlag() && !entityInfo.hasVersionAnnotation()) {
            boolean addVersionMarker = false;
            if ("version".equals(returnIdentifier)) {
                // $NON-NLS-1$
                addVersionMarker = true;
            }
            if (addVersionMarker) {
                MarkerAnnotation matd = rewriter.getAST().newMarkerAnnotation();
                matd.setTypeName(rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_VERSION));
                ListRewrite lrw = rewriter.getListRewrite(node, MethodDeclaration.MODIFIERS2_PROPERTY);
                lrw.insertFirst(matd, null);
            }
        }
    }
    if (type.isSimpleType() && (AllEntitiesProcessor.columnLength != defaultStrLength)) {
        SimpleType simpleType = (SimpleType) type;
        String typeName = simpleType.getName().getFullyQualifiedName();
        if ("java.lang.String".equals(typeName) || "String".equals(typeName)) {
            // $NON-NLS-1$ //$NON-NLS-2$
            String fieldId = returnIdentifier;
            RefColumnInfo rci = entityInfo.getRefColumnInfo(fieldId);
            if (rci == null || !rci.isExist()) {
                // if there is no @Column annotation - create new @Column annotation
                // with user defined default value length
                NormalAnnotation natd = rewriter.getAST().newNormalAnnotation();
                natd.setTypeName(rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_COLUMN));
                ListRewrite lrw = rewriter.getListRewrite(node, MethodDeclaration.MODIFIERS2_PROPERTY);
                lrw.insertFirst(natd, null);
                MemberValuePair mvp = rewriter.getAST().newMemberValuePair();
                // $NON-NLS-1$
                mvp.setName(rewriter.getAST().newSimpleName("length"));
                NumberLiteral nl = rewriter.getAST().newNumberLiteral(String.valueOf(defaultStrLength));
                mvp.setValue(nl);
                natd.values().add(mvp);
            }
        }
    }
    if (type.isSimpleType() || type.isParameterizedType() || type.isArrayType()) {
        // $NON-NLS-1$
        String fieldId = "";
        RefType refType = RefType.UNDEF;
        boolean annotated = false;
        // $NON-NLS-1$
        String fullyQualifiedName2 = "";
        fieldId = returnIdentifier;
        refType = entityInfo.getFieldIdRelValue(fieldId);
        annotated = entityInfo.getFieldIdAnnotatedValue(fieldId);
        fullyQualifiedName2 = entityInfo.getFieldIdFQNameValue(fieldId);
        Set<RefFieldInfo> setRFI = entityInfo.getRefFieldInfoSet(fullyQualifiedName2);
        if (!annotated && setRFI != null && isSimilarType(type, fullyQualifiedName2)) {
            RefEntityInfo rei = entityInfo.getFieldIdRefEntityInfo(fieldId);
            // either side may be the owning side
            if (setRFI.size() > 1 && refType != RefType.MANY2ONE) {
                if (rei.mappedBy == null || rei.mappedBy == "") {
                    // $NON-NLS-1$
                    addSimpleMarkerAnnotation(node, JPAConst.getRefType(refType));
                } else {
                    // give to the user information about selected mapping
                    addComplexNormalAnnotation(node, JPAConst.getRefType(refType), rei);
                }
            } else if (refType == RefType.MANY2ONE || refType == RefType.ENUMERATED || rei.mappedBy == null || rei.mappedBy == "") {
                // $NON-NLS-1$
                addSimpleMarkerAnnotation(node, JPAConst.getRefType(refType));
            } else {
                // in case of bidirectional OneToOne - mark both sides with mappedBy -
                // user should select the right decision
                addComplexNormalAnnotation(node, JPAConst.getRefType(refType), rei);
            }
        }
    }
    return true;
}
Also used : ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) RefColumnInfo(org.hibernate.eclipse.jdt.ui.internal.jpa.common.RefColumnInfo) RefFieldInfo(org.hibernate.eclipse.jdt.ui.internal.jpa.common.RefFieldInfo) RefType(org.hibernate.eclipse.jdt.ui.internal.jpa.common.RefType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) RefEntityInfo(org.hibernate.eclipse.jdt.ui.internal.jpa.common.RefEntityInfo) OwnerType(org.hibernate.eclipse.jdt.ui.internal.jpa.common.OwnerType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) RefType(org.hibernate.eclipse.jdt.ui.internal.jpa.common.RefType) FieldGetterType(org.hibernate.eclipse.jdt.ui.internal.jpa.common.EntityInfo.FieldGetterType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) MarkerAnnotation(org.eclipse.jdt.core.dom.MarkerAnnotation) MemberValuePair(org.eclipse.jdt.core.dom.MemberValuePair) NormalAnnotation(org.eclipse.jdt.core.dom.NormalAnnotation) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral)

Example 28 with NumberLiteral

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

the class ProcessEntityInfo method visit.

@SuppressWarnings("unchecked")
public boolean visit(FieldDeclaration node) {
    if (entityInfo == null) {
        return false;
    }
    if (annotationStyle != AnnotStyle.FIELDS) {
        return true;
    }
    Type type = node.getType();
    if (type == null) {
        return true;
    }
    if (type.isSimpleType() || type.isPrimitiveType()) {
        if (entityInfo.isAddGeneratedValueFlag()) {
            String primaryIdName = entityInfo.getPrimaryIdName();
            Iterator<VariableDeclarationFragment> itVarNames = node.fragments().iterator();
            boolean addGeneratedValueMarker = false;
            while (itVarNames.hasNext()) {
                VariableDeclarationFragment var = itVarNames.next();
                String name = var.getName().getIdentifier();
                if (primaryIdName.equals(name)) {
                    addGeneratedValueMarker = true;
                    break;
                }
            }
            if (addGeneratedValueMarker) {
                MarkerAnnotation matd = rewriter.getAST().newMarkerAnnotation();
                matd.setTypeName(rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_GENERATED_VALUE));
                ListRewrite lrw = rewriter.getListRewrite(node, FieldDeclaration.MODIFIERS2_PROPERTY);
                lrw.insertFirst(matd, null);
            }
        }
        if (entityInfo.isAddPrimaryIdFlag()) {
            String primaryIdName = entityInfo.getPrimaryIdName();
            Iterator<VariableDeclarationFragment> itVarNames = node.fragments().iterator();
            boolean addIdMarker = false;
            while (itVarNames.hasNext()) {
                VariableDeclarationFragment var = itVarNames.next();
                String name = var.getName().getIdentifier();
                if (primaryIdName.equals(name)) {
                    addIdMarker = true;
                    break;
                }
            }
            if (addIdMarker) {
                MarkerAnnotation matd = rewriter.getAST().newMarkerAnnotation();
                matd.setTypeName(rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_ID));
                ListRewrite lrw = rewriter.getListRewrite(node, FieldDeclaration.MODIFIERS2_PROPERTY);
                lrw.insertFirst(matd, null);
            }
        }
        if (enableOptLock && entityInfo.isAddVersionFlag() && !entityInfo.hasVersionAnnotation()) {
            Iterator<VariableDeclarationFragment> itVarNames = node.fragments().iterator();
            boolean addVersionMarker = false;
            while (itVarNames.hasNext()) {
                VariableDeclarationFragment var = itVarNames.next();
                String name = var.getName().getIdentifier();
                if ("version".equals(name)) {
                    // $NON-NLS-1$
                    addVersionMarker = true;
                    break;
                }
            }
            if (addVersionMarker) {
                MarkerAnnotation matd = rewriter.getAST().newMarkerAnnotation();
                matd.setTypeName(rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_VERSION));
                ListRewrite lrw = rewriter.getListRewrite(node, FieldDeclaration.MODIFIERS2_PROPERTY);
                lrw.insertFirst(matd, null);
            }
        }
    }
    if (type.isSimpleType() && (AllEntitiesProcessor.columnLength != defaultStrLength)) {
        SimpleType simpleType = (SimpleType) type;
        String typeName = simpleType.getName().getFullyQualifiedName();
        if ("java.lang.String".equals(typeName) || "String".equals(typeName)) {
            // $NON-NLS-1$ //$NON-NLS-2$
            String fieldId = null;
            Iterator<VariableDeclarationFragment> itVarNames = node.fragments().iterator();
            while (itVarNames.hasNext()) {
                VariableDeclarationFragment var = itVarNames.next();
                fieldId = var.getName().getIdentifier();
                if (fieldId != null) {
                    break;
                }
            }
            RefColumnInfo rci = entityInfo.getRefColumnInfo(fieldId);
            if (rci == null || !rci.isExist()) {
                // if there is no @Column annotation - create new @Column annotation
                // with user defined default value length
                NormalAnnotation natd = rewriter.getAST().newNormalAnnotation();
                natd.setTypeName(rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_COLUMN));
                ListRewrite lrw = rewriter.getListRewrite(node, FieldDeclaration.MODIFIERS2_PROPERTY);
                lrw.insertFirst(natd, null);
                MemberValuePair mvp = rewriter.getAST().newMemberValuePair();
                // $NON-NLS-1$
                mvp.setName(rewriter.getAST().newSimpleName("length"));
                NumberLiteral nl = rewriter.getAST().newNumberLiteral(String.valueOf(defaultStrLength));
                mvp.setValue(nl);
                natd.values().add(mvp);
            }
        }
    }
    if (type.isSimpleType() || type.isParameterizedType() || type.isArrayType()) {
        Iterator<VariableDeclarationFragment> itVarNames = node.fragments().iterator();
        // $NON-NLS-1$
        String fieldId = "";
        RefType refType = RefType.UNDEF;
        boolean annotated = false;
        // $NON-NLS-1$
        String fullyQualifiedName2 = "";
        while (itVarNames.hasNext()) {
            VariableDeclarationFragment var = itVarNames.next();
            String name = var.getName().getIdentifier();
            fieldId = name;
            refType = entityInfo.getFieldIdRelValue(fieldId);
            annotated = entityInfo.getFieldIdAnnotatedValue(fieldId);
            fullyQualifiedName2 = entityInfo.getFieldIdFQNameValue(fieldId);
            if (refType != RefType.UNDEF) {
                break;
            }
        }
        Set<RefFieldInfo> setRFI = entityInfo.getRefFieldInfoSet(fullyQualifiedName2);
        if (!annotated && setRFI != null && isSimilarType(type, fullyQualifiedName2)) {
            RefEntityInfo rei = entityInfo.getFieldIdRefEntityInfo(fieldId);
            // either side may be the owning side
            if (setRFI.size() > 1 && refType != RefType.MANY2ONE) {
                if (rei.mappedBy == null || rei.mappedBy == "") {
                    // $NON-NLS-1$
                    addSimpleMarkerAnnotation(node, JPAConst.getRefType(refType));
                } else {
                    // give to the user information about selected mapping
                    addComplexNormalAnnotation(node, JPAConst.getRefType(refType), rei);
                }
            } else if (refType == RefType.MANY2ONE || refType == RefType.ENUMERATED || rei.mappedBy == null || rei.mappedBy == "") {
                // $NON-NLS-1$
                addSimpleMarkerAnnotation(node, JPAConst.getRefType(refType));
            } else {
                // in case of bidirectional OneToOne - mark both sides with mappedBy -
                // user should select the right decision
                addComplexNormalAnnotation(node, JPAConst.getRefType(refType), rei);
            }
        }
    }
    return true;
}
Also used : ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) RefColumnInfo(org.hibernate.eclipse.jdt.ui.internal.jpa.common.RefColumnInfo) RefFieldInfo(org.hibernate.eclipse.jdt.ui.internal.jpa.common.RefFieldInfo) RefType(org.hibernate.eclipse.jdt.ui.internal.jpa.common.RefType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) RefEntityInfo(org.hibernate.eclipse.jdt.ui.internal.jpa.common.RefEntityInfo) OwnerType(org.hibernate.eclipse.jdt.ui.internal.jpa.common.OwnerType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) RefType(org.hibernate.eclipse.jdt.ui.internal.jpa.common.RefType) FieldGetterType(org.hibernate.eclipse.jdt.ui.internal.jpa.common.EntityInfo.FieldGetterType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) MarkerAnnotation(org.eclipse.jdt.core.dom.MarkerAnnotation) MemberValuePair(org.eclipse.jdt.core.dom.MemberValuePair) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) NormalAnnotation(org.eclipse.jdt.core.dom.NormalAnnotation) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral)

Aggregations

NumberLiteral (org.eclipse.jdt.core.dom.NumberLiteral)28 Expression (org.eclipse.jdt.core.dom.Expression)11 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)11 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)10 CastExpression (org.eclipse.jdt.core.dom.CastExpression)9 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)8 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)6 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)6 StringLiteral (org.eclipse.jdt.core.dom.StringLiteral)6 SuperMethodInvocation (org.eclipse.jdt.core.dom.SuperMethodInvocation)6 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)6 ASTRewrite (org.autorefactor.jdt.core.dom.ASTRewrite)5 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)5 Name (org.eclipse.jdt.core.dom.Name)5 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)4 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)4 SimpleType (org.eclipse.jdt.core.dom.SimpleType)4 TextEditGroup (org.eclipse.text.edits.TextEditGroup)4 ASTNodeFactory (org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory)3 ASTBuilder (org.autorefactor.refactoring.ASTBuilder)3