Skip to content

Commit

Permalink
Major refactor of codegen model
Browse files Browse the repository at this point in the history
  • Loading branch information
minborg committed Jan 28, 2015
1 parent f89558d commit 331b7ca
Show file tree
Hide file tree
Showing 47 changed files with 930 additions and 446 deletions.
31 changes: 21 additions & 10 deletions src/main/java/com/speedment/codegen/Test.java
Expand Up @@ -18,17 +18,20 @@

import com.speedment.codegen.control.AccessorImplementer;
import com.speedment.codegen.control.AutomaticDependencies;
import com.speedment.codegen.model.Type_;
import com.speedment.codegen.model.type.ScalarType_;
import com.speedment.codegen.model.statement.Statement_;
import com.speedment.codegen.model.annotation.Annotation_;
import com.speedment.codegen.model.field.Field_;
import static com.speedment.codegen.model.Type_.STRING;
import com.speedment.codegen.model.block.Block_;
import static com.speedment.codegen.model.type.Type_.STRING;
import com.speedment.codegen.model.statement.block.Block_;
import com.speedment.codegen.model.class_.Class_;

import com.speedment.codegen.model.method.Method_;
import com.speedment.codegen.model.package_.Package_;
import com.speedment.codegen.model.parameter.Parameter_;
import com.speedment.codegen.model.statement.If;
import com.speedment.codegen.model.statement.block.InitializerBlock_;
import com.speedment.codegen.model.statement.expression.Expression;
import com.speedment.codegen.model.statement.expression.binary.Equals;
import com.speedment.codegen.view.java.JavaCodeGen;
import java.io.InputStream;

Expand All @@ -45,11 +48,17 @@ public static void main(String[] args) {

CodeUtil.tab(" ");

Type_ type = new Type_(InputStream.class);
ScalarType_ type = new ScalarType_(InputStream.class);

final Statement_ s = new Statement_("int bar = 1");
final Statement_ s = Statement_.of("int bar = 1");

final Block_ block = new Block_().add(new Statement_("int foo=1")).add(new Statement_("int bar=1"));
final Block_ block = new Block_().add(Statement_.of("int foo=1")).add(Statement_.of("int bar=1"));

final Block_ ifBlockTest = new Block_().add(
Statement_.of("final thatCoolFeeling"),
new If(new Equals(Expression.of("1"), Expression.of("1")))
.addTrue("thatCoolFeeling=1", "int foo = 1")
.addFalse("thatCoolFeeling=0"));

final Class_ class_ = new Class_()
.package_("org.speedment.codegen.test")
Expand All @@ -64,13 +73,15 @@ public static void main(String[] args) {
.public_().final_()
.add(new Parameter_().final_().setType(STRING).setName("baz"))
.add(new Parameter_(STRING, "bazer"))
.add(new Statement_(
.add(Statement_.of(
"return (foo + baz + bar);"
))
.add(Annotation_.OVERRIDE))
.methodAdder().public_().setType(STRING).setName("bar").add(s).add()
.methodAdder().public_().setType(STRING).setName("foo").add(new Statement_("int foo=1")).add()
.methodAdder().public_().setType(STRING).setName("fooBar").add(new Statement_(block)).add();
.methodAdder().public_().setType(STRING).setName("foo").add(Statement_.of("int foo=1")).add()
.methodAdder().public_().setType(STRING).setName("fooBar").add(block).add()
.methodAdder().public_().setType(STRING).setName("ifTester").add(ifBlockTest).add()
.add(new InitializerBlock_().static_().add(Statement_.of("int fool = 42")));

new AccessorImplementer().accept(class_);
new AutomaticDependencies().accept(class_);
Expand Down
Expand Up @@ -22,7 +22,7 @@
import com.speedment.codegen.model.statement.Statement_;
import com.speedment.codegen.model.class_.Class_;
import static com.speedment.codegen.CodeUtil.*;
import com.speedment.codegen.model.Type_;
import com.speedment.codegen.model.type.ScalarType_;
import com.speedment.codegen.model.parameter.Parameter_;
import com.speedment.util.$;

Expand Down Expand Up @@ -54,23 +54,23 @@ protected void generateGetter(final Class_ class_, final Field_ field_) {
field_.getType(),
new $(GET_STRING, ucfirst(field_.getName()))
).public_()
.add(new Statement_(
.add(Statement_.of(
new $(RETURN_STRING, field_.getName(), SC)
))
);
}

protected void generateSetter(final Class_ class_, final Field_ field_) {
class_.add(new Method_(
new Type_(CodeUtil.flattenName(class_)),
new ScalarType_(CodeUtil.flattenName(class_)),
new $(SET_STRING, ucfirst(field_.getName()))
).public_()
.add(new Parameter_(
field_.getType(),
field_.getName()
)).add(new Statement_(
)).add(Statement_.of(
new $(THIS_STRING, DOT, field_.getName(), ASSIGNMENT_STRING, field_.getName(), SC)
)).add(new Statement_(
)).add(Statement_.of(
new $(RETURN_STRING, THIS_STRING, SC)
))
);
Expand Down
Expand Up @@ -16,10 +16,10 @@
*/
package com.speedment.codegen.control;

import com.speedment.codegen.model.Type_;
import com.speedment.codegen.model.class_.Class_;
import com.speedment.codegen.model.dependency_.Dependency_;
import static com.speedment.codegen.CodeUtil.*;
import com.speedment.codegen.model.type.Type_;
import com.speedment.codegen.model.annotation.Annotation_;
import com.speedment.codegen.model.class_.ClassAndInterfaceBase;
import com.speedment.codegen.model.method.Method_;
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/com/speedment/codegen/model/AbstractCodeModel.java
@@ -0,0 +1,38 @@
package com.speedment.codegen.model;

import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.Stream;

/**
*
* @author pemi
* @param <T> the CodeModel type
*/
public abstract class AbstractCodeModel<T extends AbstractCodeModel<T>> implements CodeModel {

@SuppressWarnings("unchecked")
protected <P> T add(final P parameter, final Consumer<P> consumer) {
consumer.accept(parameter);
return (T) this;
}

@SuppressWarnings("unchecked")
protected <P> T addSeveral(final P[] restOfParameters, final Consumer<P> consumer) {
Stream.of(restOfParameters).forEach(consumer::accept);
return (T) this;
}

@SuppressWarnings("unchecked")
protected <P1, P2> T add(final P1 firstParameter, final P2 secondParameter, final BiConsumer<P1, P2> biConsumer) {
biConsumer.accept(firstParameter, secondParameter);
return (T) this;
}

@SuppressWarnings("unchecked")
protected <P> T set(final P item, final Consumer<P> consumer) {
consumer.accept(item);
return (T) this;
}

}
@@ -0,0 +1,37 @@
package com.speedment.codegen.model;

import com.speedment.codegen.model.modifier.Modifiable;
import com.speedment.codegen.model.modifier.Modifier_;
import java.util.Set;
import java.util.stream.Stream;

/**
*
* @author pemi
* @param <T>
* @param <M>
*/
public abstract class AbstractModifiableCodeModel<T extends AbstractModifiableCodeModel<T, M>, M extends Modifier_<M>> extends AbstractCodeModel<T> implements Modifiable<M> {

@Override
public T add(final M firstClassModifier_m, final M... restClassModifiers) {
return add(firstClassModifier_m, restClassModifiers, (f, s) -> {
getModifiers().add(f);
Stream.of(s).forEach(getModifiers()::add);
});
}

@Override
public T set(final Set<M> newSet) {
return set(newSet, s -> {
getModifiers().clear();
getModifiers().addAll(s);
});
}

@Override
public boolean is(M modifier) {
return getModifiers().contains(modifier);
}

}
3 changes: 3 additions & 0 deletions src/main/java/com/speedment/codegen/model/Expression_.java
Expand Up @@ -19,7 +19,10 @@
/**
*
* @author pemi
* @deprecated replaced by
* com.speedment.codegen.model.statement.expression.Expression
*/
@Deprecated
public class Expression_ implements CodeModel {

private CharSequence stringExpression;
Expand Down
35 changes: 28 additions & 7 deletions src/main/java/com/speedment/codegen/model/Operator_.java
Expand Up @@ -16,17 +16,27 @@
*/
package com.speedment.codegen.model;

import com.speedment.codegen.model.statement.expression.Expression;
import com.speedment.codegen.model.statement.expression.SimpleExpression;
import com.speedment.codegen.model.statement.expression.binary.Equals;
import com.speedment.codegen.model.statement.expression.binary.Minus;
import com.speedment.codegen.model.statement.expression.binary.Plus;
import com.speedment.codegen.model.statement.expression.trinary.Conditional;
import com.speedment.codegen.model.statement.expression.unary.Not;
import java.util.function.Supplier;

/**
* Java Operators (3.12 Operators).
*
* @author pemi
*/
public enum Operator_ implements CodeModel {

ASSIGN("="), GREATER_THAN("String>"), LESS_THAN("<"), NOT("!"), COMPLEMENT("~"), QUESTION_MARK("?"), COLON(":"), ARROW("->"),
EQUALS("=="), GREATER_OR_EQAL(">="), LESS_OR_EQUAL("<="), NOT_EQUAL("!="),
NONE(""),
ASSIGN("="), GREATER_THAN(">"), LESS_THAN("<"), NOT("!", Not::new), COMPLEMENT("~"), CONDITIONAL("?", Conditional::new), COLON(":"), ARROW("->"),
EQUALS("==", Equals::new), GREATER_OR_EQAL(">="), LESS_OR_EQUAL("<="), NOT_EQUAL("!="),
AND_LOGICAL("&&"), OR_LOGICAL("||"), INCREMENT("++"), DECREMENT("--"),
PLUS("+"), MINUS("-"), MULTIPLY("*"), DIVIDE("/"), AND_BINARY("&"), OR_BINARY("|"), XOR_BINARY("^"), MODULO("%"),
PLUS("+", Plus::new), MINUS("-", Minus::new), MULTIPLY("*"), DIVIDE("/"), AND_BINARY("&"), OR_BINARY("|"), XOR_BINARY("^"), MODULO("%"),
LEFT_SHIFT("<<"), RIGHT_SHIFT(">>"), RIGTH_SHIFT_ZEROIN(">>>"),
ADD_AND_ASSIGN("+="), MINUS_AND_ASSINGN("-="), MULTIPLY_AND_ASSIGN("*="), DIVIDE_AND_ASSIGN("/="),
MODULO_AND_ASSING("%="), LEFT_SHIFT_AND_ASSIGN("<<="), RIGHT_SHIFT_AND_ASSIGN(">>="), RIGHT_SHIFT_ZEROIN_AND_ASSIGN(">>>=");
Expand All @@ -38,18 +48,29 @@ public enum Operator_ implements CodeModel {
+= -= *= /= &= |= ^= %= <<= >>= >>>=
*/
private final String text;
private final Supplier<Expression> supplier;

private Operator_(String text) {
this.text = text;
supplier = SimpleExpression::new;
}

private Operator_(String text, Supplier<Expression> supplier) {
this.text = text;
this.supplier = supplier;
}

public String getText() {
return text;
}

@Override
public Type getModelType() {
return Type.OPERATOR;
}
@Override
public Type getModelType() {
return Type.OPERATOR;
}

Expression newExpression() {
return supplier.get();
}

}
Expand Up @@ -16,6 +16,7 @@
*/
package com.speedment.codegen.model.annotation;

import com.speedment.codegen.model.AbstractCodeModel;
import com.speedment.codegen.model.CodeModel;
import com.speedment.codegen.model.CodeModel.Type;
import java.lang.annotation.Annotation;
Expand All @@ -26,7 +27,7 @@
*
* @author pemi
*/
public class Annotation_ implements CodeModel {
public class Annotation_ extends AbstractCodeModel<Annotation_> implements CodeModel {

public static final Annotation_ OVERRIDE = new UnmodifiableAnnotation_(Override.class);
public static final Annotation_ DEPRECATED = new UnmodifiableAnnotation_(Deprecated.class);
Expand All @@ -49,8 +50,9 @@ public Annotation_(Class<? extends Annotation> annotationClass) {
// this.annotaionClassName = annotaionClassName;
// }
public Annotation_ put(final String key, final Object value) {
getValuePairs().put(key, value);
return this;
return add(key, value, (k, v) -> {
getValuePairs().put(k, v);
});
}

public Class<? extends Annotation> getAnnotationClass() {
Expand Down

0 comments on commit 331b7ca

Please sign in to comment.