Skip to content

compilable variants #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
4 changes: 0 additions & 4 deletions src/clegoues/genprog4java/Search/package-info.java

This file was deleted.

7 changes: 1 addition & 6 deletions src/clegoues/genprog4java/fitness/Fitness.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.commons.lang3.tuple.Pair;
Expand All @@ -68,8 +65,6 @@
import org.junit.runner.Request;

import clegoues.genprog4java.main.Configuration;
import clegoues.genprog4java.mut.Mutation;
import clegoues.genprog4java.mut.WeightedMutation;
import clegoues.genprog4java.rep.Representation;
import clegoues.util.ConfigurationBuilder;
import junit.framework.Test;
Expand Down Expand Up @@ -485,7 +480,7 @@ private int testPassCount(Representation rep, boolean shortCircuit, List<TestCas

/**
* Test a variant sequentially on all tests, starting with the negative tests.
* Quits as soon as a failed test is found. Uses the test model @see {@link clegoues.genprog4java.Search.TrpAutoRepair}
* Quits as soon as a failed test is found. Uses the test model @see {@link clegoues.genprog4java.search.TrpAutoRepair}
* if specified. Does not sample.
* @param rep variant to be tested
* @param withModel whether to use the testModel
Expand Down
88 changes: 47 additions & 41 deletions src/clegoues/genprog4java/java/ASTUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,46 +34,40 @@
package clegoues.genprog4java.java;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.*;

import javax.tools.SimpleJavaFileObject;

import org.apache.commons.lang3.tuple.Pair;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.ParameterizedType;
import org.eclipse.jdt.core.dom.PrimitiveType;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.dom.WildcardType;

/** provides static utils for manipulating java ASTs. Used to be much longer
import org.eclipse.jdt.core.dom.*;

/**
* provides static utils for manipulating java ASTs. Used to be much longer
* before I refactored semantic check info; it may be possible to refactor this away
* at some point.
* @author clegoues
*
* @author clegoues
*/
public class ASTUtils {

public static ASTNode getEnclosingMethod(ASTNode node) {
public static ASTNode getEnclosing(Class<? extends ASTNode> desiredParent, ASTNode node) {
ASTNode parent = node.getParent();
while(parent != null && !(parent instanceof MethodDeclaration)){
while (parent != null && !desiredParent.isInstance(parent)) {
parent = parent.getParent();
}
return parent;
}

/**
public static ASTNode getEnclosingMethod(ASTNode node) {
return getEnclosing(MethodDeclaration.class, node);
}

/**
* @param node ASTNode of interest
* @return line number corresponding to the first line of the node in its CU.
* @return line number corresponding to the first line of the node in its CU.
* Note that ASTNodes can span multiple lines; this returns the first.
*/
public static int getLineNumber(ASTNode node) {
public static int getLineNumber(ASTNode node) {
ASTNode root = node.getRoot();
int lineno = -1;
if (root instanceof CompilationUnit) {
Expand All @@ -86,9 +80,10 @@ public static int getLineNumber(ASTNode node) {

/**
* parses/creates java code from a string, rather than a file on disk.
*
* @param progName
* @param code
* @return collection of objects containing the java code.
* @param code
* @return collection of objects containing the java code.
*/
public static Iterable<JavaSourceFromString> getJavaSourceFromString(
String progName, List<Pair<ClassInfo, String>> code) {
Expand All @@ -105,46 +100,48 @@ public static Iterable<JavaSourceFromString> getJavaSourceFromString(

}

/** create a new Type object from a typeBinding, a hilariously
/**
* create a new Type object from a typeBinding, a hilariously
* difficult thing to do.
*
* @param ast
* @param typeBinding
* @return
*/
public static Type typeFromBinding(AST ast, ITypeBinding typeBinding) {
if( ast == null )
if (ast == null)
throw new NullPointerException("ast is null");
if( typeBinding == null )
if (typeBinding == null)
throw new NullPointerException("typeBinding is null");

if( typeBinding.isPrimitive() ) {
if (typeBinding.isPrimitive()) {
return ast.newPrimitiveType(
PrimitiveType.toCode(typeBinding.getName()));
}

if( typeBinding.isCapture() ) {
if (typeBinding.isCapture()) {
ITypeBinding wildCard = typeBinding.getWildcard();
WildcardType capType = ast.newWildcardType();
ITypeBinding bound = wildCard.getBound();
if( bound != null ) {
if (bound != null) {
capType.setBound(typeFromBinding(ast, bound),
wildCard.isUpperbound());
}
return capType;
}

if( typeBinding.isArray() ) {
if (typeBinding.isArray()) {
Type elType = typeFromBinding(ast, typeBinding.getElementType());
return ast.newArrayType(elType, typeBinding.getDimensions());
}

if( typeBinding.isParameterizedType() ) {
if (typeBinding.isParameterizedType()) {
ParameterizedType type = ast.newParameterizedType(
typeFromBinding(ast, typeBinding.getErasure()));

@SuppressWarnings("unchecked")
List<Type> newTypeArgs = type.typeArguments();
for( ITypeBinding typeArg : typeBinding.getTypeArguments() ) {
for (ITypeBinding typeArg : typeBinding.getTypeArguments()) {
newTypeArgs.add(typeFromBinding(ast, typeArg));
}

Expand All @@ -153,36 +150,45 @@ public static Type typeFromBinding(AST ast, ITypeBinding typeBinding) {

// simple or raw type
String qualName = typeBinding.getQualifiedName();
if( "".equals(qualName) ) {
if ("".equals(qualName)) {
throw new IllegalArgumentException("No name for type binding.");
}
return ast.newSimpleType(ast.newName(qualName));
}


private static MethodDeclaration getMethodDeclaration(ASTNode node) {
while(node != null && node.getParent() != null && !(node instanceof MethodDeclaration)) {
while (node != null && node.getParent() != null && !(node instanceof MethodDeclaration)) {
node = node.getParent();
}
return (MethodDeclaration) node;
}

public static ASTNode getDefaultReturn(ASTNode node, AST hostAST) {
MethodDeclaration md = getMethodDeclaration(node);
if(md == null)
if (md == null)
return null;
Type returnType = md.getReturnType2();
if(returnType.isPrimitiveType()) {
if (returnType.isPrimitiveType()) {
PrimitiveType casted = (PrimitiveType) returnType;
PrimitiveType.Code tc = casted.getPrimitiveTypeCode();
if(tc == PrimitiveType.BOOLEAN) {
if (tc == PrimitiveType.BOOLEAN) {
return hostAST.newBooleanLiteral(false);
}
if(tc == PrimitiveType.CHAR || tc == PrimitiveType.BYTE || tc == PrimitiveType.INT || tc == PrimitiveType.SHORT)
if (tc == PrimitiveType.CHAR || tc == PrimitiveType.BYTE || tc == PrimitiveType.INT || tc == PrimitiveType.SHORT)
return hostAST.newNumberLiteral("0");
if(tc == PrimitiveType.DOUBLE || tc == PrimitiveType.FLOAT || tc == PrimitiveType.LONG)
if (tc == PrimitiveType.DOUBLE || tc == PrimitiveType.FLOAT || tc == PrimitiveType.LONG)
return hostAST.newNumberLiteral("0.0");
}
return hostAST.newNullLiteral();
}
return hostAST.newNullLiteral();
}

public static boolean isVariableDeclaration(ASTNode node) {
if (node instanceof ExpressionStatement) {
ExpressionStatement es = (ExpressionStatement) node;
if (es.getExpression() instanceof VariableDeclarationExpression) return true;
}
return node instanceof VariableDeclarationStatement;
}
}

Expand Down
41 changes: 41 additions & 0 deletions src/clegoues/genprog4java/java/CFBlockInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package clegoues.genprog4java.java;

import org.eclipse.jdt.core.dom.*;

import java.util.HashSet;
import java.util.Set;
import java.util.Stack;

public class CFBlockInfo {
private Set<ASTNode> lastStatementsInCFBlocks = new HashSet<>();
private Stack<ASTNode> currentCFBlock = new Stack<>();
ASTNode currentNode;

public boolean isLastStatementInCFBlock(ASTNode node) {
return lastStatementsInCFBlocks.contains(node);
}

public void registerNode(ASTNode node) {
if(isCFStatement(node)) {
currentCFBlock.push(node);
}
currentNode = node;
}

public void endOfNode(ASTNode node) {
if (node.equals(currentCFBlock.peek())) {
currentCFBlock.pop();
lastStatementsInCFBlocks.add(currentNode);
}
}

public static boolean isCFStatement(ASTNode node) {
return node instanceof EnhancedForStatement ||
node instanceof ForStatement ||
node instanceof DoStatement ||
node instanceof IfStatement ||
node instanceof SwitchStatement ||
node instanceof TryStatement ||
node instanceof WhileStatement;
}
}
3 changes: 2 additions & 1 deletion src/clegoues/genprog4java/java/JavaParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public class JavaParser
*/
private HashSet<String> availableMethodsAndFields;

public JavaParser(ScopeInfo scopeList)
public JavaParser(ScopeInfo scopeList, CFBlockInfo cfBlockInfo)
{
this.stmts = new LinkedList<ASTNode>();
this.methodReturnType = new HashMap<String,String>();
Expand All @@ -94,6 +94,7 @@ public JavaParser(ScopeInfo scopeList)

this.visitor.setNodeSet(this.stmts);
this.visitor.setScopeList(scopeList);
this.visitor.setCFBlockInfo(cfBlockInfo);
this.visitor.setMethodReturnType(methodReturnType);
this.visitor.setVariableType(variableTypes);

Expand Down
Loading