Created
May 7, 2015 00:42
-
-
Save bhamiltoncx/de7a855cb9a37245e79f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% javac -cp ~/src/immutables/value/target/value-2.0.10.jar -s generated src/foo/Foo.java src/bar/Bar.java | |
warning: Abstract generator process, processing over false, error raised false | |
Note: Static env init | |
warning: Abstract generator round | |
src/bar/Bar.java:6: warning: Extracting imports | |
public interface Bar { | |
^ | |
warning: Extracting source for element bar.Bar (bar/Bar.java) | |
warning: Could not collect imports for bar.Bar: java.io.FileNotFoundException: bar/Bar.java | |
src/bar/Bar.java:6: warning: Got imports: org.immutables.value.internal.$generator$.$SourceExtraction$Imports@3bd94634: all=[] classes={} | |
public interface Bar { | |
^ | |
src/bar/Bar.java:6: warning: Declaring type present DeclaringType{name=bar.Bar}, source imports org.immutables.value.internal.$generator$.$SourceExtraction$Imports@3bd94634: all=[] classes={} | |
public interface Bar { | |
^ | |
src/bar/Bar.java:6: warning: package DeclaringPackage{name=bar} current Type[Bar] Imports[] | |
public interface Bar { | |
^ | |
src/foo/Foo.java:8: warning: Extracting imports | |
public interface Foo { | |
^ | |
warning: Extracting source for element foo.Foo (foo/Foo.java) | |
warning: Could not collect imports for foo.Foo: java.io.FileNotFoundException: foo/Foo.java | |
src/foo/Foo.java:8: warning: Got imports: org.immutables.value.internal.$generator$.$SourceExtraction$Imports@3bd94634: all=[] classes={} | |
public interface Foo { | |
^ | |
src/foo/Foo.java:8: warning: Declaring type present DeclaringType{name=foo.Foo}, source imports org.immutables.value.internal.$generator$.$SourceExtraction$Imports@3bd94634: all=[] classes={} | |
public interface Foo { | |
^ | |
src/foo/Foo.java:8: warning: Declaring type present DeclaringType{name=foo.Foo}, source imports org.immutables.value.internal.$generator$.$SourceExtraction$Imports@3bd94634: all=[] classes={} | |
public interface Foo { | |
^ | |
src/foo/Foo.java:8: warning: package DeclaringPackage{name=foo} current Type[Foo] Imports[] | |
public interface Foo { | |
^ | |
src/bar/Bar.java:6: warning: Declaring type present DeclaringType{name=bar.Bar}, source imports org.immutables.value.internal.$generator$.$SourceExtraction$Imports@3bd94634: all=[] classes={} | |
public interface Bar { | |
^ | |
src/foo/Foo.java:8: warning: Declaring type present DeclaringType{name=foo.Foo}, source imports org.immutables.value.internal.$generator$.$SourceExtraction$Imports@3bd94634: all=[] classes={} | |
public interface Foo { | |
^ | |
warning: Values: {DeclaringPackage{name=bar}=[Type[Bar] Imports[]], DeclaringPackage{name=foo}=[Type[Foo] Imports[]]} | |
warning: Invoking invokable class org.immutables.value.internal.$processor$.$Generator_Immutables$1 | |
src/foo/Foo.java:8: warning: Declaring type present DeclaringType{name=foo.Foo}, source imports org.immutables.value.internal.$generator$.$SourceExtraction$Imports@3bd94634: all=[] classes={} | |
public interface Foo { | |
^ | |
warning: Invoking invokable class org.immutables.value.internal.$processor$.$Generator_Gsons$1 | |
warning: Invoking invokable class org.immutables.value.internal.$processor$.$Generator_Repositories$1 | |
warning: Abstract generator done | |
Note: Static env shutdown | |
warning: Abstract generator process, processing over false, error raised false | |
Note: Static env init | |
warning: Abstract generator round | |
warning: Values: {} | |
warning: Invoking invokable class org.immutables.value.internal.$processor$.$Generator_Immutables$1 | |
warning: Invoking invokable class org.immutables.value.internal.$processor$.$Generator_Gsons$1 | |
warning: Invoking invokable class org.immutables.value.internal.$processor$.$Generator_Repositories$1 | |
warning: Abstract generator done | |
Note: Static env shutdown | |
generated/foo/ImmutableFoo.java:17: error: cannot find symbol | |
private final ImmutableBar bar; | |
^ | |
symbol: class ImmutableBar | |
location: class ImmutableFoo | |
generated/foo/ImmutableFoo.java:23: error: cannot find symbol | |
private ImmutableFoo(ImmutableFoo original, ImmutableBar bar) { | |
^ | |
symbol: class ImmutableBar | |
location: class ImmutableFoo | |
generated/foo/ImmutableFoo.java:32: error: cannot find symbol | |
public ImmutableBar bar() { | |
^ | |
symbol: class ImmutableBar | |
location: class ImmutableFoo | |
generated/foo/ImmutableFoo.java:42: error: cannot find symbol | |
public final ImmutableFoo withBar(ImmutableBar value) { | |
^ | |
symbol: class ImmutableBar | |
location: class ImmutableFoo | |
generated/foo/ImmutableFoo.java:122: error: cannot find symbol | |
private ImmutableBar bar; | |
^ | |
symbol: class ImmutableBar | |
location: class Builder | |
generated/foo/ImmutableFoo.java:144: error: cannot find symbol | |
public final Builder bar(ImmutableBar bar) { | |
^ | |
symbol: class ImmutableBar | |
location: class Builder | |
warning: Abstract generator process, processing over true, error raised false | |
Note: Static env init | |
warning: Abstract generator done | |
Note: Static env shutdown | |
6 errors | |
32 warnings | |
[bgertzfield@beng-mbp:~/src/immutable-test-4]% cat generated/foo/ImmutableFoo.java | |
package foo; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.Objects; | |
import javax.annotation.Generated; | |
/** | |
* Immutable implementation of {@link Foo}. | |
* <p> | |
* Use builder to create immutable instances: | |
* {@code ImmutableFoo.builder()}. | |
*/ | |
@SuppressWarnings("all") | |
@Generated({"Immutables.generator", "Foo"}) | |
public final class ImmutableFoo implements Foo { | |
private final ImmutableBar bar; | |
private ImmutableFoo(ImmutableFoo.Builder builder) { | |
this.bar = builder.bar; | |
} | |
private ImmutableFoo(ImmutableFoo original, ImmutableBar bar) { | |
this.bar = bar; | |
} | |
/** | |
* {@inheritDoc} | |
* @return value of {@code bar} attribute | |
*/ | |
@Override | |
public ImmutableBar bar() { | |
return bar; | |
} | |
/** | |
* Copy current immutable object by setting value for {@link Foo#bar() bar}. | |
* Shallow reference equality check is used to prevent copying of the same value by returning {@code this}. | |
* @param value new value for bar | |
* @return modified copy of the {@code this} object | |
*/ | |
public final ImmutableFoo withBar(ImmutableBar value) { | |
if (this.bar == value) { | |
return this; | |
} | |
ImmutableBar newValue = Objects.requireNonNull(value); | |
return new ImmutableFoo(this, newValue); | |
} | |
/** | |
* This instance is equal to instances of {@code ImmutableFoo} with equal attribute values. | |
* @return {@code true} if {@code this} is equal to {@code another} instance | |
*/ | |
@Override | |
public boolean equals(Object another) { | |
return this == another | |
|| (another instanceof ImmutableFoo && equalTo((ImmutableFoo) another)); | |
} | |
private boolean equalTo(ImmutableFoo another) { | |
return bar.equals(another.bar); | |
} | |
/** | |
* Computes hash code from attributes: {@code bar}. | |
* @return hashCode value | |
*/ | |
@Override | |
public int hashCode() { | |
int h = 31; | |
h = h * 17 + bar.hashCode(); | |
return h; | |
} | |
/** | |
* Prints immutable value {@code Foo{...}} with attribute values, | |
* excluding any non-generated and auxiliary attributes. | |
* @return string representation of value | |
*/ | |
@Override | |
public String toString() { | |
return new StringBuilder("Foo{") | |
.append("bar=").append(bar) | |
.append('}').toString(); | |
} | |
/** | |
* Creates immutable copy of {@link Foo}. | |
* Uses accessors to get values to initialize immutable instance. | |
* If an instance is already immutable, it is returned as is. | |
* @return copied immutable Foo instance | |
*/ | |
public static ImmutableFoo copyOf(Foo instance) { | |
if (instance instanceof ImmutableFoo) { | |
return (ImmutableFoo) instance; | |
} | |
return ImmutableFoo.builder() | |
.from(instance) | |
.build(); | |
} | |
/** | |
* Creates builder for {@link foo.ImmutableFoo}. | |
* @return new ImmutableFoo builder | |
*/ | |
public static ImmutableFoo.Builder builder() { | |
return new ImmutableFoo.Builder(); | |
} | |
/** | |
* Builds instances of {@link foo.ImmutableFoo}. | |
* Initialized attributes and then invoke {@link #build()} method to create | |
* immutable instance. | |
* <p><em>Builder is not thread safe and generally should not be stored in field or collection, | |
* but used immediately to create instances.</em> | |
*/ | |
public static final class Builder { | |
private static final long INITIALIZED_BITSET_ALL = 0x1; | |
private static final long INITIALIZED_BIT_BAR = 0x1L; | |
private long initializedBitset; | |
private ImmutableBar bar; | |
private Builder() {} | |
/** | |
* Adjust builder with values from provided {@link Foo} instance. | |
* Regular attribute values will be overridden, i.e. replaced with ones of an instance. | |
* Instance's absent optional values will not be copied (will not override current). | |
* Collection elements and entries will be added, not replaced. | |
* @param instance instance to copy values from | |
* @return {@code this} builder for chained invocation | |
*/ | |
public final Builder from(Foo instance) { | |
Objects.requireNonNull(instance); | |
bar(instance.bar()); | |
return this; | |
} | |
/** | |
* Initializes value for {@link Foo#bar() bar}. | |
* @param bar value for bar | |
* @return {@code this} builder for chained invocation | |
*/ | |
public final Builder bar(ImmutableBar bar) { | |
this.bar = Objects.requireNonNull(bar); | |
initializedBitset |= INITIALIZED_BIT_BAR; | |
return this; | |
} | |
/** | |
* Builds new {@link foo.ImmutableFoo}. | |
* @return immutable instance of Foo | |
*/ | |
public ImmutableFoo build() { | |
checkRequiredAttributes(); | |
return new ImmutableFoo(this); | |
} | |
private boolean barIsSet() { | |
return (initializedBitset & INITIALIZED_BIT_BAR) != 0; | |
} | |
private void checkRequiredAttributes() { | |
if (initializedBitset != INITIALIZED_BITSET_ALL) { | |
throw new IllegalStateException(formatRequiredAttributesMessage()); | |
} | |
} | |
private String formatRequiredAttributesMessage() { | |
Collection<String> attributes = new ArrayList<String>(); | |
if (!barIsSet()) { | |
attributes.add("bar"); | |
} | |
return "Cannot build Foo, some of required attributes are not set " + attributes; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment