Skip to content

Commit

Permalink
codegen: Create models for representing anonymous implementations (#434)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyknic committed May 9, 2017
1 parent d3a998d commit a943e34
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 35 deletions.
Expand Up @@ -2,11 +2,14 @@


import com.speedment.common.codegen.internal.util.Copier; import com.speedment.common.codegen.internal.util.Copier;
import com.speedment.common.codegen.model.ClassOrInterface; import com.speedment.common.codegen.model.ClassOrInterface;
import com.speedment.common.codegen.model.Generic; import com.speedment.common.codegen.model.Field;
import com.speedment.common.codegen.model.Initializer;
import com.speedment.common.codegen.model.Method;
import com.speedment.common.codegen.model.Value; import com.speedment.common.codegen.model.Value;
import com.speedment.common.codegen.model.trait.HasCopy; import com.speedment.common.codegen.model.trait.HasCopy;
import com.speedment.common.codegen.model.value.AnonymousValue; import com.speedment.common.codegen.model.value.AnonymousValue;


import java.lang.reflect.Type;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;


Expand All @@ -16,22 +19,39 @@
* @author Emil Forslund * @author Emil Forslund
* @since 2.4.6 * @since 2.4.6
*/ */
public final class AnonymousValueImpl<T extends ClassOrInterface<T>> public final class AnonymousValueImpl
implements AnonymousValue<T> { implements AnonymousValue {


private final List<Value<?>> args; private final List<Value<?>> args;
private final List<Generic> generics; private final List<Type> typeParams;
private T classOrInterface; private final List<Field> fields;
private final List<Method> methods;
private final List<Initializer> initializers;
private final List<ClassOrInterface<?>> innerClasses;
private Type value;


public AnonymousValueImpl() { public AnonymousValueImpl() {
this.args = new ArrayList<>(); this.args = new ArrayList<>();
this.generics = new ArrayList<>(); this.typeParams = new ArrayList<>();
this.fields = new ArrayList<>();
this.methods = new ArrayList<>();
this.initializers = new ArrayList<>();
this.innerClasses = new ArrayList<>();
} }


private AnonymousValueImpl(AnonymousValue<T> prototype) { private AnonymousValueImpl(AnonymousValue prototype) {
this.args = Copier.copy(prototype.getValues(), HasCopy::copy); this.args = Copier.copy(prototype.getValues(), HasCopy::copy);
this.generics = Copier.copy(prototype.getGenerics()); this.typeParams = new ArrayList<>(prototype.getTypeParameters());
this.classOrInterface = Copier.copy(prototype.getValue()); this.fields = Copier.copy(prototype.getFields());
this.methods = Copier.copy(prototype.getMethods());
this.initializers = Copier.copy(prototype.getInitializers());
this.innerClasses = Copier.copy(prototype.getClasses(), HasCopy::copy);
this.value = prototype.getValue();
}

@Override
public Type getValue() {
return value;
} }


@Override @Override
Expand All @@ -40,44 +60,65 @@ public List<Value<?>> getValues() {
} }


@Override @Override
public List<Generic> getGenerics() { public List<Type> getTypeParameters() {
return generics; return typeParams;
} }


@Override @Override
public T getValue() { public List<Field> getFields() {
return classOrInterface; return fields;
} }


@Override @Override
public AnonymousValue<T> setValue(T value) { public List<Method> getMethods() {
classOrInterface = value; return methods;
}

@Override
public List<Initializer> getInitializers() {
return initializers;
}

@Override
public List<ClassOrInterface<?>> getClasses() {
return innerClasses;
}

@Override
public AnonymousValue setValue(Type value) {
this.value = value;
return this; return this;
} }


@Override @Override
public Value<T> copy() { public AnonymousValue copy() {
return new AnonymousValueImpl<>(this); return new AnonymousValueImpl(this);
} }


@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
if (!(o instanceof AnonymousValue)) return false; if (!(o instanceof AnonymousValue)) return false;


final AnonymousValue<?> that = (AnonymousValue<?>) o; AnonymousValue that = (AnonymousValue) o;
if (!args.equals(that.getValues())) return false;
else if (!getGenerics().equals(that.getGenerics())) return false; if (args != null ? !args.equals(that.getValue()) : that.getValue() != null)
return classOrInterface != null return false;
? classOrInterface.equals(that.getValue()) if (!typeParams.equals(that.getTypeParameters())) return false;
: that.getValue() == null; if (!getFields().equals(that.getFields())) return false;
if (!getMethods().equals(that.getMethods())) return false;
if (!getInitializers().equals(that.getInitializers())) return false;
return innerClasses.equals(that.getClasses());
} }


@Override @Override
public int hashCode() { public int hashCode() {
int result = args.hashCode(); int result = args != null ? args.hashCode() : 0;
result = 31 * result + getGenerics().hashCode(); result = 31 * result + typeParams.hashCode();
result = 31 * result + (classOrInterface != null ? classOrInterface.hashCode() : 0); result = 31 * result + getFields().hashCode();
result = 31 * result + getMethods().hashCode();
result = 31 * result + getInitializers().hashCode();
result = 31 * result + innerClasses.hashCode();
return result; return result;
} }
} }
@@ -1,22 +1,52 @@
package com.speedment.common.codegen.model.value; package com.speedment.common.codegen.model.value;


import com.speedment.common.codegen.model.ClassOrInterface;
import com.speedment.common.codegen.model.Value; import com.speedment.common.codegen.model.Value;
import com.speedment.common.codegen.model.trait.HasGenerics; import com.speedment.common.codegen.model.trait.HasClasses;
import com.speedment.common.codegen.model.trait.HasFields;
import com.speedment.common.codegen.model.trait.HasInitializers;
import com.speedment.common.codegen.model.trait.HasMethods;
import com.speedment.common.codegen.model.trait.HasValues; import com.speedment.common.codegen.model.trait.HasValues;


import java.lang.reflect.Type;
import java.util.List;

/** /**
* A value representing a reference to an abstract implementation used * A value representing a reference to an abstract implementation used
* anonymously to set a field. * anonymously to set a field.
* *
* @author Emil Forslund * @author Emil Forslund
* @since 2.4.6 * @since 2.4.6
*/ */
public interface AnonymousValue<T extends ClassOrInterface<T>> public interface AnonymousValue
extends Value<T>, extends Value<Type>,
HasGenerics<AnonymousValue<T>>, HasValues<AnonymousValue>,
HasValues<AnonymousValue<T>> { HasMethods<AnonymousValue>,
HasFields<AnonymousValue>,
HasInitializers<AnonymousValue>,
HasClasses<AnonymousValue> {


@Override @Override
AnonymousValue<T> setValue(T value); AnonymousValue setValue(Type value);

/**
* Returns a list of type parameters to set when instantiating the anonymous
* type.
* <p>
* The returned list is mutable.
*
* @return list of type parameters
*/
List<Type> getTypeParameters();

/**
* Adds the specified type parameter to the end of the
* {@link #getTypeParameters} list.
*
* @param typeParameter the type parameter to add
* @return a reference to this
*/
default AnonymousValue add(Type typeParameter) {
getTypeParameters().add(typeParameter);
return this;
}
} }

0 comments on commit a943e34

Please sign in to comment.