gr.uom.java.xmi.decomposition.StatementObject Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of refactoring-miner Show documentation
Show all versions of refactoring-miner Show documentation
RefactoringMiner is a library/API written in Java that can detect refactorings applied in the history of a Java project.
package gr.uom.java.xmi.decomposition;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.ExpressionStatement;
import org.eclipse.jdt.core.dom.IExtendedModifier;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.ReturnStatement;
import org.eclipse.jdt.core.dom.Statement;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
import gr.uom.java.xmi.LocationInfo;
import gr.uom.java.xmi.LocationInfo.CodeElementType;
import gr.uom.java.xmi.diff.CodeRange;
public class StatementObject extends AbstractStatement {
private String statement;
private LocationInfo locationInfo;
private List variables;
private List types;
private List variableDeclarations;
private Map> methodInvocationMap;
private List anonymousClassDeclarations;
private List stringLiterals;
private List numberLiterals;
private List nullLiterals;
private List booleanLiterals;
private List typeLiterals;
private Map> creationMap;
private List infixOperators;
private List arrayAccesses;
private List prefixExpressions;
private List postfixExpressions;
private List arguments;
private List ternaryOperatorExpressions;
private List lambdas;
public StatementObject(CompilationUnit cu, String filePath, Statement statement, int depth, CodeElementType codeElementType) {
super();
this.locationInfo = new LocationInfo(cu, filePath, statement, codeElementType);
Visitor visitor = new Visitor(cu, filePath);
statement.accept(visitor);
this.variables = visitor.getVariables();
this.types = visitor.getTypes();
this.variableDeclarations = visitor.getVariableDeclarations();
this.methodInvocationMap = visitor.getMethodInvocationMap();
this.anonymousClassDeclarations = visitor.getAnonymousClassDeclarations();
this.stringLiterals = visitor.getStringLiterals();
this.numberLiterals = visitor.getNumberLiterals();
this.nullLiterals = visitor.getNullLiterals();
this.booleanLiterals = visitor.getBooleanLiterals();
this.typeLiterals = visitor.getTypeLiterals();
this.creationMap = visitor.getCreationMap();
this.infixOperators = visitor.getInfixOperators();
this.arrayAccesses = visitor.getArrayAccesses();
this.prefixExpressions = visitor.getPrefixExpressions();
this.postfixExpressions = visitor.getPostfixExpressions();
this.arguments = visitor.getArguments();
this.ternaryOperatorExpressions = visitor.getTernaryOperatorExpressions();
this.lambdas = visitor.getLambdas();
setDepth(depth);
if(Visitor.METHOD_INVOCATION_PATTERN.matcher(statement.toString()).matches()) {
if(statement instanceof VariableDeclarationStatement) {
VariableDeclarationStatement variableDeclarationStatement = (VariableDeclarationStatement)statement;
StringBuilder sb = new StringBuilder();
List modifiers = variableDeclarationStatement.modifiers();
for(IExtendedModifier modifier : modifiers) {
sb.append(modifier.toString()).append(" ");
}
sb.append(variableDeclarationStatement.getType().toString());
List fragments = variableDeclarationStatement.fragments();
for(VariableDeclarationFragment fragment : fragments) {
sb.append(fragment.getName().getIdentifier());
Expression initializer = fragment.getInitializer();
if(initializer != null) {
sb.append(" = ");
if(initializer instanceof MethodInvocation) {
MethodInvocation methodInvocation = (MethodInvocation)initializer;
sb.append(Visitor.processMethodInvocation(methodInvocation));
}
else if(initializer instanceof ClassInstanceCreation) {
ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation)initializer;
sb.append(Visitor.processClassInstanceCreation(classInstanceCreation));
}
}
}
this.statement = sb.toString();
}
else if(statement instanceof ReturnStatement) {
ReturnStatement returnStatement = (ReturnStatement)statement;
StringBuilder sb = new StringBuilder();
sb.append("return").append(" ");
Expression expression = returnStatement.getExpression();
if(expression instanceof MethodInvocation) {
MethodInvocation methodInvocation = (MethodInvocation)expression;
sb.append(Visitor.processMethodInvocation(methodInvocation));
}
else if(expression instanceof ClassInstanceCreation) {
ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation)expression;
sb.append(Visitor.processClassInstanceCreation(classInstanceCreation));
}
this.statement = sb.toString();
}
else if(statement instanceof ExpressionStatement) {
ExpressionStatement expressionStatement = (ExpressionStatement)statement;
StringBuilder sb = new StringBuilder();
Expression expression = expressionStatement.getExpression();
if(expression instanceof MethodInvocation) {
MethodInvocation methodInvocation = (MethodInvocation)expression;
sb.append(Visitor.processMethodInvocation(methodInvocation));
}
else if(expression instanceof ClassInstanceCreation) {
ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation)expression;
sb.append(Visitor.processClassInstanceCreation(classInstanceCreation));
}
this.statement = sb.toString();
}
else {
this.statement = statement.toString();
}
}
else {
this.statement = statement.toString();
}
}
public List stringRepresentation() {
List stringRepresentation = new ArrayList();
stringRepresentation.add(this.toString());
return stringRepresentation;
}
@Override
public List getLeaves() {
List leaves = new ArrayList();
leaves.add(this);
return leaves;
}
public String toString() {
return statement;
}
@Override
public List getVariables() {
return variables;
}
@Override
public List getTypes() {
return types;
}
@Override
public List getVariableDeclarations() {
return variableDeclarations;
}
@Override
public Map> getMethodInvocationMap() {
return methodInvocationMap;
}
@Override
public List getAnonymousClassDeclarations() {
return anonymousClassDeclarations;
}
@Override
public List getStringLiterals() {
return stringLiterals;
}
@Override
public List getNumberLiterals() {
return numberLiterals;
}
@Override
public List getNullLiterals() {
return nullLiterals;
}
@Override
public List getBooleanLiterals() {
return booleanLiterals;
}
@Override
public List getTypeLiterals() {
return typeLiterals;
}
@Override
public Map> getCreationMap() {
return creationMap;
}
@Override
public List getInfixOperators() {
return infixOperators;
}
@Override
public List getArrayAccesses() {
return arrayAccesses;
}
@Override
public List getPrefixExpressions() {
return prefixExpressions;
}
@Override
public List getPostfixExpressions() {
return postfixExpressions;
}
@Override
public List getArguments() {
return arguments;
}
@Override
public List getTernaryOperatorExpressions() {
return ternaryOperatorExpressions;
}
@Override
public List getLambdas() {
return lambdas;
}
@Override
public int statementCount() {
return 1;
}
public LocationInfo getLocationInfo() {
return locationInfo;
}
public CodeRange codeRange() {
return locationInfo.codeRange();
}
public VariableDeclaration getVariableDeclaration(String variableName) {
List variableDeclarations = getVariableDeclarations();
for(VariableDeclaration declaration : variableDeclarations) {
if(declaration.getVariableName().equals(variableName)) {
return declaration;
}
}
return null;
}
}