Here are the examples of the java api jdk.test.lib.jittester.Type taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
81 Examples
19
Source : JavaCodeVisitor.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
@Override
public String visit(Type node) {
return node.getName();
}
19
Source : TypeUtil.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
/**
* Gets a list of explicitly castable types to a given one from the collection of types
*
* @param types a collection to get types from
* @param type a target type which result type could be explicitly cast to
* @return a result collection of types that match given conditions
*/
public static Collection<Type> getExplicitlyCastable(Collection<Type> types, Type type) {
return types.stream().filter(t -> t.canExplicitlyCastTo(type)).collect(Collectors.toList());
}
19
Source : TypeUtil.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
/**
* Gets a list of implicitly castable types to a given one from the collection of types
*
* @param types a collection to get types from
* @param type a target type which result type could be implicitly cast to
* @return a result collection of types that match given conditions
*/
public static Collection<Type> getImplicitlyCastable(Collection<Type> types, Type type) {
return types.stream().filter(t -> t.canImplicitlyCastTo(type)).collect(Collectors.toList());
}
19
Source : TypeVoid.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
@Override
public boolean canExplicitlyCastTo(Type t) {
return false;
}
19
Source : TypeVoid.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
@Override
public boolean canEquateTo(Type t) {
return false;
}
19
Source : TypeVoid.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
@Override
public boolean canCompareTo(Type t) {
return false;
}
19
Source : TypeVoid.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
@Override
public boolean canImplicitlyCastTo(Type t) {
return false;
}
19
Source : TypeKlass.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
@Override
public boolean canImplicitlyCastTo(Type t) {
// We can implicitly cast to anything up the hierarchy and to self
if (t instanceof TypeKlreplaced) {
return equals(t) || getAllParents().contains(t);
}
return false;
}
19
Source : TypeKlass.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
@Override
public boolean canEquateTo(Type t) {
return true;
}
19
Source : TypeKlass.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
// If canExplicitlyCastTo() returns true in this case it doesn't mean that
// it would really be successful. Since explicit casts are inherintly dynamic
// we cannot guarantee that no exception will occur.
@Override
public boolean canExplicitlyCastTo(Type t) {
if (equals(t)) {
return true;
}
if (t instanceof TypeKlreplaced && !ProductionParams.disableDowncasts.value()) {
return getAllChildren().contains(t);
}
return false;
}
19
Source : TypeChar.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
@Override
public boolean canImplicitlyCastTo(Type t) {
if (equals(t)) {
return true;
}
try {
BuiltInType _t = (BuiltInType) t;
if (_t.isMoreCapaciousThan(this)) {
return true;
}
} catch (Exception e) {
}
return false;
}
19
Source : TypeBoolean.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
@Override
public boolean canEquateTo(Type t) {
return equals(t);
}
19
Source : TypeBoolean.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
@Override
public boolean canImplicitlyCastTo(Type t) {
return equals(t);
}
19
Source : TypeBoolean.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
@Override
public boolean canExplicitlyCastTo(Type t) {
return equals(t);
}
19
Source : TypeArray.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
public clreplaced TypeArray extends TypeKlreplaced {
public List<Byte> getDims() {
return dims;
}
public void setDimentions(List<Byte> dims) {
this.dims = dims;
}
public final Type type;
public final int dimensions;
private List<Byte> dims = new ArrayList<>();
public TypeArray(Type type, int dimensions) {
super("Array", TypeKlreplaced.FINAL);
addParent(TypeList.OBJECT.getName());
setParent(TypeList.OBJECT);
this.type = type;
this.dimensions = dimensions;
}
public String getName() {
String dimString = Stream.generate(() -> "[]").limit(dimensions).collect(Collectors.joining());
return type.getName() + dimString;
}
@Override
protected void exportSymbols() {
SymbolTable.add(new VariableInfo("length", this, TypeList.INT, VariableInfo.PUBLIC | VariableInfo.FINAL));
}
@Override
public boolean equals(Object t) {
if (this == t) {
return true;
}
if (t == null || !(t instanceof TypeArray)) {
return false;
}
if (super.equals(t)) {
// make sure we're compating to an array
try {
TypeArray a = (TypeArray) t;
return a.type.equals(type) && (a.dimensions == dimensions || a.dimensions == -1 || dimensions == -1);
} catch (Exception e) {
}
}
return false;
}
@Override
public int hashCode() {
int hash = 3;
hash = 53 * hash + Objects.hashCode(this.type);
hash = 313 * hash + this.dimensions;
return hash;
}
@Override
public int compareTo(Type t) {
int r = super.compareTo(t);
if (r == 0) {
try {
TypeArray a = (TypeArray) t;
r = type.compareTo(t);
if (r == 0) {
r = dimensions - a.dimensions;
}
} catch (Exception e) {
}
}
return r;
}
public TypeArray produce() {
ArrayList<Type> all = new ArrayList<>(TypeList.getAll());
PseudoRandom.shuffle(all);
for (Type t : all) {
if (t instanceof TypeArray) {
continue;
}
int dims = PseudoRandom.randomNotZero(ProductionParams.dimensionsLimit.value());
return new TypeArray(t, dims);
}
throw new Error("Shouldn't happen");
}
@Override
public <T> T accept(Visitor<T> v) {
return v.visit(this);
}
public Type getType() {
return type;
}
public int getDimensions() {
return dimensions;
}
}
19
Source : TypeArray.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
@Override
public int compareTo(Type t) {
int r = super.compareTo(t);
if (r == 0) {
try {
TypeArray a = (TypeArray) t;
r = type.compareTo(t);
if (r == 0) {
r = dimensions - a.dimensions;
}
} catch (Exception e) {
}
}
return r;
}
19
Source : WhileFactory.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
clreplaced WhileFactory extends SafeFactory<While> {
private final Loop loop;
private final long complexityLimit;
private final int statementLimit;
private final int operatorLimit;
private final TypeKlreplaced ownerClreplaced;
private final int level;
private final Type returnType;
private long thisLoopIterLimit = 0;
private final boolean canHaveReturn;
WhileFactory(TypeKlreplaced ownerClreplaced, Type returnType, long complexityLimit, int statementLimit, int operatorLimit, int level, boolean canHaveReturn) {
this.ownerClreplaced = ownerClreplaced;
this.returnType = returnType;
loop = new Loop();
this.complexityLimit = complexityLimit;
this.statementLimit = statementLimit;
this.operatorLimit = operatorLimit;
this.level = level;
this.canHaveReturn = canHaveReturn;
}
@Override
protected While sproduce() throws ProductionFailedException {
Block emptyBlock = new Block(ownerClreplaced, returnType, new LinkedList<>(), level - 1);
if (statementLimit <= 0 || complexityLimit <= 0) {
throw new ProductionFailedException();
}
long complexity = complexityLimit;
// Loop header parameters
long headerComplLimit = (long) (0.005 * complexity * PseudoRandom.random());
complexity -= headerComplLimit;
int headerStatementLimit = PseudoRandom.randomNotZero((int) (statementLimit / 3.0));
// Loop body parameters
thisLoopIterLimit = (long) (0.0001 * complexity * PseudoRandom.random());
if (thisLoopIterLimit > Integer.MAX_VALUE || thisLoopIterLimit == 0) {
throw new ProductionFailedException();
}
complexity = thisLoopIterLimit > 0 ? complexity / thisLoopIterLimit : 0;
long condComplLimit = (long) (complexity * PseudoRandom.random());
complexity -= condComplLimit;
long body1ComplLimit = (long) (complexity * PseudoRandom.random());
complexity -= body1ComplLimit;
int body1StatementLimit = PseudoRandom.randomNotZero((int) (statementLimit / 4.0));
long body2ComplLimit = (long) (complexity * PseudoRandom.random());
complexity -= body2ComplLimit;
int body2StatementLimit = PseudoRandom.randomNotZero((int) (statementLimit / 4.0));
long body3ComplLimit = complexity;
int body3StatementLimit = PseudoRandom.randomNotZero((int) (statementLimit / 4.0));
// Production
IRNodeBuilder builder = new IRNodeBuilder().setOwnerKlreplaced(ownerClreplaced).setResultType(returnType).setOperatorLimit(operatorLimit);
loop.initialization = builder.getCounterInitializerFactory(0).produce();
Block header;
try {
header = builder.setComplexityLimit(headerComplLimit).setStatementLimit(headerStatementLimit).setLevel(level - 1).setSubBlock(true).setCanHaveBreaks(false).setCanHaveContinues(false).setCanHaveReturn(false).getBlockFactory().produce();
} catch (ProductionFailedException e) {
header = emptyBlock;
}
LocalVariable counter = new LocalVariable(loop.initialization.getVariableInfo());
Literal limiter = new Literal((int) thisLoopIterLimit, TypeList.INT);
loop.condition = builder.setComplexityLimit(condComplLimit).setLocalVariable(counter).getLoopingConditionFactory(limiter).produce();
Block body1;
SymbolTable.push();
try {
body1 = builder.setComplexityLimit(body1ComplLimit).setStatementLimit(body1StatementLimit).setLevel(level).setSubBlock(true).setCanHaveBreaks(true).setCanHaveContinues(false).setCanHaveReturn(canHaveReturn).getBlockFactory().produce();
} catch (ProductionFailedException e) {
body1 = emptyBlock;
}
loop.manipulator = builder.setLocalVariable(counter).getCounterManipulatorFactory().produce();
Block body2;
try {
body2 = builder.setComplexityLimit(body2ComplLimit).setStatementLimit(body2StatementLimit).setLevel(level).setSubBlock(true).setCanHaveBreaks(true).setCanHaveContinues(true).setCanHaveReturn(canHaveReturn).getBlockFactory().produce();
} catch (ProductionFailedException e) {
body2 = emptyBlock;
}
Block body3;
try {
body3 = builder.setComplexityLimit(body3ComplLimit).setStatementLimit(body3StatementLimit).setLevel(level).setSubBlock(true).setCanHaveBreaks(true).setCanHaveContinues(false).setCanHaveReturn(canHaveReturn).getBlockFactory().produce();
} catch (ProductionFailedException e) {
body3 = emptyBlock;
}
SymbolTable.pop();
return new While(level, loop, thisLoopIterLimit, header, body1, body2, body3);
}
}
19
Source : VariableDeclarationFactory.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
clreplaced VariableDeclarationFactory extends Factory<VariableDeclaration> {
private final boolean isStatic;
private final boolean isLocal;
private final TypeKlreplaced ownerClreplaced;
private Type resultType;
VariableDeclarationFactory(TypeKlreplaced ownerClreplaced, boolean isStatic, boolean isLocal, Type resultType) {
this.ownerClreplaced = ownerClreplaced;
this.isStatic = isStatic;
this.isLocal = isLocal;
this.resultType = resultType;
}
@Override
public VariableDeclaration produce() throws ProductionFailedException {
if (resultType.equals(TypeList.VOID)) {
LinkedList<Type> types = new LinkedList<>(TypeList.getAll());
PseudoRandom.shuffle(types);
if (types.isEmpty()) {
throw new ProductionFailedException();
}
resultType = types.getFirst();
}
String resultName = "var_" + SymbolTable.getNextVariableNumber();
int flags = VariableInfo.NONE;
if (isStatic) {
flags |= VariableInfo.STATIC;
}
if (isLocal) {
flags |= VariableInfo.LOCAL;
}
VariableInfo varInfo = new VariableInfo(resultName, ownerClreplaced, resultType, flags);
SymbolTable.add(varInfo);
return new VariableDeclaration(varInfo);
}
}
19
Source : UnaryPlusMinusOperatorFactory.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
@Override
protected boolean isApplicable(Type resultType) {
if (!TypeList.isBuiltIn(resultType) || resultType.equals(TypeList.BOOLEAN)) {
return false;
}
BuiltInType resType = (BuiltInType) resultType;
return resType.equals(TypeList.INT) || resType.isMoreCapaciousThan(TypeList.INT);
}
19
Source : UnaryPlusMinusOperatorFactory.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
@Override
protected UnaryOperator generateProduction(Type type) throws ProductionFailedException {
return new UnaryOperator(opKind, new IRNodeBuilder().setComplexityLimit(complexityLimit).setOperatorLimit(operatorLimit).setOwnerKlreplaced((TypeKlreplaced) ownerClreplaced).setResultType(type).setExceptionSafe(exceptionSafe).setNoConsts(noconsts).getExpressionFactory().produce());
}
19
Source : UnaryOperatorFactory.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
public abstract clreplaced UnaryOperatorFactory extends OperatorFactory<UnaryOperator> {
protected final OperatorKind opKind;
protected final Type resultType;
protected final Type ownerClreplaced;
protected UnaryOperatorFactory(OperatorKind opKind, long complexityLimit, int operatorLimit, Type ownerClreplaced, Type resultType, boolean exceptionSafe, boolean noconsts) {
super(opKind.priority, complexityLimit, operatorLimit, exceptionSafe, noconsts);
this.opKind = opKind;
this.resultType = resultType;
this.ownerClreplaced = ownerClreplaced;
}
protected Type generateType() {
return resultType;
}
protected abstract UnaryOperator generateProduction(Type type) throws ProductionFailedException;
protected abstract boolean isApplicable(Type resultType);
@Override
public UnaryOperator produce() throws ProductionFailedException {
if (!isApplicable(resultType)) {
// avoid implicit use of resultType.toString()
throw new ProductionFailedException("Type " + resultType.getName() + " is not applicable by " + getClreplaced().getName());
}
Type type;
try {
type = generateType();
} catch (Exception ex) {
throw new ProductionFailedException(ex.getMessage());
}
try {
SymbolTable.push();
UnaryOperator result = generateProduction(type);
SymbolTable.merge();
return result;
} catch (ProductionFailedException e) {
SymbolTable.pop();
throw e;
}
}
}
19
Source : UnaryOperatorFactory.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
@Override
public UnaryOperator produce() throws ProductionFailedException {
if (!isApplicable(resultType)) {
// avoid implicit use of resultType.toString()
throw new ProductionFailedException("Type " + resultType.getName() + " is not applicable by " + getClreplaced().getName());
}
Type type;
try {
type = generateType();
} catch (Exception ex) {
throw new ProductionFailedException(ex.getMessage());
}
try {
SymbolTable.push();
UnaryOperator result = generateProduction(type);
SymbolTable.merge();
return result;
} catch (ProductionFailedException e) {
SymbolTable.pop();
throw e;
}
}
19
Source : TryCatchBlockFactory.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
clreplaced TryCatchBlockFactory extends Factory<TryCatchBlock> {
private final static double CATCH_SELECTION_COEF = 0.1d;
private final Type returnType;
private final long complexityLimit;
private final int statementLimit, operatorLimit;
private final boolean subBlock;
private final boolean canHaveBreaks;
private final boolean canHaveContinues;
private final boolean canHaveReturn;
private final int level;
private final TypeKlreplaced ownerClreplaced;
TryCatchBlockFactory(TypeKlreplaced ownerClreplaced, Type returnType, long complexityLimit, int statementLimit, int operatorLimit, int level, boolean subBlock, boolean canHaveBreaks, boolean canHaveContinues, boolean canHaveReturn) {
this.ownerClreplaced = ownerClreplaced;
this.returnType = returnType;
this.complexityLimit = complexityLimit;
this.statementLimit = statementLimit;
this.operatorLimit = operatorLimit;
this.level = level;
this.subBlock = subBlock;
this.canHaveBreaks = canHaveBreaks;
this.canHaveContinues = canHaveContinues;
this.canHaveReturn = canHaveReturn;
}
@Override
public TryCatchBlock produce() throws ProductionFailedException {
if (complexityLimit < 1 || statementLimit < 1) {
throw new ProductionFailedException();
}
List<Type> uncheckedThrowables = getUncheckedThrowables();
IRNodeBuilder builder = new IRNodeBuilder().setOwnerKlreplaced(ownerClreplaced).setResultType(returnType).setOperatorLimit(operatorLimit).setLevel(level).setSubBlock(subBlock).setCanHaveReturn(canHaveReturn).setCanHaveContinues(canHaveContinues).setCanHaveBreaks(canHaveBreaks);
Block body = getBlock(builder, 0.6);
int catchBlocksCount = (int) (CATCH_SELECTION_COEF * PseudoRandom.random() * uncheckedThrowables.size());
List<CatchBlock> catchBlocks = new ArrayList<>();
List<Type> caught = new ArrayList<>();
for (int i = 0; i < catchBlocksCount; i++) {
List<Type> whatToCatch = new ArrayList<>();
int throwableLimit = 1 + (int) ((1 / (2 * CATCH_SELECTION_COEF)) * PseudoRandom.random());
for (int j = 0; j < throwableLimit; j++) {
whatToCatch.add(selectUniqueThrowable(uncheckedThrowables, caught));
}
catchBlocks.add(new CatchBlock(getBlock(builder, 0.3 / catchBlocksCount), whatToCatch, level));
}
Block finallyBody = PseudoRandom.randomBoolean() || catchBlocksCount == 0 ? getBlock(builder, 0.1) : null;
return new TryCatchBlock(body, finallyBody, catchBlocks, level);
}
private Type selectUniqueThrowable(List<Type> variants, List<Type> caught) {
Type selected;
do {
int randomIndex = PseudoRandom.randomNotZero(variants.size()) - 1;
selected = variants.get(randomIndex);
} while (caught.contains(selected));
caught.add(selected);
return selected;
}
private Block getBlock(IRNodeBuilder builder, double weight) throws ProductionFailedException {
long actualComplexityLim = (long) (weight * PseudoRandom.random() * complexityLimit);
int actualStatementLim = (int) (weight * PseudoRandom.random() * statementLimit);
return builder.setStatementLimit(actualStatementLim).setComplexityLimit(actualComplexityLim).getBlockFactory().produce();
}
private List<Type> getUncheckedThrowables() {
List<Type> result = new ArrayList<>();
result.addAll(TypeUtil.getImplicitlyCastable(TypeList.getAll(), new TypeKlreplaced("java.lang.Error")));
result.addAll(TypeUtil.getImplicitlyCastable(TypeList.getAll(), new TypeKlreplaced("java.lang.RuntimeException")));
return result;
}
}
19
Source : TryCatchBlockFactory.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
private Type selectUniqueThrowable(List<Type> variants, List<Type> caught) {
Type selected;
do {
int randomIndex = PseudoRandom.randomNotZero(variants.size()) - 1;
selected = variants.get(randomIndex);
} while (caught.contains(selected));
caught.add(selected);
return selected;
}
19
Source : TernaryOperatorFactory.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
clreplaced TernaryOperatorFactory extends OperatorFactory<TernaryOperator> {
private final Type resultType;
private final TypeKlreplaced ownerClreplaced;
TernaryOperatorFactory(long complexityLimit, int operatorLimit, TypeKlreplaced ownerClreplaced, Type resultType, boolean exceptionSafe, boolean noconsts) {
super(2, complexityLimit, operatorLimit, exceptionSafe, noconsts);
this.resultType = resultType;
this.ownerClreplaced = ownerClreplaced;
}
private TernaryOperator generateProduction() throws ProductionFailedException {
int leftOpLimit = (int) (PseudoRandom.random() * 0.3 * (operatorLimit - 1));
int rightOpLimit = (int) (PseudoRandom.random() * 0.3 * (operatorLimit - 1));
int condOpLimit = operatorLimit - 1 - leftOpLimit - rightOpLimit;
long leftComplLimit = (long) (PseudoRandom.random() * 0.3 * (complexityLimit - 1));
long rightComplLimit = (long) (PseudoRandom.random() * 0.3 * (complexityLimit - 1));
long condComplLimit = complexityLimit - 1 - leftComplLimit - rightComplLimit;
if (leftComplLimit == 0 || rightComplLimit == 0 || condComplLimit == 0 || leftOpLimit == 0 || rightOpLimit == 0 || condOpLimit == 0) {
throw new ProductionFailedException();
}
IRNodeBuilder builder = new IRNodeBuilder().setOwnerKlreplaced(ownerClreplaced).setExceptionSafe(exceptionSafe);
IRNode conditionalExp = builder.setComplexityLimit(condComplLimit).setOperatorLimit(condOpLimit).setResultType(TypeList.BOOLEAN).setNoConsts(noconsts).getExpressionFactory().produce();
// Ignore initializations performed in left and right branches:
IRNode leftExp;
SymbolTable.push();
try {
leftExp = builder.setComplexityLimit(leftComplLimit).setOperatorLimit(leftOpLimit).setResultType(resultType).setNoConsts(false).getExpressionFactory().produce();
} finally {
SymbolTable.pop();
}
IRNode rightExp;
SymbolTable.push();
try {
rightExp = builder.setComplexityLimit(rightComplLimit).setOperatorLimit(rightOpLimit).setResultType(resultType).setNoConsts(false).getExpressionFactory().produce();
} finally {
SymbolTable.pop();
}
return new TernaryOperator(conditionalExp, leftExp, rightExp);
}
@Override
public TernaryOperator produce() throws ProductionFailedException {
try {
SymbolTable.push();
TernaryOperator result = generateProduction();
SymbolTable.merge();
return result;
} catch (ProductionFailedException e) {
SymbolTable.pop();
throw e;
}
}
}
19
Source : StaticMemberVariableFactory.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
clreplaced StaticMemberVariableFactory extends Factory<StaticMemberVariable> {
private final Type type;
private final int flags;
private final Type ownerClreplaced;
StaticMemberVariableFactory(TypeKlreplaced ownerClreplaced, Type type, int flags) {
this.ownerClreplaced = ownerClreplaced;
this.type = type;
this.flags = flags;
}
@Override
public StaticMemberVariable produce() throws ProductionFailedException {
// Get the variables of the requested type from SymbolTable
ArrayList<Symbol> variables = new ArrayList<>(SymbolTable.get(type, VariableInfo.clreplaced));
if (!variables.isEmpty()) {
PseudoRandom.shuffle(variables);
for (Symbol symbol : variables) {
VariableInfo varInfo = (VariableInfo) symbol;
if ((varInfo.flags & VariableInfo.FINAL) == (flags & VariableInfo.FINAL) && (varInfo.flags & VariableInfo.INITIALIZED) == (flags & VariableInfo.INITIALIZED) && (varInfo.flags & VariableInfo.STATIC) > 0) {
return new StaticMemberVariable((TypeKlreplaced) ownerClreplaced, varInfo);
}
}
}
throw new ProductionFailedException();
}
}
19
Source : ReturnFactory.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
clreplaced ReturnFactory extends SafeFactory<Return> {
private final long complexityLimit;
private final int operatorLimit;
private final Type resultType;
private final boolean exceptionSafe;
private final TypeKlreplaced ownerClreplaced;
ReturnFactory(long compLimit, int opLimit, TypeKlreplaced ownerClreplaced, Type resultType, boolean exceptionSafe) {
this.complexityLimit = compLimit;
this.operatorLimit = opLimit;
this.resultType = resultType;
this.ownerClreplaced = ownerClreplaced;
this.exceptionSafe = exceptionSafe;
}
@Override
protected Return sproduce() throws ProductionFailedException {
return new Return(new IRNodeBuilder().setComplexityLimit(complexityLimit - 1).setOperatorLimit(operatorLimit - 1).setOwnerKlreplaced(ownerClreplaced).setResultType(resultType).setExceptionSafe(exceptionSafe).setNoConsts(false).getLimitedExpressionFactory().produce());
}
}
19
Source : NonStaticMemberVariableFactory.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
clreplaced NonStaticMemberVariableFactory extends Factory<NonStaticMemberVariable> {
private final Type type;
private final int flags;
private final long complexityLimit;
private final int operatorLimit;
private final boolean exceptionSafe;
private final Type ownerClreplaced;
NonStaticMemberVariableFactory(long complexityLimit, int operatorLimit, TypeKlreplaced ownerClreplaced, Type type, int flags, boolean exceptionSafe) {
this.ownerClreplaced = ownerClreplaced;
this.type = type;
this.flags = flags;
this.complexityLimit = complexityLimit;
this.operatorLimit = operatorLimit;
this.exceptionSafe = exceptionSafe;
}
@Override
public NonStaticMemberVariable produce() throws ProductionFailedException {
// Get the variables of the requested type from SymbolTable
ArrayList<Symbol> variables = new ArrayList<>(SymbolTable.get(type, VariableInfo.clreplaced));
if (!variables.isEmpty()) {
PseudoRandom.shuffle(variables);
IRNodeBuilder builder = new IRNodeBuilder().setComplexityLimit(complexityLimit).setOperatorLimit(operatorLimit).setOwnerKlreplaced((TypeKlreplaced) ownerClreplaced).setExceptionSafe(exceptionSafe).setNoConsts(false);
for (Symbol symbol : variables) {
VariableInfo varInfo = (VariableInfo) symbol;
if ((varInfo.flags & VariableInfo.FINAL) == (flags & VariableInfo.FINAL) && (varInfo.flags & VariableInfo.INITIALIZED) == (flags & VariableInfo.INITIALIZED) && (varInfo.flags & VariableInfo.STATIC) == 0 && (varInfo.flags & VariableInfo.LOCAL) == 0) {
try {
IRNode object = builder.setResultType(varInfo.owner).getExpressionFactory().produce();
return new NonStaticMemberVariable(object, varInfo);
} catch (ProductionFailedException e) {
}
}
}
}
throw new ProductionFailedException();
}
}
19
Source : LogicalInversionOperatorFactory.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
@Override
protected UnaryOperator generateProduction(Type resultType) throws ProductionFailedException {
return new UnaryOperator(opKind, new ExpressionFactory(complexityLimit - 1, operatorLimit - 1, (TypeKlreplaced) ownerClreplaced, resultType, exceptionSafe, noconsts).produce());
}
19
Source : LogicalInversionOperatorFactory.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
@Override
protected boolean isApplicable(Type resultType) {
return resultType.equals(TypeList.BOOLEAN);
}
19
Source : LocalVariableFactory.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
clreplaced LocalVariableFactory extends Factory<LocalVariable> {
private final Type type;
private final int flags;
LocalVariableFactory(Type type, int flags) {
this.type = type;
this.flags = flags;
}
@Override
public LocalVariable produce() throws ProductionFailedException {
// Get the variables of the requested type from SymbolTable
ArrayList<Symbol> allVariables = new ArrayList<>(SymbolTable.get(type, VariableInfo.clreplaced));
if (!allVariables.isEmpty()) {
PseudoRandom.shuffle(allVariables);
for (Symbol symbol : allVariables) {
VariableInfo varInfo = (VariableInfo) symbol;
if ((varInfo.flags & VariableInfo.FINAL) == (flags & VariableInfo.FINAL) && (varInfo.flags & VariableInfo.INITIALIZED) == (flags & VariableInfo.INITIALIZED) && (varInfo.flags & VariableInfo.LOCAL) > 0) {
return new LocalVariable(varInfo);
}
}
}
throw new ProductionFailedException();
}
}
19
Source : LiteralFactory.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
clreplaced LiteralFactory extends Factory<Literal> {
protected final Type resultType;
LiteralFactory(Type resultType) {
this.resultType = resultType;
}
@Override
public Literal produce() throws ProductionFailedException {
Literal literal;
if (resultType.equals(TypeList.BOOLEAN)) {
literal = new Literal(PseudoRandom.randomBoolean(), TypeList.BOOLEAN);
} else if (resultType.equals(TypeList.CHAR)) {
literal = new Literal((char) ((char) (PseudoRandom.random() * ('z' - 'A')) + 'A'), TypeList.CHAR);
} else if (resultType.equals(TypeList.INT)) {
literal = new Literal((int) (PseudoRandom.random() * Integer.MAX_VALUE), TypeList.INT);
} else if (resultType.equals(TypeList.LONG)) {
literal = new Literal((long) (PseudoRandom.random() * Long.MAX_VALUE), TypeList.LONG);
} else if (resultType.equals(TypeList.FLOAT)) {
literal = new Literal(Float.valueOf(String.format((Locale) null, "%." + ProductionParams.floatingPointPrecision.value() + "EF", (float) PseudoRandom.random() * Float.MAX_VALUE)), TypeList.FLOAT);
} else if (resultType.equals(TypeList.DOUBLE)) {
literal = new Literal(Double.valueOf(String.format((Locale) null, "%." + 2 * ProductionParams.floatingPointPrecision.value() + "E", PseudoRandom.random() * Double.MAX_VALUE)), TypeList.DOUBLE);
} else if (resultType.equals(TypeList.BYTE)) {
literal = new Literal((byte) (PseudoRandom.random() * Byte.MAX_VALUE), TypeList.BYTE);
} else if (resultType.equals(TypeList.SHORT)) {
literal = new Literal((short) (PseudoRandom.random() * Short.MAX_VALUE), TypeList.SHORT);
} else if (resultType.equals(TypeList.STRING)) {
int size = (int) (PseudoRandom.random() * ProductionParams.stringLiteralSizeLimit.value());
byte[] str = new byte[size];
for (int i = 0; i < size; i++) {
str[i] = (byte) ((int) (('z' - 'a') * PseudoRandom.random()) + 'a');
}
literal = new Literal(new String(str), TypeList.STRING);
} else {
throw new ProductionFailedException();
}
return literal;
}
}
19
Source : IRNodeBuilder.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
public IRNodeBuilder setResultType(Type value) {
resultType = Optional.of(value);
return this;
}
19
Source : IncDecOperatorFactory.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
@Override
protected UnaryOperator generateProduction(Type l) throws ProductionFailedException {
return new UnaryOperator(opKind, new IRNodeBuilder().setComplexityLimit(complexityLimit - 1).setOperatorLimit(operatorLimit - 1).setOwnerKlreplaced((TypeKlreplaced) ownerClreplaced).setResultType(l).setIsConstant(false).setIsInitialized(true).setExceptionSafe(exceptionSafe).setNoConsts(exceptionSafe).getVariableFactory().produce());
}
19
Source : IncDecOperatorFactory.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
@Override
protected boolean isApplicable(Type resultType) {
return TypeList.isBuiltInInt(resultType) && !resultType.equals(TypeList.BOOLEAN);
}
19
Source : IfFactory.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
clreplaced IfFactory extends SafeFactory<If> {
protected final long complexityLimit;
protected final int statementLimit;
protected final int operatorLimit;
protected final boolean canHaveBreaks;
protected final boolean canHaveContinues;
protected final boolean canHaveReturn;
protected final TypeKlreplaced ownerClreplaced;
protected final Type returnType;
protected final int level;
IfFactory(TypeKlreplaced ownerClreplaced, Type returnType, long complexityLimit, int statementLimit, int operatorLimit, int level, boolean canHaveBreaks, boolean canHaveContinues, boolean canHaveReturn) {
this.ownerClreplaced = ownerClreplaced;
this.returnType = returnType;
this.complexityLimit = complexityLimit;
this.statementLimit = statementLimit;
this.operatorLimit = operatorLimit;
this.level = level;
this.canHaveBreaks = canHaveBreaks;
this.canHaveContinues = canHaveContinues;
this.canHaveReturn = canHaveReturn;
}
@Override
public If sproduce() throws ProductionFailedException {
// resizeUpChildren(If.IfPart.values().length);
if (statementLimit > 0 && complexityLimit > 0) {
long conditionComplLimit = (long) (0.01 * PseudoRandom.random() * (complexityLimit - 1));
IRNodeBuilder builder = new IRNodeBuilder().setOwnerKlreplaced(ownerClreplaced).setOperatorLimit(operatorLimit);
IRNode condition = builder.setComplexityLimit(conditionComplLimit).setResultType(TypeList.BOOLEAN).setExceptionSafe(false).setNoConsts(false).getLimitedExpressionFactory().produce();
// setChild(If.IfPart.CONDITION.ordinal(), condition);
long remainder = complexityLimit - 1 - condition.complexity();
long ifBlockComplLimit = (long) (PseudoRandom.random() * remainder);
long elseBlockComplLimit = remainder - ifBlockComplLimit;
int ifBlockLimit = (int) (PseudoRandom.random() * statementLimit);
int elseBlockLimit = statementLimit - ifBlockLimit;
If.IfPart controlDeviation;
if (ifBlockLimit > 0 && elseBlockLimit <= 0) {
controlDeviation = If.IfPart.THEN;
} else {
controlDeviation = PseudoRandom.randomBoolean() ? If.IfPart.THEN : If.IfPart.ELSE;
}
if (ifBlockLimit > 0 && ifBlockComplLimit > 0) {
Block thenBlock;
builder.setResultType(returnType).setLevel(level).setComplexityLimit(ifBlockComplLimit).setStatementLimit(ifBlockLimit);
if (controlDeviation == If.IfPart.THEN) {
thenBlock = builder.setSubBlock(false).setCanHaveBreaks(canHaveBreaks).setCanHaveContinues(canHaveContinues).setCanHaveReturn(canHaveReturn).getBlockFactory().produce();
} else {
thenBlock = builder.setSubBlock(false).setCanHaveBreaks(false).setCanHaveContinues(false).setCanHaveReturn(false).getBlockFactory().produce();
}
// setChild(If.IfPart.THEN.ordinal(), thenBlock);
Block elseBlock = null;
if (elseBlockLimit > 0 && elseBlockComplLimit > 0) {
builder.setComplexityLimit(elseBlockComplLimit).setStatementLimit(elseBlockLimit);
if (controlDeviation == If.IfPart.ELSE) {
elseBlock = builder.setSubBlock(false).setCanHaveBreaks(canHaveBreaks).setCanHaveContinues(canHaveContinues).setCanHaveReturn(canHaveReturn).getBlockFactory().produce();
} else {
elseBlock = builder.setSubBlock(false).setCanHaveBreaks(false).setCanHaveContinues(false).setCanHaveReturn(false).getBlockFactory().produce();
}
}
// setChild(If.IfPart.ELSE.ordinal(), elseBlock);
return new If(condition, thenBlock, elseBlock, level);
}
}
throw new ProductionFailedException();
}
}
19
Source : FunctionDefinitionFactory.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
clreplaced FunctionDefinitionFactory extends Factory<FunctionDefinition> {
private final Type resultType;
private final String name;
private final long complexityLimit;
private final int statementLimit;
private final int operatorLimit;
private final int memberFunctionsArgLimit;
private final int flags;
private final int level;
private final TypeKlreplaced ownerClreplaced;
FunctionDefinitionFactory(String name, TypeKlreplaced ownerClreplaced, Type resultType, long complexityLimit, int statementLimit, int operatorLimit, int memberFunctionsArgLimit, int level, int flags) {
this.name = name;
this.ownerClreplaced = ownerClreplaced;
this.resultType = resultType;
this.complexityLimit = complexityLimit;
this.statementLimit = statementLimit;
this.operatorLimit = operatorLimit;
this.memberFunctionsArgLimit = memberFunctionsArgLimit;
this.level = level;
this.flags = flags;
}
@Override
public FunctionDefinition produce() throws ProductionFailedException {
Type resType = resultType;
if (resType == null) {
List<Type> types = new ArrayList<>(TypeList.getAll());
types.add(TypeList.VOID);
resType = PseudoRandom.randomElement(types);
}
int argNumber = (int) (PseudoRandom.random() * memberFunctionsArgLimit);
ArrayList<VariableInfo> argumentsInfo;
if ((flags & FunctionInfo.STATIC) > 0) {
argumentsInfo = new ArrayList<>(argNumber);
} else {
argumentsInfo = new ArrayList<>(argNumber + 1);
argumentsInfo.add(new VariableInfo("this", ownerClreplaced, ownerClreplaced, VariableInfo.FINAL | VariableInfo.LOCAL | VariableInfo.INITIALIZED));
}
ArrayList<ArgumentDeclaration> argumentsDeclaration = new ArrayList<>(argNumber);
SymbolTable.push();
IRNode body;
Return returnNode;
FunctionInfo functionInfo;
try {
IRNodeBuilder builder = new IRNodeBuilder().setArgumentType(ownerClreplaced);
int i = 0;
for (; i < argNumber; i++) {
ArgumentDeclaration d = builder.setVariableNumber(i).getArgumentDeclarationFactory().produce();
argumentsDeclaration.add(d);
argumentsInfo.add(d.variableInfo);
}
Collection<Symbol> thisKlreplacedFuncs = SymbolTable.getAllCombined(ownerClreplaced, FunctionInfo.clreplaced);
Collection<Symbol> parentFuncs = FunctionDefinition.getFuncsFromParents(ownerClreplaced);
while (true) {
functionInfo = new FunctionInfo(name, ownerClreplaced, resType, 0, flags, argumentsInfo);
if (thisKlreplacedFuncs.contains(functionInfo) || FunctionDefinition.isInvalidOverride(functionInfo, parentFuncs)) {
// try changing the signature, and go checking again.
ArgumentDeclaration argDecl = builder.setVariableNumber(i++).getArgumentDeclarationFactory().produce();
argumentsDeclaration.add(argDecl);
argumentsInfo.add(argDecl.variableInfo);
} else {
break;
}
}
long blockComplLimit = (long) (PseudoRandom.random() * complexityLimit);
body = builder.setOwnerKlreplaced(ownerClreplaced).setResultType(resType).setComplexityLimit(blockComplLimit).setStatementLimit(statementLimit).setOperatorLimit(operatorLimit).setLevel(level).setSubBlock(true).setCanHaveBreaks(false).setCanHaveContinues(false).setCanHaveReturn(true).getBlockFactory().produce();
if (!resType.equals(TypeList.VOID)) {
returnNode = builder.setComplexityLimit(complexityLimit - blockComplLimit).setExceptionSafe(false).getReturnFactory().produce();
} else {
returnNode = new Return(new Nothing());
}
} finally {
SymbolTable.pop();
}
// addChildren(argumentsDeclaration); // not neccessary while complexity() doesn't use it
functionInfo = new FunctionInfo(name, ownerClreplaced, resType, body == null ? 0 : body.complexity(), flags, argumentsInfo);
// If it's all ok, add the function to the symbol table.
SymbolTable.add(functionInfo);
return new FunctionDefinition(functionInfo, argumentsDeclaration, body, returnNode);
}
}
19
Source : FunctionDeclarationFactory.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
clreplaced FunctionDeclarationFactory extends Factory<FunctionDeclaration> {
private final Type resultType;
private final TypeKlreplaced ownerClreplaced;
private final String name;
private final int memberFunctionsArgLimit;
private final int flags;
FunctionDeclarationFactory(String name, TypeKlreplaced ownerClreplaced, Type resultType, int memberFunctionsArgLimit, int flags) {
this.name = name;
this.ownerClreplaced = ownerClreplaced;
this.resultType = resultType;
this.memberFunctionsArgLimit = memberFunctionsArgLimit;
this.flags = flags;
}
@Override
public FunctionDeclaration produce() throws ProductionFailedException {
Type resType = resultType;
if (resType == null) {
List<Type> types = new ArrayList<>(TypeList.getAll());
types.add(TypeList.VOID);
resType = PseudoRandom.randomElement(types);
}
int argNumber = (int) (PseudoRandom.random() * memberFunctionsArgLimit);
ArrayList<VariableInfo> argumentsInfo = new ArrayList<>(argNumber + 1);
argumentsInfo.add(new VariableInfo("this", ownerClreplaced, ownerClreplaced, VariableInfo.FINAL | VariableInfo.LOCAL | VariableInfo.INITIALIZED));
ArrayList<ArgumentDeclaration> argumentsDeclaration = new ArrayList<>(argNumber);
SymbolTable.push();
FunctionInfo functionInfo;
IRNodeBuilder builder = new IRNodeBuilder().setArgumentType(ownerClreplaced);
try {
int i = 0;
for (; i < argNumber; i++) {
ArgumentDeclaration d = builder.setVariableNumber(i).getArgumentDeclarationFactory().produce();
argumentsDeclaration.add(d);
argumentsInfo.add(d.variableInfo);
}
Collection<Symbol> thisKlreplacedFuncs = SymbolTable.getAllCombined(ownerClreplaced, FunctionInfo.clreplaced);
Collection<Symbol> parentFuncs = FunctionDefinition.getFuncsFromParents(ownerClreplaced);
while (true) {
functionInfo = new FunctionInfo(name, ownerClreplaced, resType, 0, flags, argumentsInfo);
if (thisKlreplacedFuncs.contains(functionInfo) || FunctionDefinition.isInvalidOverride(functionInfo, parentFuncs)) {
// try changing the signature, and go checking again.
ArgumentDeclaration d = builder.setVariableNumber(i++).getArgumentDeclarationFactory().produce();
argumentsDeclaration.add(d);
argumentsInfo.add(d.variableInfo);
} else {
break;
}
}
} finally {
SymbolTable.pop();
}
// addChildren(argumentsDeclaration); // not neccessary while complexity is 0
functionInfo = new FunctionInfo(name, ownerClreplaced, resType, 0, flags, argumentsInfo);
// If it's all ok, add the function to the symbol table.
SymbolTable.add(functionInfo);
return new FunctionDeclaration(functionInfo, argumentsDeclaration);
}
}
19
Source : ForFactory.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
clreplaced ForFactory extends SafeFactory<For> {
private final Loop loop;
private final long complexityLimit;
private final int statementLimit;
private final int operatorLimit;
private final TypeKlreplaced ownerClreplaced;
private final Type returnType;
private final int level;
private final boolean canHaveReturn;
ForFactory(TypeKlreplaced ownerClreplaced, Type returnType, long complexityLimit, int statementLimit, int operatorLimit, int level, boolean canHaveReturn) {
this.ownerClreplaced = ownerClreplaced;
this.returnType = returnType;
this.complexityLimit = complexityLimit;
this.statementLimit = statementLimit;
this.operatorLimit = operatorLimit;
this.level = level;
loop = new Loop();
this.canHaveReturn = canHaveReturn;
}
@Override
protected For sproduce() throws ProductionFailedException {
Block emptyBlock = new Block(ownerClreplaced, returnType, new LinkedList<>(), level - 1);
if (statementLimit <= 0 || complexityLimit <= 0) {
throw new ProductionFailedException();
}
IRNodeBuilder builder = new IRNodeBuilder().setOwnerKlreplaced(ownerClreplaced).setResultType(returnType).setOperatorLimit(operatorLimit).setSemicolon(false).setExceptionSafe(false).setNoConsts(false);
long complexity = complexityLimit;
// Loop header parameters
long headerComplLimit = (long) (0.005 * complexity * PseudoRandom.random());
complexity -= headerComplLimit;
int headerStatementLimit = PseudoRandom.randomNotZero((int) (statementLimit / 4.0));
long statement1ComplLimit = (long) (0.005 * complexity * PseudoRandom.random());
complexity -= statement1ComplLimit;
// Loop body parameters
long thisLoopIterLimit = (long) (0.0001 * complexity * PseudoRandom.random());
if (thisLoopIterLimit > Integer.MAX_VALUE || thisLoopIterLimit == 0) {
throw new ProductionFailedException();
}
complexity = thisLoopIterLimit > 0 ? complexity / thisLoopIterLimit : 0;
long condComplLimit = (long) (complexity * PseudoRandom.random());
complexity -= condComplLimit;
long statement2ComplLimit = (long) (complexity * PseudoRandom.random());
complexity -= statement2ComplLimit;
long body1ComplLimit = (long) (complexity * PseudoRandom.random());
complexity -= body1ComplLimit;
int body1StatementLimit = PseudoRandom.randomNotZero((int) (statementLimit / 4.0));
long body2ComplLimit = (long) (complexity * PseudoRandom.random());
complexity -= body2ComplLimit;
int body2StatementLimit = PseudoRandom.randomNotZero((int) (statementLimit / 4.0));
long body3ComplLimit = complexity;
int body3StatementLimit = PseudoRandom.randomNotZero((int) (statementLimit / 4.0));
// Production
loop.initialization = builder.getCounterInitializerFactory(0).produce();
Block header;
try {
header = builder.setComplexityLimit(headerComplLimit).setStatementLimit(headerStatementLimit).setLevel(level - 1).setSubBlock(true).setCanHaveBreaks(false).setCanHaveContinues(false).setCanHaveReturn(false).getBlockFactory().produce();
} catch (ProductionFailedException e) {
header = emptyBlock;
}
SymbolTable.push();
IRNode statement1;
try {
Rule<IRNode> rule = new Rule<>("statement1");
builder.setComplexityLimit(statement1ComplLimit);
rule.add("replacedignment", builder.getreplacedignmentOperatorFactory());
rule.add("function", builder.getFunctionFactory(), 0.1);
rule.add("initialization", builder.setIsConstant(false).setIsStatic(false).setIsLocal(true).getVariableInitializationFactory());
statement1 = rule.produce();
} catch (ProductionFailedException e) {
statement1 = new Nothing();
}
LocalVariable counter = new LocalVariable(loop.initialization.getVariableInfo());
Literal limiter = new Literal((int) thisLoopIterLimit, TypeList.INT);
loop.condition = builder.setComplexityLimit(condComplLimit).setLocalVariable(counter).getLoopingConditionFactory(limiter).produce();
IRNode statement2;
try {
statement2 = builder.setComplexityLimit(statement2ComplLimit).getreplacedignmentOperatorFactory().produce();
} catch (ProductionFailedException e) {
statement2 = new Nothing();
}
Block body1;
try {
body1 = builder.setComplexityLimit(body1ComplLimit).setStatementLimit(body1StatementLimit).setLevel(level).setSubBlock(true).setCanHaveBreaks(true).setCanHaveContinues(false).setCanHaveReturn(false).getBlockFactory().produce();
} catch (ProductionFailedException e) {
body1 = emptyBlock;
}
loop.manipulator = builder.setLocalVariable(counter).getCounterManipulatorFactory().produce();
Block body2;
try {
body2 = builder.setComplexityLimit(body2ComplLimit).setStatementLimit(body2StatementLimit).setLevel(level).setSubBlock(true).setCanHaveBreaks(true).setCanHaveContinues(true).setCanHaveReturn(false).getBlockFactory().produce();
} catch (ProductionFailedException e) {
body2 = emptyBlock;
}
Block body3;
try {
body3 = builder.setComplexityLimit(body3ComplLimit).setStatementLimit(body3StatementLimit).setLevel(level).setSubBlock(true).setCanHaveBreaks(true).setCanHaveContinues(false).setCanHaveReturn(canHaveReturn).getBlockFactory().produce();
} catch (ProductionFailedException e) {
body3 = emptyBlock;
}
SymbolTable.pop();
return new For(level, loop, thisLoopIterLimit, header, new Statement(statement1, false), new Statement(statement2, false), body1, body2, body3);
}
}
19
Source : DoWhileFactory.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
clreplaced DoWhileFactory extends SafeFactory<DoWhile> {
private final Loop loop;
private final long complexityLimit;
private final int statementLimit;
private final int operatorLimit;
private boolean canHaveReturn = false;
private final TypeKlreplaced ownerClreplaced;
private final int level;
private final Type returnType;
private long thisLoopIterLimit;
DoWhileFactory(TypeKlreplaced ownerClreplaced, Type returnType, long complexityLimit, int statementLimit, int operatorLimit, int level, boolean canHaveReturn) {
loop = new Loop();
this.ownerClreplaced = ownerClreplaced;
this.returnType = returnType;
this.complexityLimit = complexityLimit;
this.statementLimit = statementLimit;
this.operatorLimit = operatorLimit;
this.level = level;
this.canHaveReturn = canHaveReturn;
thisLoopIterLimit = 0;
}
@Override
protected DoWhile sproduce() throws ProductionFailedException {
Block emptyBlock = new Block(ownerClreplaced, returnType, new LinkedList<>(), level - 1);
if (statementLimit > 0 && complexityLimit > 0) {
long complexity = complexityLimit;
// Loop header parameters
long headerComplLimit = (long) (0.005 * complexity * PseudoRandom.random());
complexity -= headerComplLimit;
int headerStatementLimit = PseudoRandom.randomNotZero((int) (statementLimit / 3.0));
// Loop body parameters
thisLoopIterLimit = (long) (0.0001 * complexity * PseudoRandom.random());
if (thisLoopIterLimit > Integer.MAX_VALUE || thisLoopIterLimit == 0) {
throw new ProductionFailedException();
}
complexity = thisLoopIterLimit > 0 ? complexity / thisLoopIterLimit : 0;
long condComplLimit = (long) (complexity * PseudoRandom.random());
complexity -= condComplLimit;
long body1ComplLimit = (long) (complexity * PseudoRandom.random());
complexity -= body1ComplLimit;
int body1StatementLimit = PseudoRandom.randomNotZero((int) (statementLimit / 3.0));
long body2ComplLimit = (long) (complexity * PseudoRandom.random());
complexity -= body2ComplLimit;
int body2StatementLimit = PseudoRandom.randomNotZero((int) (statementLimit / 3.0));
// Production
IRNodeBuilder builder = new IRNodeBuilder().setOwnerKlreplaced(ownerClreplaced).setResultType(returnType).setOperatorLimit(operatorLimit);
loop.initialization = builder.getCounterInitializerFactory(0).produce();
Block header;
try {
header = builder.setComplexityLimit(headerComplLimit).setStatementLimit(headerStatementLimit).setLevel(level - 1).setSubBlock(true).setCanHaveBreaks(false).setCanHaveContinues(false).setCanHaveReturn(false).getBlockFactory().produce();
} catch (ProductionFailedException e) {
header = emptyBlock;
}
// getChildren().set(DoWhile.DoWhilePart.HEADER.ordinal(), header);
LocalVariable counter = new LocalVariable(loop.initialization.getVariableInfo());
Literal limiter = new Literal((int) thisLoopIterLimit, TypeList.INT);
loop.condition = builder.setComplexityLimit(condComplLimit).setLocalVariable(counter).getLoopingConditionFactory(limiter).produce();
SymbolTable.push();
Block body1;
try {
body1 = builder.setComplexityLimit(body1ComplLimit).setStatementLimit(body1StatementLimit).setLevel(level).setSubBlock(true).setCanHaveBreaks(true).setCanHaveContinues(false).setCanHaveReturn(false).getBlockFactory().produce();
} catch (ProductionFailedException e) {
body1 = emptyBlock;
}
// getChildren().set(DoWhile.DoWhilePart.BODY1.ordinal(), body1);
loop.manipulator = builder.setLocalVariable(counter).getCounterManipulatorFactory().produce();
Block body2;
try {
body2 = builder.setComplexityLimit(body2ComplLimit).setStatementLimit(body2StatementLimit).setLevel(level).setSubBlock(true).setCanHaveBreaks(true).setCanHaveContinues(false).setCanHaveReturn(canHaveReturn).getBlockFactory().produce();
} catch (ProductionFailedException e) {
body2 = emptyBlock;
}
// getChildren().set(DoWhile.DoWhilePart.BODY2.ordinal(), body2);
SymbolTable.pop();
return new DoWhile(level, loop, thisLoopIterLimit, header, body1, body2);
}
throw new ProductionFailedException();
}
}
19
Source : CompoundShiftAssignmentOperatorFactory.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
@Override
protected BinaryOperator generateProduction(Type leftType, Type rightType) throws ProductionFailedException {
long leftComplexityLimit = (long) (PseudoRandom.random() * complexityLimit);
long rightComplexityLimit = complexityLimit - leftComplexityLimit;
int leftOperatorLimit = (int) (PseudoRandom.random() * operatorLimit);
int rightOperatorLimit = operatorLimit = leftOperatorLimit;
IRNodeBuilder builder = new IRNodeBuilder().setOwnerKlreplaced((TypeKlreplaced) ownerClreplaced).setExceptionSafe(exceptionSafe).setNoConsts(noconsts);
IRNode leftExpr = builder.setComplexityLimit(leftComplexityLimit).setOperatorLimit(leftOperatorLimit).setResultType(leftType).setIsConstant(false).setIsInitialized(true).getVariableFactory().produce();
IRNode rightExpr = builder.setComplexityLimit(rightComplexityLimit).setOperatorLimit(rightOperatorLimit).setResultType(rightType).getExpressionFactory().produce();
return new BinaryOperator(opKind, resultType, leftExpr, rightExpr);
}
19
Source : CompoundBitwiseAssignmentOperatorFactory.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
@Override
protected boolean isApplicable(Type resultType) {
return TypeList.isBuiltInInt(resultType);
}
19
Source : CompoundArithmeticAssignmentOperatorFactory.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
@Override
protected boolean isApplicable(Type resultType) {
return TypeList.isBuiltIn(resultType) && !resultType.equals(TypeList.BOOLEAN);
}
19
Source : CompoundArithmeticAssignmentOperatorFactory.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
@Override
protected BinaryOperator generateProduction(Type leftType, Type rightType) throws ProductionFailedException {
long leftComplexityLimit = (long) (PseudoRandom.random() * complexityLimit);
long rightComplexityLimit = complexityLimit - leftComplexityLimit;
int leftOperatorLimit = (int) (PseudoRandom.random() * operatorLimit);
int rightOperatorLimit = operatorLimit = leftOperatorLimit;
IRNodeBuilder builder = new IRNodeBuilder().setOwnerKlreplaced((TypeKlreplaced) ownerClreplaced).setExceptionSafe(exceptionSafe).setNoConsts(noconsts);
IRNode rightExpr = builder.setComplexityLimit(rightComplexityLimit).setOperatorLimit(rightOperatorLimit).setResultType(rightType).getExpressionFactory().produce();
VariableBase leftExpr = builder.setComplexityLimit(leftComplexityLimit).setOperatorLimit(leftOperatorLimit).setResultType(leftType).setIsConstant(false).setIsInitialized(true).getVariableFactory().produce();
return new BinaryOperator(opKind, resultType, leftExpr, rightExpr);
}
19
Source : CastOperatorFactory.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
clreplaced CastOperatorFactory extends OperatorFactory<CastOperator> {
private final Type resultType;
private final Type ownerClreplaced;
CastOperatorFactory(long complexityLimit, int operatorLimit, TypeKlreplaced ownerClreplaced, Type resultType, boolean exceptionSafe, boolean noconsts) {
super(13, complexityLimit, operatorLimit, exceptionSafe, noconsts);
this.resultType = resultType;
this.ownerClreplaced = ownerClreplaced;
}
@Override
public CastOperator produce() throws ProductionFailedException {
ArrayList<Type> argType = new ArrayList<>(TypeList.getAll());
PseudoRandom.shuffle(argType);
for (Type type : argType) {
try {
Factory<IRNode> expressionFactory = new IRNodeBuilder().setComplexityLimit(complexityLimit - 1).setOperatorLimit(operatorLimit - 1).setOwnerKlreplaced((TypeKlreplaced) ownerClreplaced).setExceptionSafe(exceptionSafe).setNoConsts(noconsts).setResultType(type).getExpressionFactory();
SymbolTable.push();
if (type.equals(resultType) || ((!exceptionSafe || exceptionSafe && !(type instanceof TypeKlreplaced)) && type.canExplicitlyCastTo(resultType))) {
// In safe mode we cannot explicitly cast an object, because it may throw.
CastOperator castOperator = new CastOperator(resultType, expressionFactory.produce());
SymbolTable.merge();
return castOperator;
}
throw new ProductionFailedException();
} catch (ProductionFailedException e) {
SymbolTable.pop();
}
}
throw new ProductionFailedException();
}
}
19
Source : BlockFactory.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
clreplaced BlockFactory extends Factory<Block> {
private final Type returnType;
private final long complexityLimit;
private final int statementLimit;
private final int operatorLimit;
private final boolean subBlock;
private final boolean canHaveBreaks;
private final boolean canHaveContinues;
private final boolean canHaveReturn;
private final boolean canHaveThrow;
private final int level;
private final TypeKlreplaced ownerClreplaced;
BlockFactory(TypeKlreplaced klreplaced, Type returnType, long complexityLimit, int statementLimit, int operatorLimit, int level, boolean subBlock, boolean canHaveBreaks, boolean canHaveContinues, boolean canHaveReturn, boolean canHaveThrows) {
this.ownerClreplaced = klreplaced;
this.returnType = returnType;
this.complexityLimit = complexityLimit;
this.statementLimit = statementLimit;
this.operatorLimit = operatorLimit;
this.level = level;
this.subBlock = subBlock;
this.canHaveBreaks = canHaveBreaks;
this.canHaveContinues = canHaveContinues;
this.canHaveReturn = canHaveReturn;
this.canHaveThrow = canHaveThrows;
}
@Override
public Block produce() throws ProductionFailedException {
if (statementLimit > 0 && complexityLimit > 0) {
List<IRNode> content = new ArrayList<>();
int slimit = PseudoRandom.randomNotZero(statementLimit);
long climit = complexityLimit;
IRNodeBuilder builder = new IRNodeBuilder().setOperatorLimit(operatorLimit).setOwnerKlreplaced(ownerClreplaced).setResultType(returnType).setCanHaveReturn(canHaveReturn).setCanHaveThrow(canHaveThrow).setCanHaveBreaks(canHaveBreaks).setCanHaveContinues(canHaveContinues).setExceptionSafe(false).setNoConsts(false);
Rule<IRNode> rule;
SymbolTable.push();
for (int i = 0; i < slimit && climit > 0; ) {
int subLimit = (int) (PseudoRandom.random() * (slimit - i - 1));
builder.setComplexityLimit((long) (PseudoRandom.random() * climit));
rule = new Rule<>("block");
rule.add("statement", builder.getStatementFactory(), 5);
if (!ProductionParams.disableVarsInBlock.value()) {
rule.add("decl", builder.setIsLocal(true).getDeclarationFactory());
}
if (subLimit > 0) {
builder.setStatementLimit(subLimit).setLevel(level + 1);
if (!ProductionParams.disableNestedBlocks.value()) {
rule.add("block", builder.setCanHaveReturn(false).setCanHaveThrow(false).setCanHaveBreaks(false).setCanHaveContinues(false).getBlockFactory());
rule.add("try-catch", builder.getTryCatchBlockFactory(), 0.3);
builder.setCanHaveReturn(canHaveReturn).setCanHaveThrow(canHaveThrow).setCanHaveBreaks(canHaveBreaks).setCanHaveContinues(canHaveContinues);
}
addControlFlowDeviation(rule, builder);
}
try {
IRNode choiceResult = rule.produce();
if (choiceResult instanceof If || choiceResult instanceof While || choiceResult instanceof DoWhile || choiceResult instanceof For || choiceResult instanceof Switch) {
i += subLimit;
} else {
i++;
}
// climit -= subBlockComplLimit; // very approximate. to obnain a precise value, change to p.complexity()
climit -= choiceResult.complexity();
content.add(choiceResult);
} catch (ProductionFailedException e) {
i++;
}
}
// Ok, if the block can end with break and continue. Generate the appropriate productions.
rule = new Rule<>("block_ending");
if (canHaveBreaks && !subBlock) {
rule.add("break", builder.getBreakFactory());
}
if (canHaveContinues && !subBlock) {
rule.add("continue", builder.getContinueFactory());
}
if (canHaveReturn && !subBlock && !returnType.equals(TypeList.VOID)) {
rule.add("return", builder.setComplexityLimit(climit).getReturnFactory());
}
if (canHaveThrow && !subBlock) {
Type rtException = TypeList.find("java.lang.RuntimeException");
rtException = PseudoRandom.randomElement(TypeUtil.getImplicitlyCastable(TypeList.getAll(), rtException));
rule.add("throw", builder.setResultType(rtException).setComplexityLimit(Math.max(climit, 5)).setOperatorLimit(Math.max(operatorLimit, 5)).getThrowFactory());
}
try {
if (rule.size() > 0) {
content.add(rule.produce());
}
} catch (ProductionFailedException e) {
}
if (!subBlock) {
SymbolTable.pop();
} else {
SymbolTable.merge();
}
return new Block(ownerClreplaced, returnType, content, level);
}
throw new ProductionFailedException();
}
private void addControlFlowDeviation(Rule<IRNode> rule, IRNodeBuilder builder) {
if (!ProductionParams.disableIf.value()) {
rule.add("if", builder.getIfFactory());
}
if (!ProductionParams.disableWhile.value()) {
rule.add("while", builder.getWhileFactory());
}
if (!ProductionParams.disableDoWhile.value()) {
rule.add("do_while", builder.getDoWhileFactory());
}
if (!ProductionParams.disableFor.value()) {
rule.add("for", builder.getForFactory());
}
if (!ProductionParams.disableSwitch.value()) {
rule.add("switch", builder.getSwitchFactory(), 0.1);
}
}
}
19
Source : BitwiseInversionOperatorFactory.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
@Override
protected UnaryOperator generateProduction(Type resultType) throws ProductionFailedException {
return new UnaryOperator(opKind, new IRNodeBuilder().setComplexityLimit(complexityLimit - 1).setOperatorLimit(operatorLimit - 1).setOwnerKlreplaced((TypeKlreplaced) ownerClreplaced).setResultType(resultType).setExceptionSafe(exceptionSafe).setNoConsts(noconsts).getExpressionFactory().produce());
}
19
Source : BitwiseInversionOperatorFactory.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
@Override
protected boolean isApplicable(Type resultType) {
return resultType.equals(TypeList.INT) || resultType.equals(TypeList.LONG);
}
19
Source : BinaryShiftOperatorFactory.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
@Override
protected Pair<Type, Type> generateTypes() {
Type leftType = resultType.equals(TypeList.INT) ? PseudoRandom.randomElement(TypeUtil.getImplicitlyCastable(TypeList.getBuiltInInt(), resultType)) : resultType;
Type rightType = PseudoRandom.randomElement(TypeUtil.getImplicitlyCastable(TypeList.getBuiltInInt(), TypeList.LONG));
return new Pair<>(leftType, rightType);
}
19
Source : BinaryOperatorFactory.java
with GNU General Public License v2.0
from hzio
with GNU General Public License v2.0
from hzio
abstract clreplaced BinaryOperatorFactory extends OperatorFactory<BinaryOperator> {
protected final OperatorKind opKind;
protected final Type resultType;
protected final Type ownerClreplaced;
protected BinaryOperatorFactory(OperatorKind opKind, long complexityLimit, int operatorLimit, Type ownerClreplaced, Type resultType, boolean exceptionSafe, boolean noconsts) {
super(opKind.priority, complexityLimit, operatorLimit, exceptionSafe, noconsts);
this.opKind = opKind;
this.resultType = resultType;
this.ownerClreplaced = ownerClreplaced;
}
protected abstract boolean isApplicable(Type resultType);
protected abstract Pair<Type, Type> generateTypes();
protected BinaryOperator generateProduction(Type leftType, Type rightType) throws ProductionFailedException {
int leftOpLimit = (int) (PseudoRandom.random() * (operatorLimit - 1));
int rightOpLimit = operatorLimit - 1 - leftOpLimit;
long leftComplLimit = (long) (PseudoRandom.random() * (complexityLimit - 1));
long rightComplLimit = complexityLimit - 1 - leftComplLimit;
if (leftOpLimit == 0 || rightOpLimit == 0 || leftComplLimit == 0 || rightComplLimit == 0) {
throw new ProductionFailedException();
}
boolean swap = PseudoRandom.randomBoolean();
IRNodeBuilder builder = new IRNodeBuilder().setExceptionSafe(exceptionSafe).setOwnerKlreplaced((TypeKlreplaced) ownerClreplaced).setNoConsts(!swap && noconsts);
IRNode leftExpr = builder.setComplexityLimit(leftComplLimit).setOperatorLimit(leftOpLimit).setResultType(leftType).getExpressionFactory().produce();
IRNode rightExpr = builder.setComplexityLimit(rightComplLimit).setOperatorLimit(rightOpLimit).setResultType(rightType).getExpressionFactory().produce();
return new BinaryOperator(opKind, resultType, leftExpr, rightExpr);
}
@Override
public final BinaryOperator produce() throws ProductionFailedException {
if (!isApplicable(resultType)) {
// avoid implicit use of resultType.toString()
throw new ProductionFailedException("Type " + resultType.getName() + " is not applicable by " + getClreplaced().getName());
}
Pair<Type, Type> types;
try {
types = generateTypes();
} catch (RuntimeException ex) {
throw new ProductionFailedException(ex.getMessage());
}
try {
SymbolTable.push();
BinaryOperator p = generateProduction(types.first, types.second);
SymbolTable.merge();
return p;
} catch (ProductionFailedException e) {
SymbolTable.pop();
throw e;
}
}
}
See More Examples