Search in sources :

Example 71 with StringLiteral

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

the class JUnitCodeGenerator method createFieldWriteAccessStmt.

@SuppressWarnings("unchecked")
@Override
public void createFieldWriteAccessStmt(CaptureLog log, int logRecNo) {
    // assumption: all necessary statements are created and there is one variable for reach referenced object
    final Object[] methodArgs = log.params.get(logRecNo);
    final String methodName = log.methodNames.get(logRecNo);
    final int oid = log.objectIds.get(logRecNo);
    final int captureId = log.captureIds.get(logRecNo);
    final String fieldName = log.namesOfAccessedFields.get(captureId);
    final String typeName = log.oidClassNames.get(log.oidRecMapping.get(oid));
    final Class<?> type = getClassForName(typeName);
    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)) {
            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 = log.descList.get(logRecNo);
            final Class<?> fieldType = CaptureUtil.getClassFromDesc(fieldDesc);
            if (fieldType.isAssignableFrom(argType) || fieldType.isPrimitive()) {
                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 72 with StringLiteral

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

the class JUnitCodeGenerator method createUnobservedInitStmt.

@SuppressWarnings("unchecked")
@Override
public void createUnobservedInitStmt(CaptureLog log, int logRecNo) {
    PostProcessor.notifyRecentlyProcessedLogRecNo(logRecNo);
    // NOTE: PLAIN INIT: has always one non-null param
    // TODO: use primitives
    final int oid = log.objectIds.get(logRecNo);
    // final String type    = log.oidClassNames.get(log.oidRecMapping.get(oid));
    final Object value = 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, log.oidClassNames.get(log.oidRecMapping.get(oid)), true)));
    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);
    vd.setInitializer(methodInvocation);
    final VariableDeclarationStatement vs = ast.newVariableDeclarationStatement(vd);
    vs.setType(this.createAstType("Object", 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) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name)

Example 73 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral in project quarkus-ls by redhat-developer.

the class AbstractQuteTemplateLinkCollector method processTemplateLink.

private void processTemplateLink(FieldDeclaration node) {
    List modifiers = node.modifiers();
    if (modifiers != null) {
        for (Object modifier : modifiers) {
            if (modifier instanceof SingleMemberAnnotation) {
                SingleMemberAnnotation annotation = (SingleMemberAnnotation) modifier;
                if (AnnotationUtils.isMatchAnnotation(annotation, LOCATION_ANNOTATION)) {
                    // @Location("/items/my.items.qute.html")
                    // Template items;
                    Expression expression = annotation.getValue();
                    if (expression != null && expression instanceof StringLiteral) {
                        String location = ((StringLiteral) expression).getLiteralValue();
                        if (StringUtils.isNotBlank(location)) {
                            processTemplateLink(node, (TypeDeclaration) node.getParent(), null, null, location);
                        }
                        return;
                    }
                }
            }
        }
    }
    List fragments = node.fragments();
    if (fragments != null && !fragments.isEmpty()) {
        VariableDeclaration variable = (VariableDeclaration) fragments.get(0);
        String fieldName = variable.getName().toString();
        processTemplateLink(node, (TypeDeclaration) node.getParent(), null, fieldName, null);
    }
}
Also used : StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) Expression(org.eclipse.jdt.core.dom.Expression) SingleMemberAnnotation(org.eclipse.jdt.core.dom.SingleMemberAnnotation) List(java.util.List) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration)

Example 74 with StringLiteral

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

the class MicroProfileConfigASTValidator method validatePropertyDefaultValue.

/**
 * Validate "defaultValue" attribute of <code>@ConfigProperty</code> and
 * generate diagnostics if "defaultValue" cannot be represented by the given
 * field type.
 *
 * See
 * https://github.com/eclipse/microprofile-config/blob/master/spec/src/main/asciidoc/converters.asciidoc
 * for more details on default converters.
 *
 * @param annotation       the annotation to validate the defaultValue of
 * @param defaultValueExpr the default value expression, or null if no default
 *                         value is defined
 */
private void validatePropertyDefaultValue(Annotation annotation, Expression defaultValueExpr) {
    FieldDeclaration fieldDeclaration = (FieldDeclaration) annotation.getParent();
    if (defaultValueExpr != null) {
        String defValue = ((StringLiteral) defaultValueExpr).getLiteralValue();
        ITypeBinding fieldBinding = fieldDeclaration.getType().resolveBinding();
        if (fieldBinding != null && !isAssignable(fieldBinding, defValue)) {
            String message = MessageFormat.format(EXPECTED_TYPE_ERROR_MESSAGE, defValue, fieldBinding.getName());
            super.addDiagnostic(message, MICRO_PROFILE_CONFIG_DIAGNOSTIC_SOURCE, defaultValueExpr, MicroProfileConfigErrorCode.DEFAULT_VALUE_IS_WRONG_TYPE, DiagnosticSeverity.Error);
        }
    }
}
Also used : StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

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