Skip to content
This repository was archived by the owner on May 13, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/main/java/jp/naist/sd/kenja/factextractor/ast/ASTMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public class ASTMethod implements Treeable {
*/
private Blob parameters;

/**
* A Blob isntance corresponding to local variables in the method.
*/
private Blob variables;

/**
* root Tree of a Method.
*/
Expand All @@ -42,6 +47,8 @@ public class ASTMethod implements Treeable {
*/
private static final String PARAMETERS_BLOB_NAME = "parameters";

private static final String VARIABLES_BLOB_NAME = "variables";

/**
* True if method is a constructor.
*/
Expand Down Expand Up @@ -72,6 +79,7 @@ protected ASTMethod(MethodDeclaration node) {
isConstructor = node.isConstructor();
setBody(node);
setParameters(node.parameters());
setVariables(node);
}

/**
Expand Down Expand Up @@ -142,6 +150,27 @@ private void setParameters(List parametersList) {
parameters.setBody(parameterBody);
}

/**
* Read and set local variables to the Blob.
* @param node
*/
private void setVariables(MethodDeclaration node) {
variables = new Blob(VARIABLES_BLOB_NAME);
root.append(variables);

VariableDeclarationVisitor visitor = new VariableDeclarationVisitor();
node.getBody().accept(visitor);
visitor.sortVariableList();

StringBuilder body = new StringBuilder();
for (String variable : visitor.getVariables()) {
body.append(variable);
body.append("\n");
}

variables.setBody(body.toString());
}

/**
* return directory name of the method.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package jp.naist.sd.kenja.factextractor.ast;

import com.google.common.collect.Ordering;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;

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

public class VariableDeclarationVisitor extends ASTVisitor {
private List<String> variables;

public VariableDeclarationVisitor() {
variables = new ArrayList<String>();
}

public List<String> getVariables() {
return variables;
}

public void sortVariableList() {
variables = Ordering.from(String.CASE_INSENSITIVE_ORDER).sortedCopy(variables);
}

@Override
public boolean visit(VariableDeclarationStatement node) {
StringBuilder variableNameBase = new StringBuilder();
variableNameBase.append(node.getType());
variableNameBase.append(" ");
for (Object obj : node.fragments()) {
StringBuilder variableName = new StringBuilder(variableNameBase);
VariableDeclarationFragment fragment = (VariableDeclarationFragment)obj;
variableName.append(fragment.getName());
for (int dimension = 0; dimension < fragment.getExtraDimensions(); dimension++) {
variableName.append("[]");
}
variables.add(variableName.toString());
}

return true;
}
}