Skip to content

Commit

Permalink
Fix mkarneim#154 - Fields using static inner classes with same name g…
Browse files Browse the repository at this point in the history
…enerate overlapping imports
  • Loading branch information
Adrodoc committed Dec 29, 2018
1 parent c4f7948 commit 0d1cc69
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 17 deletions.
3 changes: 0 additions & 3 deletions src/main/java/net/karneim/pojobuilder/model/ImportTypesM.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,4 @@ public void removePackage(String packageName) {
}
}
}



}
42 changes: 29 additions & 13 deletions src/main/java/net/karneim/pojobuilder/model/TypeM.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,43 @@
package net.karneim.pojobuilder.model;

import java.util.ArrayList;
import java.util.List;

public class TypeM {
private final String packageName;
private final String simpleName;
private final List<String> simpleNames;
private final String name;
private final TypeListM typeParameters = new TypeListM();

protected TypeM(String name) {
this("", name);
}

public TypeM(String packageName, String simpleName) {
public TypeM(String packageName, String simpleNames) {
if (packageName == null) {
this.packageName = "";
} else {
this.packageName = packageName;
packageName = "";
}
this.simpleName = simpleName;
this.packageName = packageName;
this.simpleNames = split(simpleNames, '.');
if (this.packageName.isEmpty()) {
this.name = simpleName;
this.name = simpleNames;
} else {
this.name = this.packageName + "." + this.simpleName;
this.name = this.packageName + "." + simpleNames;
}
}

private static List<String> split(String string, char seperator) {
ArrayList<String> result = new ArrayList<>();
int lastSplit = -1;
for (int i = 0; i < string.length(); i++) {
if (string.charAt(i) == seperator) {
result.add(string.substring(lastSplit + 1, i));
lastSplit = i;
}
}
result.add(string.substring(lastSplit + 1, string.length()));
result.trimToSize();
return result;
}

public TypeM(Class<?> aClass) {
Expand All @@ -40,7 +56,7 @@ public TypeListM getTypeParameters() {
}

public String getSimpleName() {
return simpleName;
return simpleNames.get(simpleNames.size() - 1);
}

public String getName() {
Expand Down Expand Up @@ -127,7 +143,7 @@ public int hashCode() {
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((packageName == null) ? 0 : packageName.hashCode());
result = prime * result + ((simpleName == null) ? 0 : simpleName.hashCode());
result = prime * result + ((simpleNames == null) ? 0 : simpleNames.hashCode());
return result;
}

Expand All @@ -150,10 +166,10 @@ public boolean equals(Object obj) {
return false;
} else if (!packageName.equals(other.packageName))
return false;
if (simpleName == null) {
if (other.simpleName != null)
if (simpleNames == null) {
if (other.simpleNames != null)
return false;
} else if (!simpleName.equals(other.simpleName))
} else if (!simpleNames.equals(other.simpleNames))
return false;
if (typeParameters == null) {
if (other.typeParameters != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import net.karneim.pojobuilder.processor.AnnotationProcessor;
import net.karneim.pojobuilder.processor.with.ProcessorTestSupport;
import net.karneim.pojobuilder.processor.with.ambiguousimports.innerclasses.PojoWithAmbiguousInnerClassImports;
import net.karneim.pojobuilder.processor.with.ambiguousimports.innerclasses.PojoWithAmbiguousInnerClassImportsBuilder;
import net.karneim.pojobuilder.testenv.JavaProject.Compilation;
import org.junit.Test;

Expand All @@ -17,7 +19,7 @@ public class AnnotationProcessor_Import_Test extends ProcessorTestSupport {
* @scenario the builder is created with appropriate import statements.
*/
@Test
public void testShouldGenerateBuilderWithAppropriateInportProperties() {
public void testShouldGenerateBuilderWithAppropriateImportProperties() {
// Given:
sourceFor(Pojo.class);
// When:
Expand All @@ -29,4 +31,22 @@ public void testShouldGenerateBuilderWithAppropriateInportProperties() {
.reported(Compilation.Success);
}

/**
* Test for issue <a href="https://github.com/mkarneim/pojobuilder/issues/154">#154</a>.
*/
@Test
public void test_ambiguous_inner_class_imports() {
// given:
sourceFor(PojoWithAmbiguousInnerClassImports.class);

// when:
prj.compile();

// then:
assertThat(prj)
.generatedSameSourceAs(PojoWithAmbiguousInnerClassImportsBuilder.class)
.compiled(PojoWithAmbiguousInnerClassImportsBuilder.class)
.reported(Compilation.Success);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.karneim.pojobuilder.processor.with.ambiguousimports.innerclasses;

import net.karneim.pojobuilder.GeneratePojoBuilder;

@GeneratePojoBuilder
public class PojoWithAmbiguousInnerClassImports {
public net.karneim.pojobuilder.processor.with.ambiguousimports.innerclasses.pack1.Outer1.Inner field1;
public net.karneim.pojobuilder.processor.with.ambiguousimports.innerclasses.pack2.Outer2.Inner field2;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package net.karneim.pojobuilder.processor.with.ambiguousimports.innerclasses;

import javax.annotation.processing.Generated;
import net.karneim.pojobuilder.GwtIncompatible;
import net.karneim.pojobuilder.processor.with.ambiguousimports.innerclasses.pack1.Outer1.Inner;

@Generated("PojoBuilder")
public class PojoWithAmbiguousInnerClassImportsBuilder
implements Cloneable {
protected PojoWithAmbiguousInnerClassImportsBuilder self;
protected Inner value$field1$net$karneim$pojobuilder$processor$with$ambiguousimports$innerclasses$pack1$Outer1$Inner;
protected boolean isSet$field1$net$karneim$pojobuilder$processor$with$ambiguousimports$innerclasses$pack1$Outer1$Inner;
protected net.karneim.pojobuilder.processor.with.ambiguousimports.innerclasses.pack2.Outer2.Inner value$field2$net$karneim$pojobuilder$processor$with$ambiguousimports$innerclasses$pack2$Outer2$Inner;
protected boolean isSet$field2$net$karneim$pojobuilder$processor$with$ambiguousimports$innerclasses$pack2$Outer2$Inner;

/**
* Creates a new {@link PojoWithAmbiguousInnerClassImportsBuilder}.
*/
public PojoWithAmbiguousInnerClassImportsBuilder() {
self = (PojoWithAmbiguousInnerClassImportsBuilder)this;
}

/**
* Sets the default value for the {@link PojoWithAmbiguousInnerClassImports#field1} property.
*
* @param value the default value
* @return this builder
*/
public PojoWithAmbiguousInnerClassImportsBuilder withField1(Inner value) {
this.value$field1$net$karneim$pojobuilder$processor$with$ambiguousimports$innerclasses$pack1$Outer1$Inner = value;
this.isSet$field1$net$karneim$pojobuilder$processor$with$ambiguousimports$innerclasses$pack1$Outer1$Inner = true;
return self;
}

/**
* Sets the default value for the {@link PojoWithAmbiguousInnerClassImports#field2} property.
*
* @param value the default value
* @return this builder
*/
public PojoWithAmbiguousInnerClassImportsBuilder withField2(net.karneim.pojobuilder.processor.with.ambiguousimports.innerclasses.pack2.Outer2.Inner value) {
this.value$field2$net$karneim$pojobuilder$processor$with$ambiguousimports$innerclasses$pack2$Outer2$Inner = value;
this.isSet$field2$net$karneim$pojobuilder$processor$with$ambiguousimports$innerclasses$pack2$Outer2$Inner = true;
return self;
}

/**
* Returns a clone of this builder.
*
* @return the clone
*/
@Override
@GwtIncompatible
public Object clone() {
try {
PojoWithAmbiguousInnerClassImportsBuilder result = (PojoWithAmbiguousInnerClassImportsBuilder)super.clone();
result.self = result;
return result;
} catch (CloneNotSupportedException e) {
throw new InternalError(e.getMessage());
}
}

/**
* Returns a clone of this builder.
*
* @return the clone
*/
@GwtIncompatible
public PojoWithAmbiguousInnerClassImportsBuilder but() {
return (PojoWithAmbiguousInnerClassImportsBuilder)clone();
}

/**
* Creates a new {@link PojoWithAmbiguousInnerClassImports} based on this builder's settings.
*
* @return the created PojoWithAmbiguousInnerClassImports
*/
public PojoWithAmbiguousInnerClassImports build() {
try {
PojoWithAmbiguousInnerClassImports result = new PojoWithAmbiguousInnerClassImports();
if (isSet$field1$net$karneim$pojobuilder$processor$with$ambiguousimports$innerclasses$pack1$Outer1$Inner) {
result.field1 = value$field1$net$karneim$pojobuilder$processor$with$ambiguousimports$innerclasses$pack1$Outer1$Inner;
}
if (isSet$field2$net$karneim$pojobuilder$processor$with$ambiguousimports$innerclasses$pack2$Outer2$Inner) {
result.field2 = value$field2$net$karneim$pojobuilder$processor$with$ambiguousimports$innerclasses$pack2$Outer2$Inner;
}
return result;
} catch (RuntimeException ex) {
throw ex;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.karneim.pojobuilder.processor.with.ambiguousimports.innerclasses.pack1;

public class Outer1 {
public static class Inner {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.karneim.pojobuilder.processor.with.ambiguousimports.innerclasses.pack2;

public class Outer2 {
public static class Inner {

}
}

0 comments on commit 0d1cc69

Please sign in to comment.