Skip to content

Commit

Permalink
Added generic collection matching to support better validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
johncarl81 committed Nov 10, 2014
1 parent b9e20b8 commit 8b3c092
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
10 changes: 6 additions & 4 deletions README.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,18 @@ types:
* +String+
* +IBinder+
* +Bundle+
* +SparseArray+ of any of the mapped types
* +SparseArray+ of any of the mapped types*
* +SparseBooleanArray+
* +List+ of any of the mapped types
* +Map+ of any of the mapped types
* +Set+ of any of the mapped types
* +List+ of any of the mapped types*
* +Map+ of any of the mapped types*
* +Set+ of any of the mapped types*
* +Parcelable+
* +Serializable+
* Array of any of the mapped types
* Any other class annotated with +@Parcel+

*Parcel will error if the generic parameter is not mapped.

Parceler may be configured to serialize using getter and setter methods and a non-empty constructor.
In addition, fields, methods and constructor parameters may be associated using the +@ParcelParameter+ annotation.
This supports a number of bean strategies including immutability and traditional getter/setter beans.
Expand Down
13 changes: 7 additions & 6 deletions parceler/src/main/java/org/parceler/internal/ParcelerModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.parceler.Generated;
import org.parceler.ParcelAnnotationProcessor;
import org.parceler.internal.generator.*;
import org.parceler.internal.matcher.GenericCollectionMatcher;
import org.parceler.internal.matcher.ParcelMatcher;

import javax.annotation.processing.Filer;
Expand Down Expand Up @@ -223,12 +224,12 @@ public static Generators addGenerators(Generators generators,
generators.add(new ImplementsMatcher(new ASTStringType("android.os.Parcelable")), new ParcelableReadWriteGenerator("readParcelable", "writeParcelable", "android.os.Parcelable"));
generators.add(new ParcelMatcher(externalParcelRepository), new ParcelReadWriteGenerator(generationUtil, analysis, generator, namer));
generators.add(new ASTArrayMatcher(), new ArrayReadWriteGenerator(generationUtil, namer, generators, codeModel));
generators.add(Matchers.type(astClassFactory.getType(List.class)).ignoreGenerics().build(), new ListReadWriteGenerator(generationUtil, namer, generators, astClassFactory, codeModel));
generators.add(Matchers.type(astClassFactory.getType(ArrayList.class)).ignoreGenerics().build(), new ListReadWriteGenerator(generationUtil, namer, generators, astClassFactory, codeModel));
generators.add(Matchers.type(astClassFactory.getType(Map.class)).ignoreGenerics().build(), new MapReadWriteGenerator(generationUtil, namer, generators, astClassFactory, codeModel));
generators.add(Matchers.type(astClassFactory.getType(HashMap.class)).ignoreGenerics().build(), new MapReadWriteGenerator(generationUtil, namer, generators, astClassFactory, codeModel));
generators.add(Matchers.type(astClassFactory.getType(Set.class)).ignoreGenerics().build(), new SetReadWriteGenerator(generationUtil, namer, generators, astClassFactory, codeModel));
generators.add(Matchers.type(astClassFactory.getType(HashSet.class)).ignoreGenerics().build(), new SetReadWriteGenerator(generationUtil, namer, generators, astClassFactory, codeModel));
generators.add(new GenericCollectionMatcher(astClassFactory.getType(List.class), generators, 1), new ListReadWriteGenerator(generationUtil, namer, generators, astClassFactory, codeModel));
generators.add(new GenericCollectionMatcher(astClassFactory.getType(ArrayList.class), generators, 1), new ListReadWriteGenerator(generationUtil, namer, generators, astClassFactory, codeModel));
generators.add(new GenericCollectionMatcher(astClassFactory.getType(Map.class), generators, 2), new MapReadWriteGenerator(generationUtil, namer, generators, astClassFactory, codeModel));
generators.add(new GenericCollectionMatcher(astClassFactory.getType(HashMap.class), generators, 2), new MapReadWriteGenerator(generationUtil, namer, generators, astClassFactory, codeModel));
generators.add(new GenericCollectionMatcher(astClassFactory.getType(Set.class), generators, 1), new SetReadWriteGenerator(generationUtil, namer, generators, astClassFactory, codeModel));
generators.add(new GenericCollectionMatcher(astClassFactory.getType(HashSet.class), generators, 1), new SetReadWriteGenerator(generationUtil, namer, generators, astClassFactory, codeModel));
generators.add(new InheritsMatcher(astClassFactory.getType(Serializable.class)), serializableReadWriteGenerator);

return generators;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.parceler.internal.matcher;

import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import org.androidtransfuse.adapter.ASTType;
import org.androidtransfuse.util.matcher.Matcher;
import org.androidtransfuse.util.matcher.Matchers;
import org.parceler.internal.Generators;

/**
* Matches both the type of a collection and the
*
* @author John Ericksen
*/
public class GenericCollectionMatcher implements Matcher<ASTType> {

private final Matcher<ASTType> collectionMatcher;
private final Generators generators;
private final int parameterCount;

public GenericCollectionMatcher(ASTType collectionType, Generators generators, int parameterCount) {
this.collectionMatcher = Matchers.type(collectionType).ignoreGenerics().build();
this.generators = generators;
this.parameterCount = parameterCount;
}

@Override
public boolean matches(ASTType input) {

return collectionMatcher.matches(input) &&
input.getGenericParameters().size() == parameterCount &&
FluentIterable.from(input.getGenericParameters()).allMatch(new Predicate<ASTType>() {
public boolean apply(ASTType astType) {
return generators.matches(astType);
}
});
}
}

0 comments on commit 8b3c092

Please sign in to comment.