Skip to content

Commit

Permalink
Correctly resolve imports for attribute types inherited from traits in
Browse files Browse the repository at this point in the history
different files and even packages #145
  • Loading branch information
augustotravillio committed Jul 30, 2015
1 parent 1dc4b2c commit 6f83547
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 54 deletions.
9 changes: 9 additions & 0 deletions value-fixture/src/org/immutables/fixture/couse/Trait.java
@@ -0,0 +1,9 @@
package org.immutables.fixture.couse;

import org.immutables.fixture.couse.sub.C;

public interface Trait {
B b();

C c();
}
@@ -0,0 +1,8 @@
package org.immutables.fixture.couse.sub2;

import org.immutables.value.Value;
import org.immutables.fixture.couse.Trait;

// Test import resolution in generated type Z for attributes inherited from Trait
@Value.Immutable
public interface AbstractZ extends Trait {}
Expand Up @@ -980,12 +980,6 @@ public Constitution constitution() {
.protoclass(this)
.build();
}

SourceExtraction.Imports sourceImports() {
return declaringType().isPresent()
? declaringType().get().associatedTopLevel().sourceImports()
: SourceExtraction.Imports.empty();
}
}

enum ElementToName implements Function<TypeElement, String> {
Expand Down
47 changes: 23 additions & 24 deletions value-processor/src/org/immutables/value/processor/meta/Round.java
Expand Up @@ -150,6 +150,27 @@ private void checkAnnotation(TypeElement annotation, Set<? extends Element> anno
}
}

TypeElement wrapElement(TypeElement element) {
return CachingElements.asCaching(element);
}

ExecutableElement wrapElement(ExecutableElement element) {
return CachingElements.asCaching(element);
}

PackageElement wrapElement(PackageElement element) {
return CachingElements.asCaching(element);
}

DeclaringType declaringTypeFrom(TypeElement element) {
return interners.forType(
ImmutableProto.DeclaringType.builder()
.environment(environment())
.interner(interners)
.element(wrapElement(element))
.build());
}

private class ProtoclassCollecter {
final ImmutableList.Builder<Protoclass> builder = ImmutableList.builder();

Expand Down Expand Up @@ -181,12 +202,7 @@ void collect(Element element) {
}

void collectDefinedBy(ExecutableElement element) {
DeclaringType declaringType = interners.forType(
ImmutableProto.DeclaringType.builder()
.environment(environment())
.interner(interners)
.element(wrapElement((TypeElement) element.getEnclosingElement()))
.build());
DeclaringType declaringType = declaringTypeFrom((TypeElement) element.getEnclosingElement());

if (declaringType.verifiedFactory(element)) {
builder.add(interners.forProto(ImmutableProto.Protoclass.builder()
Expand Down Expand Up @@ -220,12 +236,7 @@ void collectIncludedBy(PackageElement element) {
}

void collectIncludedAndDefinedBy(TypeElement element) {
DeclaringType declaringType = interners.forType(
ImmutableProto.DeclaringType.builder()
.environment(environment())
.interner(interners)
.element(wrapElement(element))
.build());
DeclaringType declaringType = declaringTypeFrom(element);

if (declaringType.hasInclude()) {
Kind kind = declaringType.isEnclosing()
Expand Down Expand Up @@ -278,16 +289,4 @@ private Kind kindOfDefinedBy(DeclaringType declaringType) {
return Kind.DEFINED_ENCLOSING_TYPE;
}
}

public TypeElement wrapElement(TypeElement element) {
return CachingElements.asCaching(element);
}

public ExecutableElement wrapElement(ExecutableElement element) {
return CachingElements.asCaching(element);
}

public PackageElement wrapElement(PackageElement element) {
return CachingElements.asCaching(element);
}
}
Expand Up @@ -21,14 +21,13 @@
import org.immutables.generator.AnnotationMirrors;
import org.immutables.generator.SourceExtraction;
import org.immutables.generator.SourceTypes;
import org.immutables.value.processor.meta.Proto.Protoclass;

/**
* Encapsulates routines for get relevant strings for the raw types and type parameters,
* while attempting to resolve unresolved types using source imports.
*/
class TypeStringProvider {
private final Protoclass protoclass;
private final ValueAttribute attribute;
private final TypeMirror startType;
private final Element element;
private final List<String> typeParameterStrings = Lists.newArrayListWithCapacity(2);
Expand All @@ -45,10 +44,10 @@ class TypeStringProvider {
@Nullable
private String workaroundTypeString;

TypeStringProvider(TypeMirror returnType, Protoclass protoclass, Element element) {
this.startType = returnType;
this.protoclass = protoclass;
this.element = element;
TypeStringProvider(ValueAttribute attribute) {
this.attribute = attribute;
this.startType = attribute.returnType;
this.element = attribute.element;
}

String rawTypeName() {
Expand Down Expand Up @@ -122,7 +121,8 @@ private String resolveIfPossible(String typeName) {
if (indexOfDot > 0) {
resolvable = resolvable.substring(0, indexOfDot);
}
@Nullable String resolved = getFromSourceImports(resolvable);
@Nullable
String resolved = getFromSourceImports(resolvable);
if (resolved != null) {
if (indexOfDot > 0) {
typeName = resolved + '.' + resolvable.substring(indexOfDot + 1);
Expand All @@ -138,7 +138,9 @@ private String resolveIfPossible(String typeName) {
@Nullable
private String getFromSourceImports(String resolvable) {
if (sourceClassesImports == null) {
sourceClassesImports = protoclass.sourceImports().classes;
sourceClassesImports = attribute.getDeclaringType()
.associatedTopLevel()
.sourceImports().classes;
}
return sourceClassesImports.get(resolvable);
}
Expand Down Expand Up @@ -226,8 +228,10 @@ void caseType(TypeMirror type) {
break;
case WILDCARD:
WildcardType wildcard = (WildcardType) type;
@Nullable TypeMirror extendsBound = wildcard.getExtendsBound();
@Nullable TypeMirror superBound = wildcard.getSuperBound();
@Nullable

This comment has been minimized.

Copy link
@elucash

elucash Jul 31, 2015

Member

@augustotravillio fix auto-formatter settings?

TypeMirror extendsBound = wildcard.getExtendsBound();
@Nullable
TypeMirror superBound = wildcard.getSuperBound();
if (extendsBound != null) {
buffer.append("? extends ");
caseType(extendsBound);
Expand All @@ -247,8 +251,7 @@ void caseType(TypeMirror type) {
break;
}

protoclass.report()
.withElement(element)
attribute.report()
.error("It is a compiler/annotation processing bug to receive type variables '%s' here."
+ " To avoid it — do not use not yet generated types in %s attribute",
type,
Expand Down
Expand Up @@ -15,6 +15,8 @@
*/
package org.immutables.value.processor.meta;

import java.util.NoSuchElementException;
import org.immutables.value.processor.meta.Proto.DeclaringType;
import com.google.common.base.MoreObjects;
import com.google.common.base.Optional;
import com.google.common.base.Splitter;
Expand Down Expand Up @@ -384,7 +386,7 @@ public boolean isGenerateEnumMap() {
public String getUnwrappedElementType() {
return isContainerType() ? unwrapType(containmentTypeName()) : getElementType();
}

public String getUnwrappedValueElementType() {
return isMapType()
? getUnwrappedSecondaryElementType()
Expand Down Expand Up @@ -760,10 +762,7 @@ private void prohibitAuxiliaryOnAnnotationTypes() {
}

private void initTypeName() {
TypeStringProvider provider = new TypeStringProvider(
returnType,
containingType.constitution.protoclass(),
element);
TypeStringProvider provider = new TypeStringProvider(this);

provider.process();

Expand All @@ -784,6 +783,22 @@ private void initTypeKind() {
}
}

DeclaringType getDeclaringType() {
@Nullable
TypeElement declaringType = null;
for (Element e = element; e != null;) {
e = e.getEnclosingElement();
if (e instanceof TypeElement) {
declaringType = (TypeElement) e;
break;
}
}
if (declaringType == null) {
throw new NoSuchElementException();
}
return containingType.round.declaringTypeFrom(declaringType);
}

private void makeRegularIfDefaultWithValidation() {
if (isGenerateDefault && isContainerType()) {
typeKind = AttributeTypeKind.REGULAR;
Expand Down
Expand Up @@ -746,15 +746,34 @@ private boolean useCollectionUtility(Predicate<ValueAttribute> predicate) {
return Iterables.any(getSettableAttributes(), predicate);
}

public List<String> getRequiredSourceStarImports() {
public Set<String> getRequiredSourceStarImports() {
if (!hasSomeUnresolvedTypes()) {
return Collections.emptyList();
return Collections.emptySet();
}
SourceExtraction.Imports sourceImports = constitution.protoclass().sourceImports();
List<String> starImports = Lists.newArrayList();
for (String importStatement : sourceImports.all) {
if (importStatement.indexOf('*') > 0) {
starImports.add(importStatement);
Set<String> starImports = Sets.newLinkedHashSet();

for (ValueType n : FluentIterable.from(nested).append(this)) {
for (ValueAttribute a : n.attributes) {
if (a.hasSomeUnresolvedTypes) {
DeclaringType topLevel = a.getDeclaringType().associatedTopLevel();

SourceExtraction.Imports sourceImports =
topLevel.sourceImports();

for (String importStatement : sourceImports.all) {
if (importStatement.indexOf('*') > 0) {
starImports.add(importStatement);
}
}

if (!topLevel.packageOf().equals(constitution.protoclass().packageOf())) {
String prefix = topLevel.packageOf().asPrefix();
// guard against unnamed packages
if (!prefix.isEmpty()) {
starImports.add(prefix + '*');
}
}
}
}
}
return starImports;
Expand Down

0 comments on commit 6f83547

Please sign in to comment.