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