Skip to content

Commit

Permalink
Interface updates
Browse files Browse the repository at this point in the history
  • Loading branch information
minborg committed Jan 26, 2015
1 parent 210e7b1 commit 7c552ae
Show file tree
Hide file tree
Showing 18 changed files with 375 additions and 73 deletions.
12 changes: 12 additions & 0 deletions src/main/java/com/speedment/codegen/Adder.java
@@ -0,0 +1,12 @@
package com.speedment.codegen;

/**
*
* @author pemi
* @param <T> the type of the containing class
*/
public interface Adder<T> {

T add();

}
4 changes: 3 additions & 1 deletion src/main/java/com/speedment/codegen/CodeUtil.java
Expand Up @@ -193,7 +193,9 @@ public static void tab(String tab) {
* @return The full name. * @return The full name.
*/ */
public static CharSequence flattenName(ClassAndInterfaceBase model) { public static CharSequence flattenName(ClassAndInterfaceBase model) {
return new $(Trees.walk(model.getPackage(), Package_::getPackage).map(Package_::getName).collect(Collectors.joining(DOT)), DOT, model.getName()); return new $(Trees.walk(model.getPackage(), Package_::getPackage, Trees.Order.BACKWARD).map(Package_::getName).collect(Collectors.joining(DOT)),
DOT,
model.getName());
} }


/** /**
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/com/speedment/codegen/Test.java
Expand Up @@ -23,6 +23,7 @@
import com.speedment.codegen.model.annotation.Annotation_; import com.speedment.codegen.model.annotation.Annotation_;
import com.speedment.codegen.model.field.Field_; import com.speedment.codegen.model.field.Field_;
import static com.speedment.codegen.model.Type_.STRING; import static com.speedment.codegen.model.Type_.STRING;
import com.speedment.codegen.model.block.Block_;
import com.speedment.codegen.model.class_.Class_; import com.speedment.codegen.model.class_.Class_;


import com.speedment.codegen.model.method.Method_; import com.speedment.codegen.model.method.Method_;
Expand Down Expand Up @@ -50,6 +51,10 @@ public static void main(String[] args) {


CodeUtil.tab(" "); CodeUtil.tab(" ");


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

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

final Class_ class_ = new Class_() final Class_ class_ = new Class_()
.package_("org.speedment.codegen.test") .package_("org.speedment.codegen.test")
.public_() .public_()
Expand All @@ -63,10 +68,13 @@ public static void main(String[] args) {
.add(new Statement_( .add(new Statement_(
"return (foo + baz + bar);" "return (foo + baz + bar);"
)) ))
); )
.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();


new AccessorImplementer().apply(class_); new AccessorImplementer().accept(class_);
new AutomaticDependencies().apply(class_); new AutomaticDependencies().accept(class_);


class_.add(Annotation_.DEPRECATED); class_.add(Annotation_.DEPRECATED);
JavaCodeGen gen = new JavaCodeGen(); JavaCodeGen gen = new JavaCodeGen();
Expand Down
Expand Up @@ -32,7 +32,7 @@
public class AccessorImplementer implements Controller<Class_> { public class AccessorImplementer implements Controller<Class_> {


@Override @Override
public void apply(Class_ class_) { public void accept(Class_ class_) {
class_.getFields().forEach((f) -> generateAccessors(class_, f)); class_.getFields().forEach((f) -> generateAccessors(class_, f));
} }


Expand Down
Expand Up @@ -33,7 +33,7 @@
*/ */
public class AutomaticDependencies implements Controller<Class_> { public class AutomaticDependencies implements Controller<Class_> {
@Override @Override
public void apply(Class_ model) { public void accept(Class_ model) {
model.getAnnotations().stream() model.getAnnotations().stream()
.filter(a -> isDependantOf(model, a.getAnnotationClass())) .filter(a -> isDependantOf(model, a.getAnnotationClass()))
.forEach(a -> model.add(dependency(a))); .forEach(a -> model.add(dependency(a)));
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/com/speedment/codegen/control/Controller.java
Expand Up @@ -17,7 +17,12 @@
package com.speedment.codegen.control; package com.speedment.codegen.control;


import com.speedment.codegen.model.CodeModel; import com.speedment.codegen.model.CodeModel;
import java.util.function.Consumer;

@FunctionalInterface
public interface Controller<T extends CodeModel> extends Consumer<T> {

@Override
abstract void accept(T model);


public interface Controller<T extends CodeModel> {
void apply(T model);
} }
40 changes: 25 additions & 15 deletions src/main/java/com/speedment/codegen/model/CodeModel.java
Expand Up @@ -16,25 +16,35 @@
*/ */
package com.speedment.codegen.model; package com.speedment.codegen.model;


import java.util.stream.Stream;

/** /**
* *
* @author Duncan * @author Duncan
*/ */
public interface CodeModel { public interface CodeModel {
/**
* The number of model types there are. If new types are added to this enum,
* new models and new views will have be be created. All implementations of
* <code>CodeViewBuilder</code> will have to be updated to support the new
* enum case.
*/
public static enum Type {
ANNOTATION, BLOCK, CLASS, CONSTRUCTOR, DEPENDENCY, EXPRESSION, FIELD,
INTERFACE, METHOD, OPERATOR, PACKAGE, STATEMENT, TYPE, PARAMETER, GENERIC_PARAMETER;
}


/** /**
* Returns the type of this model. * The number of model types there are. If new types are added to this enum,
* @return the type. * new models and new views will have be be created. All implementations of
*/ * <code>CodeViewBuilder</code> will have to be updated to support the new
Type getModelType(); * enum case.
*/
public static enum Type {

ANNOTATION, BLOCK, CLASS, CONSTRUCTOR, DEPENDENCY, EXPRESSION, FIELD,
INTERFACE, METHOD, OPERATOR, PACKAGE, STATEMENT, TYPE, PARAMETER, GENERIC_PARAMETER;
}

/**
* Returns the type of this model.
*
* @return the type.
*/
Type getModelType();

default Stream<CodeModel> stream() {
return Stream.empty();
}

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


import com.speedment.codegen.CodeUtil;
import com.speedment.codegen.Nameable; import com.speedment.codegen.Nameable;
import com.speedment.codegen.model.CodeModel; import com.speedment.codegen.model.CodeModel;
import com.speedment.codegen.model.Type_; import com.speedment.codegen.model.Type_;
Expand All @@ -27,14 +26,18 @@
import com.speedment.codegen.model.block.InitializerBlock_; import com.speedment.codegen.model.block.InitializerBlock_;
import com.speedment.codegen.model.dependency_.Dependable; import com.speedment.codegen.model.dependency_.Dependable;
import com.speedment.codegen.model.dependency_.Dependency_; import com.speedment.codegen.model.dependency_.Dependency_;
import com.speedment.codegen.model.field.FieldAdder;
import com.speedment.codegen.model.field.Field_; import com.speedment.codegen.model.field.Field_;
import com.speedment.codegen.model.field.Fieldable; import com.speedment.codegen.model.field.Fieldable;
import com.speedment.codegen.model.method.MethodAdder;
import com.speedment.codegen.model.method.Methodable; import com.speedment.codegen.model.method.Methodable;
import com.speedment.codegen.model.modifier.Modifiable; import com.speedment.codegen.model.modifier.Modifiable;
import com.speedment.codegen.model.modifier.Modifier_; import com.speedment.codegen.model.modifier.Modifier_;
import com.speedment.codegen.model.package_.Packagable; import com.speedment.codegen.model.package_.Packagable;
import com.speedment.codegen.model.package_.Package_; import com.speedment.codegen.model.package_.Package_;
import com.speedment.util.StreamUtil;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
Expand Down Expand Up @@ -72,6 +75,15 @@ public ClassAndInterfaceBase(final Class<M> mClass) {
dependencies = new HashSet<>(); dependencies = new HashSet<>();
} }


@Override
public Stream<CodeModel> stream() {
return streamBuilder().build();
}

protected Stream.Builder<CodeModel> streamBuilder() {
return StreamUtil.streamBuilder(fields, methods, interfaces, modifiers, annotations, nestedClasses, initializers, dependencies);
}

@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public T add(final Interface_ interf) { public T add(final Interface_ interf) {
Expand Down Expand Up @@ -229,9 +241,39 @@ public Type_ toType() {
public T package_(CharSequence packagePath) { public T package_(CharSequence packagePath) {
return setPackage(Package_.by(packagePath)); return setPackage(Package_.by(packagePath));
} }


// public T method() { public MethodAdder<T> methodAdder() {
// return return new MyMethodAdder();
// } }


public FieldAdder<T> fieldAdder() {
return new MyFieldAdder();
}

private class MyMethodAdder extends MethodAdder<T> {

public MyMethodAdder() {
super((T) ClassAndInterfaceBase.this);
}

@Override
public void addToParent(T parent) {
parent.add(this);
}

}

private class MyFieldAdder extends FieldAdder<T> {

public MyFieldAdder() {
super((T) ClassAndInterfaceBase.this);
}

@Override
public void addToParent(T parent) {
parent.add(this);
}

}

} }
8 changes: 8 additions & 0 deletions src/main/java/com/speedment/codegen/model/class_/Class_.java
Expand Up @@ -16,10 +16,13 @@
*/ */
package com.speedment.codegen.model.class_; package com.speedment.codegen.model.class_;


import com.speedment.codegen.model.CodeModel;
import com.speedment.codegen.model.Type_; import com.speedment.codegen.model.Type_;
import com.speedment.codegen.model.modifier.ClassModifier_; import com.speedment.codegen.model.modifier.ClassModifier_;
import com.speedment.util.StreamUtil;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Stream;


/** /**
* *
Expand Down Expand Up @@ -53,6 +56,11 @@ public Class_(String className, Type_ superClass) {
setSuperClassType(superClass); setSuperClassType(superClass);
} }


@Override
protected Stream.Builder<CodeModel> streamBuilder() {
return StreamUtil.streamBuilder(super.streamBuilder(), constructors);
}

public Class_ add(Constructor_ constructor) { public Class_ add(Constructor_ constructor) {
getConstructors().add(constructor); getConstructors().add(constructor);
return this; return this;
Expand Down
Expand Up @@ -16,7 +16,6 @@
*/ */
package com.speedment.codegen.model.class_; package com.speedment.codegen.model.class_;


import com.speedment.codegen.model.modifier.ClassModifier_;
import com.speedment.codegen.model.modifier.InterfaceModifier_; import com.speedment.codegen.model.modifier.InterfaceModifier_;


/** /**
Expand Down
96 changes: 96 additions & 0 deletions src/main/java/com/speedment/codegen/model/field/FieldAdder.java
@@ -0,0 +1,96 @@
package com.speedment.codegen.model.field;

import com.speedment.codegen.Adder;
import com.speedment.codegen.model.annotation.Annotation_;
import com.speedment.codegen.model.modifier.FieldModifier_;
import java.util.Set;

/**
*
* @author pemi
* @param <T>
*/
public abstract class FieldAdder<T> extends Field_ implements Adder<T> {

T parent;

public FieldAdder(T parent) {
super(null, null);
this.parent = parent;
}

protected abstract void addToParent(T parent);

@Override
public T add() {
addToParent(parent);
return parent;
}

@Override
public FieldAdder<T> public_() {
super.public_();
return this;
}

@Override
public FieldAdder<T> protected_() {
super.protected_();
return this;
}

@Override
public FieldAdder<T> private_() {
super.private_();
return this;
}

@Override
public FieldAdder<T> static_() {
super.static_();
return this;
}

@Override
public FieldAdder<T> final_() {
super.final_();
return this;
}

@Override
public FieldAdder<T> transient_() {
super.transient_();
return this;
}

@Override
public FieldAdder<T> volatile_() {
super.volatile_();
return this;
}

@Override
public FieldAdder<T> set(Set<FieldModifier_> newSet) {
super.set(newSet);
return this;
}

@Override
public FieldAdder<T> setName(CharSequence name) {
super.setName(name);
return this;
}

@Override
public FieldAdder<T> add(Annotation_ annotation) {
super.add(annotation);
return this;
}

@Override
public FieldAdder<T> add(FieldModifier_ firstModifier_m, FieldModifier_... restModifiers) {
super.add(firstModifier_m, restModifiers);
return this;
}

}
8 changes: 3 additions & 5 deletions src/main/java/com/speedment/codegen/model/field/Field_.java
Expand Up @@ -65,8 +65,9 @@ public Field_ setName(CharSequence name_) {
return this; return this;
} }


public void setType(Type_ type) { public Field_ setType(Type_ type) {
this.setType_(type); this.type = type;
return this;
} }


public Expression_ getExpression() { public Expression_ getExpression() {
Expand All @@ -86,9 +87,6 @@ public Type_ getType() {
return type; return type;
} }


public void setType_(Type_ type) {
this.type = type;
}


@Override @Override
public Set<FieldModifier_> getModifiers() { public Set<FieldModifier_> getModifiers() {
Expand Down

0 comments on commit 7c552ae

Please sign in to comment.