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