All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gr.uom.java.xmi.decomposition.StatementObject Maven / Gradle / Ivy

package gr.uom.java.xmi.decomposition;

import java.util.ArrayList;
import java.util.List;

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.VariableDeclarationContainer;
import gr.uom.java.xmi.diff.CodeRange;
import static gr.uom.java.xmi.decomposition.Visitor.stringify;

public class StatementObject extends AbstractStatement {
	
	private String statement;
	private LocationInfo locationInfo;
	private List variables;
	private List types;
	private List variableDeclarations;
	private List methodInvocations;
	private List anonymousClassDeclarations;
	private List textBlocks;
	private List stringLiterals;
	private List charLiterals;
	private List numberLiterals;
	private List nullLiterals;
	private List booleanLiterals;
	private List typeLiterals;
	private List creations;
	private List infixExpressions;
	private List assignments;
	private List infixOperators;
	private List arrayAccesses;
	private List prefixExpressions;
	private List postfixExpressions;
	private List thisExpressions;
	private List arguments;
	private List parenthesizedExpressions;
	private List castExpressions;
	private List ternaryOperatorExpressions;
	private List lambdas;
	
	public StatementObject(CompilationUnit cu, String filePath, Statement statement, int depth, CodeElementType codeElementType, VariableDeclarationContainer container) {
		super();
		this.locationInfo = new LocationInfo(cu, filePath, statement, codeElementType);
		Visitor visitor = new Visitor(cu, filePath, container);
		statement.accept(visitor);
		this.variables = visitor.getVariables();
		this.types = visitor.getTypes();
		this.variableDeclarations = visitor.getVariableDeclarations();
		this.methodInvocations = visitor.getMethodInvocations();
		this.anonymousClassDeclarations = visitor.getAnonymousClassDeclarations();
		this.textBlocks = visitor.getTextBlocks();
		this.stringLiterals = visitor.getStringLiterals();
		this.charLiterals = visitor.getCharLiterals();
		this.numberLiterals = visitor.getNumberLiterals();
		this.nullLiterals = visitor.getNullLiterals();
		this.booleanLiterals = visitor.getBooleanLiterals();
		this.typeLiterals = visitor.getTypeLiterals();
		this.creations = visitor.getCreations();
		this.infixExpressions = visitor.getInfixExpressions();
		this.assignments = visitor.getAssignments();
		this.infixOperators = visitor.getInfixOperators();
		this.arrayAccesses = visitor.getArrayAccesses();
		this.prefixExpressions = visitor.getPrefixExpressions();
		this.postfixExpressions = visitor.getPostfixExpressions();
		this.thisExpressions = visitor.getThisExpressions();
		this.arguments = visitor.getArguments();
		this.parenthesizedExpressions = visitor.getParenthesizedExpressions();
		this.castExpressions = visitor.getCastExpressions();
		this.ternaryOperatorExpressions = visitor.getTernaryOperatorExpressions();
		this.lambdas = visitor.getLambdas();
		setDepth(depth);
		String statementAsString = stringify(statement);
		if(Visitor.METHOD_INVOCATION_PATTERN.matcher(statementAsString).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(stringify(variableDeclarationStatement.getType()));
				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 = statementAsString;
			}
		}
		else {
			this.statement = statementAsString;
		}
	}

	@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 List getMethodInvocations() {
		return methodInvocations;
	}

	@Override
	public List getAnonymousClassDeclarations() {
		return anonymousClassDeclarations;
	}

	@Override
	public List getTextBlocks() {
		return textBlocks;
	}

	@Override
	public List getStringLiterals() {
		return stringLiterals;
	}

	@Override
	public List getCharLiterals() {
		return charLiterals;
	}

	@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 List getCreations() {
		return creations;
	}

	@Override
	public List getInfixExpressions() {
		return infixExpressions;
	}

	@Override
	public List getAssignments() {
		return assignments;
	}

	@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 getThisExpressions() {
		return thisExpressions;
	}

	@Override
	public List getArguments() {
		return arguments;
	}

	@Override
	public List getParenthesizedExpressions() {
		return parenthesizedExpressions;
	}

	@Override
	public List getCastExpressions() {
		return castExpressions;
	}

	@Override
	public List getTernaryOperatorExpressions() {
		return ternaryOperatorExpressions;
	}

	@Override
	public List getLambdas() {
		return lambdas;
	}

	@Override
	public int statementCount() {
		return 1;
	}

	@Override
	public int statementCountIncludingBlocks() {
		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;
			}
		}
		for(LambdaExpressionObject lambda : getLambdas()) {
			for(VariableDeclaration declaration : lambda.getParameters()) {
				if(declaration.getVariableName().equals(variableName)) {
					return declaration;
				}
			}
		}
		return null;
	}

	@Override
	public List stringRepresentation() {
		List stringRepresentation = new ArrayList();
		stringRepresentation.add(this.toString());
		return stringRepresentation;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy