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));
}
}
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);
}
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);
}
}
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);
}
}
}
Aggregations