From 21ca36645d4d37a58f09c23121b797baf5b8f237 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jyri-Matti=20L=C3=A4hteenm=C3=A4ki?= Date: Wed, 25 Feb 2015 12:16:24 +0200 Subject: [PATCH] new functional-utils --- pom.xml | 4 +- .../utils/meta/CommonMetadataProcessor.java | 36 ++++----- .../java/fi/solita/utils/meta/Helpers.java | 65 ++++++++-------- .../solita/utils/meta/MetaConstructors.java | 10 +-- .../fi/solita/utils/meta/MetaMethods.java | 6 +- .../generators/ConstructorsAsFunctions.java | 45 ++++++----- .../solita/utils/meta/generators/Content.java | 34 ++++----- .../meta/generators/InstanceFieldsAsEnum.java | 10 +-- .../generators/InstanceFieldsAsFunctions.java | 40 +++++----- .../generators/InstanceFieldsAsTuple.java | 19 +++-- .../meta/generators/MethodsAsFunctions.java | 75 +++++++++---------- 11 files changed, 172 insertions(+), 172 deletions(-) diff --git a/pom.xml b/pom.xml index 68a5da3..9c52271 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 fi.solita.utils meta-utils - 0.8.3 + 0.9 @@ -36,7 +36,7 @@ fi.solita.utils functional-utils - 0.8.1 + 0.9 provided diff --git a/src/main/java/fi/solita/utils/meta/CommonMetadataProcessor.java b/src/main/java/fi/solita/utils/meta/CommonMetadataProcessor.java index 483664d..e9f82d1 100644 --- a/src/main/java/fi/solita/utils/meta/CommonMetadataProcessor.java +++ b/src/main/java/fi/solita/utils/meta/CommonMetadataProcessor.java @@ -10,7 +10,7 @@ import static fi.solita.utils.meta.Helpers.qualifiedName; import static fi.solita.utils.meta.Helpers.removeGenericPart; import static fi.solita.utils.meta.Helpers.simpleName; -import static fi.solita.utils.meta.Helpers.withAnnotation; +import static fi.solita.utils.meta.Helpers.withAnnotations; import static fi.solita.utils.functional.Collections.emptyList; import static fi.solita.utils.functional.Collections.newArray; import static fi.solita.utils.functional.Collections.newList; @@ -20,6 +20,7 @@ import static fi.solita.utils.functional.Functional.find; import static fi.solita.utils.functional.Functional.flatMap; import static fi.solita.utils.functional.Functional.map; +import static fi.solita.utils.functional.Functional.mkString; import static fi.solita.utils.functional.Functional.sequence; import static fi.solita.utils.functional.Functional.transpose; import static fi.solita.utils.functional.Option.Some; @@ -52,6 +53,7 @@ import fi.solita.utils.meta.generators.InstanceFieldsAsTuple; import fi.solita.utils.meta.generators.MethodsAsFunctions; import fi.solita.utils.functional.Apply; +import fi.solita.utils.functional.Function; import fi.solita.utils.functional.Function1; import fi.solita.utils.functional.Option; import fi.solita.utils.functional.Pair; @@ -96,7 +98,7 @@ protected boolean enabledByDefault() { public String generatedClassNamePattern() { return findOption(Options.generatedClassNamePattern, "{}_"); } public String generatedPackagePattern() { return findOption(Options.generatedPackagePattern, "{}"); } public String includesAnnotation() { return findOption(Options.includesAnnotation, ""); } - public String excludesAnnotation() { return findOption(Options.excludesAnnotation, NoMetadataGeneration.class.getName()); } + public String excludesAnnotation() { return findOption(Options.excludesAnnotation, mkString(",", newList("javax.persistence.Entity", "javax.persistence.MappedSuperclass", "javax.persistence.Embeddable", NoMetadataGeneration.class.getName()))); } public Pattern extendClassNamePattern() { return Pattern.compile(""); } public String findOption(String option, String defaultIfNotFound) { @@ -105,8 +107,8 @@ public String findOption(String option, String defaultIfNotFound) { public Predicate elementsToProcess() { return Helpers.instanceOf(TypeElement.class).and( nonGeneratedElements).and( - (includeAllByAnnotation.or(withAnnotation(includesAnnotation())))).and( - not(withAnnotation(excludesAnnotation()))).and( + (includeAllByAnnotation.or(withAnnotations(includesAnnotation())))).and( + not(withAnnotations(excludesAnnotation()))).and( qualifiedName.andThen(matches(includesRegex()).and( not(matches(excludesRegex()))))).and( simpleName.andThen(not(equalTo("package-info")))); } @@ -153,7 +155,7 @@ public Apply>> transform(final Apply> apply(TypeElement t) { long start = System.nanoTime(); - List result = newList(map(source.apply(t), padding)); + List result = newList(map(padding, source.apply(t))); return Pair.of(System.nanoTime() - start, result); } }; @@ -180,7 +182,7 @@ public boolean doProcess(Set annotations, RoundEnvironmen for (Generator g: generators()) { generators.add(g.ap(processingEnv, options)); } - List>>> generatorsWithTiming = newList(map(generators, timed)); + List>>> generatorsWithTiming = newList(map(timed, generators)); String genClassNamePat = generatedClassNamePattern(); String genPackagePat = generatedPackagePattern(); @@ -196,23 +198,23 @@ public boolean doProcess(Set annotations, RoundEnvironmen long[] cumulativeGeneratorTimes = new long[generators.size()]; Predicate acceptedElement = elementsToProcess(); @SuppressWarnings("unchecked") - List elementsToProcess = newList((Iterable)filter(roundEnv.getRootElements(), acceptedElement)); + List elementsToProcess = newList((Iterable)filter(acceptedElement, roundEnv.getRootElements())); if (options.onlyPublicMembers()) { - elementsToProcess = newList(filter(elementsToProcess, publicElement)); + elementsToProcess = newList(filter(publicElement, elementsToProcess)); } - Apply, List>> nestedDataProducer = Content.withNestedClasses.ap(options, genClassNamePat, acceptedElement, generatorsWithTiming); + Apply, List>> nestedDataProducer = Content.withNestedClasses.ap(options, genClassNamePat, Predicate.of(Function.constant(true)), generatorsWithTiming); for (TypeElement element: elementsToProcess) { try { long time = System.nanoTime(); - List>> elemData = newList(sequence(generatorsWithTiming, element)); + List>> elemData = newList(sequence(element, generatorsWithTiming)); long time2 = System.nanoTime(); Iterable nestedToProcess = element2NestedClasses.apply(element); if (options.onlyPublicMembers()) { - nestedToProcess = newList(filter(nestedToProcess, publicElement)); + nestedToProcess = newList(filter(publicElement, nestedToProcess)); } - List, List>> nestedData = newList(map(filter(nestedToProcess, acceptedElement), nestedDataProducer)); + List, List>> nestedData = newList(map(nestedDataProducer, filter(Predicate.of(Function.constant(true)), nestedToProcess))); long time3 = System.nanoTime(); - Iterable content = map(concat(flatMap(elemData, Helpers.>right()), flatMap(nestedData, Helpers.>right())), padding); + Iterable content = map(padding, concat(flatMap(Helpers.>right(), elemData), flatMap(Helpers.>right(), nestedData))); String genPackage = genPackagePat.replace("{}", getPackageName(element)); String genClassName = genClassNamePat.replace("{}", element.getSimpleName().toString()); @@ -226,9 +228,9 @@ public boolean doProcess(Set annotations, RoundEnvironmen rest += time4 - time3; fileWriting += System.nanoTime() - time4; - Iterable generatorTimesForContent = map(elemData, Helpers.left()); - Iterable> generatorTimesForNestedClasses = map(nestedData, Helpers.>left()); - Long[] totalTimesPerGenerator = newArray(Long.class, map(transpose(cons(generatorTimesForContent, generatorTimesForNestedClasses)), Helpers.iterableSum)); + Iterable generatorTimesForContent = map(Helpers.left(), elemData); + Iterable> generatorTimesForNestedClasses = map(Helpers.>left(), nestedData); + Long[] totalTimesPerGenerator = newArray(Long.class, map(Helpers.iterableSum, transpose(cons(generatorTimesForContent, generatorTimesForNestedClasses)))); for (int i = 0; i < cumulativeGeneratorTimes.length; ++i) { cumulativeGeneratorTimes[i] += totalTimesPerGenerator[i]; @@ -239,7 +241,7 @@ public boolean doProcess(Set annotations, RoundEnvironmen } } if (!elementsToProcess.isEmpty()) { - processingEnv.getMessager().printMessage(Kind.WARNING, getClass().getName() + " (" + version + ") processed " + elementsToProcess.size() + " elements in " + (System.nanoTime()-started)/1000/1000 + " ms (" + generation/1000/1000 + "/" + nestedGeneration/1000/1000 + "/" + rest/1000/1000 + "/" + fileWriting/1000/1000 + " ms) (" + newList(map(newArray(cumulativeGeneratorTimes), nanosToMillis)) + " ms)"); + processingEnv.getMessager().printMessage(Kind.WARNING, getClass().getName() + " (" + version + ") processed " + elementsToProcess.size() + " elements in " + (System.nanoTime()-started)/1000/1000 + " ms (" + generation/1000/1000 + "/" + nestedGeneration/1000/1000 + "/" + rest/1000/1000 + "/" + fileWriting/1000/1000 + " ms) (" + newList(map(nanosToMillis, newArray(cumulativeGeneratorTimes))) + " ms)"); } return false; } diff --git a/src/main/java/fi/solita/utils/meta/Helpers.java b/src/main/java/fi/solita/utils/meta/Helpers.java index e14f25d..06aed08 100644 --- a/src/main/java/fi/solita/utils/meta/Helpers.java +++ b/src/main/java/fi/solita/utils/meta/Helpers.java @@ -4,14 +4,14 @@ import static fi.solita.utils.functional.Collections.newMap; import static fi.solita.utils.functional.Collections.newSet; import static fi.solita.utils.functional.Functional.concat; +import static fi.solita.utils.functional.Functional.exists; +import static fi.solita.utils.functional.Functional.filter; import static fi.solita.utils.functional.Functional.head; +import static fi.solita.utils.functional.Functional.map; +import static fi.solita.utils.functional.Functional.reduce; import static fi.solita.utils.functional.Functional.zip; import static fi.solita.utils.functional.FunctionalC.reverse; -import static fi.solita.utils.functional.FunctionalImpl.exists; -import static fi.solita.utils.functional.FunctionalImpl.filter; -import static fi.solita.utils.functional.FunctionalImpl.groupBy; -import static fi.solita.utils.functional.FunctionalImpl.map; -import static fi.solita.utils.functional.FunctionalImpl.reduce; +import static fi.solita.utils.functional.FunctionalM.groupBy; import static fi.solita.utils.functional.Predicates.not; import static fi.solita.utils.functional.Transformers.prepend; @@ -110,7 +110,7 @@ public String transform(String source) { public static final Transformer typeParameter2String = new Transformer() { @Override public String transform(TypeParameterElement source) { - String bound = Functional.mkString(" & ", map(source.getBounds(), typeMirror2GenericQualifiedName)).toString(); + String bound = Functional.mkString(" & ", map(typeMirror2GenericQualifiedName, source.getBounds())).toString(); return source.getSimpleName().toString() + (bound.equals("java.lang.Object") ? "" : " extends " + bound); } }; @@ -143,11 +143,11 @@ public String transform(TypeMirror source) { if (params == null) { return type; } else { - List p = newList(filter(map(params, this), notEmpty)); + List p = newList(filter(notEmpty, map(this, params))); if (p.isEmpty()) { return type; } else { - return type + "<" + Functional.mkString(", ", map(p, boxed)) + ">"; + return type + "<" + Functional.mkString(", ", map(boxed, p)) + ">"; } } } @@ -222,7 +222,7 @@ public Element transform(Element source) { public static final Function1 handleTypeVariables(final List typeParameters) { // FIXME: How to "convert" a typeVariable to a concrete class? - final Map> bySimpleName = groupBy(typeParameters, simpleName); + final Map> bySimpleName = groupBy(simpleName, typeParameters); return new Transformer() { @Override public String transform(String source) { @@ -287,7 +287,7 @@ public String visitPackage(PackageElement e, Object p) { @SuppressWarnings("unchecked") @Override public Iterable transform(Element source) { - return (Iterable) filter(Workaround.getEnclosedElementsDeclarationOrder(source), fields); + return (Iterable) filter(fields, Workaround.getEnclosedElementsDeclarationOrder(source)); } }; @@ -295,7 +295,7 @@ public Iterable transform(Element source) { @SuppressWarnings("unchecked") @Override public Iterable transform(Element source) { - return (Iterable) filter(Workaround.getEnclosedElementsDeclarationOrder(source), methods); + return (Iterable) filter(methods, Workaround.getEnclosedElementsDeclarationOrder(source)); } }; @@ -303,7 +303,7 @@ public Iterable transform(Element source) { @SuppressWarnings("unchecked") @Override public Iterable transform(Element source) { - return (Iterable)filter(Workaround.getEnclosedElementsDeclarationOrder(source), constructors); + return (Iterable)filter(constructors, Workaround.getEnclosedElementsDeclarationOrder(source)); } }; @@ -311,7 +311,7 @@ public Iterable transform(Element source) { @SuppressWarnings("unchecked") @Override public Iterable transform(Element source) { - return (Iterable) filter(source.getEnclosedElements(), enums.or((classes.or(interfaces)).and(staticElements))); + return (Iterable) filter(enums.or((classes.or(interfaces)).and(staticElements)), source.getEnclosedElements()); } }; @@ -485,7 +485,7 @@ public final boolean isSameType(TypeMirror type, Class other) { } public final boolean throwsCheckedExceptions(ExecutableElement e) { - return exists(e.getThrownTypes(), not(uncheckedExceptions)); + return exists(not(uncheckedExceptions), e.getThrownTypes()); } } @@ -510,13 +510,13 @@ public static final Iterable allTypeParams(Execu if (staticElements.accept(e)) { return typeParameters; } - final Set typeParameterNames = newSet(map(typeParameters, simpleName)); - Iterable enclosingTypeVars = filter(((TypeElement)e.getEnclosingElement()).getTypeParameters(), simpleName.andThen(new Predicate() { + final Set typeParameterNames = newSet(map(simpleName, typeParameters)); + Iterable enclosingTypeVars = filter(simpleName.andThen(new Predicate() { @Override public boolean accept(String candidate) { return !typeParameterNames.contains(candidate); } - })); + }), ((TypeElement)e.getEnclosingElement()).getTypeParameters()); return concat(enclosingTypeVars, typeParameters); } @@ -529,23 +529,23 @@ public static final Iterable relevantTypeParams( return typeParameters; } - final Set typeParameterNames = newSet(map(typeParameters, simpleName)); - Iterable enclosingTypeVars = filter(((TypeElement)e.getEnclosingElement()).getTypeParameters(), simpleName.andThen(new Predicate() { + final Set typeParameterNames = newSet(map(simpleName, typeParameters)); + Iterable enclosingTypeVars = filter(simpleName.andThen(new Predicate() { @Override public boolean accept(String candidate) { return !typeParameterNames.contains(candidate); } - })); + }), ((TypeElement)e.getEnclosingElement()).getTypeParameters()); if (e.getKind() == ElementKind.CONSTRUCTOR) { return concat(enclosingTypeVars, typeParameters); } final Set allUsedTypeParameters = allUsedTypeParameters(e); - return concat(filter(enclosingTypeVars, new Predicate() { + return concat(filter(new Predicate() { @Override public boolean accept(TypeParameterElement candidate) { return allUsedTypeParameters.contains(candidate.getSimpleName()); } - }), typeParameters); + }, enclosingTypeVars), typeParameters); } //private static final Map patternCacheForUsedIn = newMap(); @@ -682,7 +682,7 @@ public static final String resolveVisibility(Element e) { } public static final Iterable parameterTypesAsClasses(ExecutableElement element, List relevantTypeParams) { - return map(element.getParameters(), qualifiedName.andThen(handleTypeVariables(relevantTypeParams).andThen(removeGenericPart))); + return map(qualifiedName.andThen(handleTypeVariables(relevantTypeParams).andThen(removeGenericPart)), element.getParameters()); } public static final Transformer removeGenericPart = new Transformer() { @@ -694,22 +694,23 @@ public String transform(String source) { }; public static final String elementGenericQualifiedName(TypeElement enclosingElement) { - return qualifiedName.apply(enclosingElement) + (enclosingElement.getTypeParameters().isEmpty() ? "" : "<" + Functional.mkString(", ", map(enclosingElement.getTypeParameters(), qualifiedName)) + ">"); + return qualifiedName.apply(enclosingElement) + (enclosingElement.getTypeParameters().isEmpty() ? "" : "<" + Functional.mkString(", ", map(qualifiedName, enclosingElement.getTypeParameters())) + ">"); } public static final Iterable paramsWithCast(List parameters, final boolean isPrivate) { - Iterable argCasts = map(parameters, new Transformer() { + Iterable argCasts = map(new Transformer() { @Override public String transform(VariableElement source) { return isPrivate ? "(Object)" : source.asType().getKind().isPrimitive() ? "(" + qualifiedName.apply(source) + ")" : ""; } - }); - return map(zip(argCasts, map(parameters, simpleName)), join); + }, parameters); + return map(join, zip(argCasts, map(simpleName, parameters))); } - public static final Predicate withAnnotation(final String className) { + public static final Predicate withAnnotations(final String classNames) { + final Set className = newSet(classNames.split(",")); return new Predicate() { @Override public boolean accept(final Element e) { @@ -721,13 +722,13 @@ private boolean acc(final Element e, final Set visited) { return false; } visited.add(e); - return exists(e.getAnnotationMirrors(), new Predicate() { + return exists(new Predicate() { @Override public boolean accept(AnnotationMirror m) { Element elem = m.getAnnotationType().asElement(); - return qualifiedName.apply(elem).equals(className) || !elem.equals(e) && acc(elem, visited); + return className.contains(qualifiedName.apply(elem)) || !elem.equals(e) && acc(elem, visited); } - }); + }, e.getAnnotationMirrors()); } }; } @@ -820,7 +821,7 @@ public String transform(String candidate) { public static final Transformer,Long> iterableSum = new Transformer,Long>() { @Override public Long transform(Iterable source) { - return reduce(source, Monoids.longSum); + return reduce(Monoids.longSum, source); } }; diff --git a/src/main/java/fi/solita/utils/meta/MetaConstructors.java b/src/main/java/fi/solita/utils/meta/MetaConstructors.java index 02da71b..6c5c83e 100644 --- a/src/main/java/fi/solita/utils/meta/MetaConstructors.java +++ b/src/main/java/fi/solita/utils/meta/MetaConstructors.java @@ -1,14 +1,14 @@ package fi.solita.utils.meta; -import static fi.solita.utils.functional.Functional.map; import static fi.solita.utils.functional.Functional.mkString; +import static fi.solita.utils.functional.FunctionalA.map; import java.lang.reflect.Constructor; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Map; -import fi.solita.utils.meta.generators.Content; import fi.solita.utils.functional.Function0; import fi.solita.utils.functional.Function1; import fi.solita.utils.functional.Function10; @@ -44,7 +44,6 @@ import fi.solita.utils.functional.Tuple17; import fi.solita.utils.functional.Tuple18; import fi.solita.utils.functional.Tuple19; -import fi.solita.utils.functional.Tuple2; import fi.solita.utils.functional.Tuple20; import fi.solita.utils.functional.Tuple21; import fi.solita.utils.functional.Tuple22; @@ -55,6 +54,7 @@ import fi.solita.utils.functional.Tuple7; import fi.solita.utils.functional.Tuple8; import fi.solita.utils.functional.Tuple9; +import fi.solita.utils.meta.generators.Content; public abstract class MetaConstructors { static final Constructor doGetMember(Class clazz, Class... argClasses) { @@ -73,7 +73,7 @@ public String transform(Class source) { }; }; static final String doToString(Class clazz, Class[] argClasses) { - return clazz.getSimpleName() + "(" + mkString(", ", map(argClasses, className)) + ")"; + return clazz.getSimpleName() + "(" + mkString(", ", map(className, argClasses)) + ")"; } public static abstract class C0 extends Function0 implements MetaConstructor { @@ -124,7 +124,7 @@ public String toString() { return doToString(clazz, argClasses); } } - public static abstract class C2 extends Function2 implements MetaConstructor,R> { + public static abstract class C2 extends Function2 implements MetaConstructor,R> { private transient Constructor $r; private final Class clazz; private final Class[] argClasses; diff --git a/src/main/java/fi/solita/utils/meta/MetaMethods.java b/src/main/java/fi/solita/utils/meta/MetaMethods.java index 52c578a..e3eaf07 100644 --- a/src/main/java/fi/solita/utils/meta/MetaMethods.java +++ b/src/main/java/fi/solita/utils/meta/MetaMethods.java @@ -1,8 +1,8 @@ package fi.solita.utils.meta; import java.lang.reflect.Method; +import java.util.Map; -import fi.solita.utils.meta.generators.Content; import fi.solita.utils.functional.Function0; import fi.solita.utils.functional.Function1; import fi.solita.utils.functional.Function10; @@ -37,7 +37,6 @@ import fi.solita.utils.functional.Tuple17; import fi.solita.utils.functional.Tuple18; import fi.solita.utils.functional.Tuple19; -import fi.solita.utils.functional.Tuple2; import fi.solita.utils.functional.Tuple20; import fi.solita.utils.functional.Tuple21; import fi.solita.utils.functional.Tuple22; @@ -48,6 +47,7 @@ import fi.solita.utils.functional.Tuple7; import fi.solita.utils.functional.Tuple8; import fi.solita.utils.functional.Tuple9; +import fi.solita.utils.meta.generators.Content; public abstract class MetaMethods { static final Method doGetMember(Class clazz, String name, Class... argClasses) { @@ -113,7 +113,7 @@ public final String toString() { return doToString(clazz, name); } } - public static abstract class M2 extends Function2 implements MetaMethod,R> { + public static abstract class M2 extends Function2 implements MetaMethod,R> { private transient Method $r; private final Class clazz; private final String name; diff --git a/src/main/java/fi/solita/utils/meta/generators/ConstructorsAsFunctions.java b/src/main/java/fi/solita/utils/meta/generators/ConstructorsAsFunctions.java index 044baab..8537ce2 100644 --- a/src/main/java/fi/solita/utils/meta/generators/ConstructorsAsFunctions.java +++ b/src/main/java/fi/solita/utils/meta/generators/ConstructorsAsFunctions.java @@ -1,5 +1,16 @@ package fi.solita.utils.meta.generators; +import static fi.solita.utils.functional.Collections.emptyList; +import static fi.solita.utils.functional.Collections.newList; +import static fi.solita.utils.functional.Functional.concat; +import static fi.solita.utils.functional.Functional.cons; +import static fi.solita.utils.functional.Functional.filter; +import static fi.solita.utils.functional.Functional.flatten; +import static fi.solita.utils.functional.Functional.map; +import static fi.solita.utils.functional.Functional.mkString; +import static fi.solita.utils.functional.Functional.zip; +import static fi.solita.utils.functional.Functional.zipWithIndex; +import static fi.solita.utils.functional.Option.Some; import static fi.solita.utils.meta.Helpers.boxedQualifiedName; import static fi.solita.utils.meta.Helpers.element2Constructors; import static fi.solita.utils.meta.Helpers.elementGenericQualifiedName; @@ -21,18 +32,6 @@ import static fi.solita.utils.meta.generators.Content.None; import static fi.solita.utils.meta.generators.Content.catchBlock; import static fi.solita.utils.meta.generators.Content.reflectionInvokationArgs; -import static fi.solita.utils.functional.Collections.emptyList; -import static fi.solita.utils.functional.Collections.newList; -import static fi.solita.utils.functional.Functional.concat; -import static fi.solita.utils.functional.Functional.cons; -import static fi.solita.utils.functional.Functional.flatten; -import static fi.solita.utils.functional.Functional.mkString; -import static fi.solita.utils.functional.Functional.zip; -import static fi.solita.utils.functional.Functional.zipWithIndex; -import static fi.solita.utils.functional.FunctionalA.concat; -import static fi.solita.utils.functional.FunctionalImpl.filter; -import static fi.solita.utils.functional.FunctionalImpl.map; -import static fi.solita.utils.functional.Option.Some; import java.util.List; import java.util.Map; @@ -45,10 +44,10 @@ import javax.lang.model.element.TypeParameterElement; import javax.lang.model.element.VariableElement; -import fi.solita.utils.meta.Helpers; import fi.solita.utils.functional.Apply; import fi.solita.utils.functional.Function1; import fi.solita.utils.functional.Function3; +import fi.solita.utils.meta.Helpers; public class ConstructorsAsFunctions extends Generator { @@ -71,11 +70,11 @@ public Iterable apply(ProcessingEnvironment processingEnv, Options optio Iterable elements = element2Constructors.apply(source); if (options.onlyPublicMembers()) { - elements = filter(elements, publicElement); + elements = filter(publicElement, elements); } Function1, Iterable> singleElementTransformer = constructorGen.ap(new Helpers.EnvDependent(processingEnv), options); - Iterable> rm = newList(map(newList(zipWithIndex(elements)), singleElementTransformer)); + Iterable> rm = newList(map(singleElementTransformer, newList(zipWithIndex(elements)))); return flatten(rm); } @@ -88,7 +87,7 @@ public Iterable apply(Helpers.EnvDependent helper, Options options, Map. int index = entry.getKey(); List relevantTypeParams = newList(relevantTypeParams(constructor)); - String relevantTypeParamsString = relevantTypeParams.isEmpty() ? "" : "<" + importTypes(mkString(", ", map(relevantTypeParams, typeParameter2String))) + ">"; + String relevantTypeParamsString = relevantTypeParams.isEmpty() ? "" : "<" + importTypes(mkString(", ", map(typeParameter2String, relevantTypeParams))) + ">"; List parameters = constructor.getParameters(); boolean needsToBeFunction = !relevantTypeParams.isEmpty(); @@ -100,8 +99,8 @@ public Iterable apply(Helpers.EnvDependent helper, Options options, Map. String returnTypeImported = importTypes(elementGenericQualifiedName(enclosingElement)); int argCount = constructor.getParameters().size(); - List argTypes = newList(map(parameters, boxedQualifiedName)); - List argNames = newList(map(parameters, simpleName)); + List argTypes = newList(map(boxedQualifiedName, parameters)); + List argNames = newList(map(simpleName, parameters)); List argNamesWithCast = newList(paramsWithCast(parameters, isPrivate)); String fieldName = "$" + (index == 0 ? "" : index); @@ -116,14 +115,14 @@ public Iterable apply(Helpers.EnvDependent helper, Options options, Map. Iterable tryCatchBlock = isPrivate || throwsChecked ? concat( Some("try {"), - map(tryBlock, padding), + map(padding, tryBlock), catchBlock, Some("}")) : tryBlock; Iterable applyBlock = concat( - Some("public " + returnTypeImported + " apply(" + mkString(", ", map(zip(argTypes, argNames), joinWithSpace)) + ") {"), - map(tryCatchBlock, padding), + Some("public " + returnTypeImported + " apply(" + mkString(", ", map(joinWithSpace, zip(argTypes, argNames))) + ") {"), + map(padding, tryCatchBlock), Some("}") ); @@ -133,9 +132,9 @@ public Iterable apply(Helpers.EnvDependent helper, Options options, Map. ? Some("@SuppressWarnings(\"rawtypes\")") : None, Some(declaration + (needsToBeFunction ? "() { return" : " =") + " new " + fundef + "(" + mkString(", ", cons(importTypes(enclosingElementQualifiedName) + ".class", reflectionInvokationArgs(parameterTypesAsClasses(constructor, relevantTypeParams)))) + ") {"), - map(applyBlock, padding), + map(padding, applyBlock), EmptyLine, - map(options.getAdditionalBodyLinesForConstructors(constructor), padding), + map(padding, options.getAdditionalBodyLinesForConstructors(constructor)), Some("};"), needsToBeFunction ? Some("}") diff --git a/src/main/java/fi/solita/utils/meta/generators/Content.java b/src/main/java/fi/solita/utils/meta/generators/Content.java index ed62bba..7063372 100644 --- a/src/main/java/fi/solita/utils/meta/generators/Content.java +++ b/src/main/java/fi/solita/utils/meta/generators/Content.java @@ -11,15 +11,15 @@ import static fi.solita.utils.meta.Helpers.resolveVisibility; import static fi.solita.utils.functional.Collections.newList; import static fi.solita.utils.functional.Functional.cons; +import static fi.solita.utils.functional.Functional.filter; +import static fi.solita.utils.functional.Functional.flatMap; import static fi.solita.utils.functional.Functional.isEmpty; +import static fi.solita.utils.functional.Functional.map; import static fi.solita.utils.functional.Functional.mkString; import static fi.solita.utils.functional.Functional.repeat; +import static fi.solita.utils.functional.Functional.sequence; import static fi.solita.utils.functional.Functional.transpose; import static fi.solita.utils.functional.FunctionalA.concat; -import static fi.solita.utils.functional.FunctionalA.filter; -import static fi.solita.utils.functional.FunctionalImpl.flatMap; -import static fi.solita.utils.functional.FunctionalImpl.map; -import static fi.solita.utils.functional.FunctionalImpl.sequence; import static fi.solita.utils.functional.Option.None; import static fi.solita.utils.functional.Option.Some; import static fi.solita.utils.functional.Predicates.equalTo; @@ -63,7 +63,7 @@ public static final List memberNameAccessor(String name) { private static final Pattern typeArgArray = Pattern.compile("[^.\\[\\]]+((\\[\\])+)"); public static final Iterable reflectionInvokationArgs(Iterable argumentClasses) { - return map(argumentClasses, new Transformer() { + return map(new Transformer() { @Override public String transform(String source) { if (isPrimitive(arrayClass.matcher(source).replaceAll(""))) { @@ -77,7 +77,7 @@ public String transform(String source) { return importTypes(source) + ".class"; } } - }); + }, argumentClasses); } @@ -99,7 +99,7 @@ public static final Iterable memberInitializer(String classNameGeneric, " $r = (" + clsName + ")(" + importType(Object.class) + ")" + importTypes(removeGenericPart.apply(classNameGeneric)) + ".class." + declr + ";", " $r.setAccessible(true);" ), - map(catchBlock, padding.andThen(padding)), + map(padding.andThen(padding), catchBlock), newList( " }", " }", @@ -139,24 +139,24 @@ public Pair,List> apply(CommonMetadataProcessor.CombinedGener // cannot refer to private classes return Pair.of(newList(repeat(0l, generators.size())), Collections.emptyList()); } - List>> elemData = newList(sequence(generators, source)); + List>> elemData = newList(sequence(source, generators)); Iterable nestedToProcess = element2NestedClasses.apply(source); if (options.onlyPublicMembers()) { - nestedToProcess = filter(nestedToProcess, publicElement); + nestedToProcess = filter(publicElement, nestedToProcess); } - List, List>> nestedData = newList(map(filter(nestedToProcess, predicate), withNestedClasses.ap(options, generatedClassNamePattern, predicate, generators))); + List, List>> nestedData = newList(map(withNestedClasses.ap(options, generatedClassNamePattern, predicate, generators), filter(predicate, nestedToProcess))); - Iterable generatorTimesForContent = map(elemData, Helpers.left()); - Iterable> generatorTimesForNestedClasses = map(nestedData, Helpers.>left()); - Iterable elemContents = flatMap(elemData, Helpers.>right()); - Iterable nestedContents = flatMap(nestedData, Helpers.>right()); + Iterable generatorTimesForContent = map(Helpers.left(), elemData); + Iterable> generatorTimesForNestedClasses = map(Helpers.>left(), nestedData); + Iterable elemContents = flatMap(Helpers.>right(), elemData); + Iterable nestedContents = flatMap(Helpers.>right(), nestedData); - List totalTimesPerGenerator = newList(map(transpose(cons(generatorTimesForContent, generatorTimesForNestedClasses)), Helpers.iterableSum)); + List totalTimesPerGenerator = newList(map(Helpers.iterableSum, transpose(cons(generatorTimesForContent, generatorTimesForNestedClasses)))); Iterable warnings = source.getAnnotation(SuppressWarnings.class) == null ? Collections.emptyList() - : filter(source.getAnnotation(SuppressWarnings.class).value(), not(equalTo("unused"))); + : filter(not(equalTo("unused")), source.getAnnotation(SuppressWarnings.class).value()); List allContents = newList(concat( isEmpty(warnings) @@ -167,7 +167,7 @@ public Pair,List> apply(CommonMetadataProcessor.CombinedGener : Some("@Deprecated"), Some(resolveVisibility(source) + "static final class " + generatedClassNamePattern.replace("{}", source.getSimpleName().toString()) + " implements " + Serializable.class.getName() + " {"), elemContents, - map(nestedContents, padding), + map(padding, nestedContents), Some("}"))); return Pair.of(totalTimesPerGenerator, allContents); } diff --git a/src/main/java/fi/solita/utils/meta/generators/InstanceFieldsAsEnum.java b/src/main/java/fi/solita/utils/meta/generators/InstanceFieldsAsEnum.java index 39d4647..bee7784 100644 --- a/src/main/java/fi/solita/utils/meta/generators/InstanceFieldsAsEnum.java +++ b/src/main/java/fi/solita/utils/meta/generators/InstanceFieldsAsEnum.java @@ -7,9 +7,9 @@ import static fi.solita.utils.meta.Helpers.staticElements; import static fi.solita.utils.functional.Collections.emptyList; import static fi.solita.utils.functional.Collections.newList; +import static fi.solita.utils.functional.Functional.filter; +import static fi.solita.utils.functional.Functional.map; import static fi.solita.utils.functional.FunctionalA.concat; -import static fi.solita.utils.functional.FunctionalImpl.filter; -import static fi.solita.utils.functional.FunctionalImpl.map; import static fi.solita.utils.functional.Option.Some; import static fi.solita.utils.functional.Predicates.not; import static fi.solita.utils.functional.Transformers.append; @@ -32,16 +32,16 @@ public static interface Options extends GeneratorOptions { public Iterable apply(ProcessingEnvironment processingEnv, Options options, TypeElement source) { Iterable elements = element2Fields.apply(source); if (options.onlyPublicMembers()) { - elements = filter(elements, publicElement); + elements = filter(publicElement, elements); } - List fieldsToInclude = newList(filter(elements, not(staticElements))); + List fieldsToInclude = newList(filter(not(staticElements), elements)); if (fieldsToInclude.isEmpty()) { return emptyList(); } return concat( Some("public enum $FieldNames {"), - map(fieldsToInclude, simpleName.andThen(padding).andThen(append(","))), + map(simpleName.andThen(padding).andThen(append(",")), fieldsToInclude), Some("}"), Some("") ); diff --git a/src/main/java/fi/solita/utils/meta/generators/InstanceFieldsAsFunctions.java b/src/main/java/fi/solita/utils/meta/generators/InstanceFieldsAsFunctions.java index 1bbc621..4e865f2 100644 --- a/src/main/java/fi/solita/utils/meta/generators/InstanceFieldsAsFunctions.java +++ b/src/main/java/fi/solita/utils/meta/generators/InstanceFieldsAsFunctions.java @@ -20,14 +20,14 @@ import static fi.solita.utils.meta.generators.Content.catchBlock; import static fi.solita.utils.functional.Collections.newList; import static fi.solita.utils.functional.Functional.contains; +import static fi.solita.utils.functional.Functional.filter; +import static fi.solita.utils.functional.Functional.flatMap; import static fi.solita.utils.functional.Functional.isEmpty; +import static fi.solita.utils.functional.Functional.map; import static fi.solita.utils.functional.Functional.mkString; import static fi.solita.utils.functional.Functional.subtract; import static fi.solita.utils.functional.Functional.zip; import static fi.solita.utils.functional.FunctionalA.concat; -import static fi.solita.utils.functional.FunctionalImpl.filter; -import static fi.solita.utils.functional.FunctionalImpl.flatMap; -import static fi.solita.utils.functional.FunctionalImpl.map; import static fi.solita.utils.functional.Option.Some; import static fi.solita.utils.functional.Predicates.not; @@ -67,10 +67,10 @@ public static interface Options extends GeneratorOptions { public Iterable apply(ProcessingEnvironment processingEnv, Options options, TypeElement source) { Iterable elements = element2Fields.apply(source); if (options.onlyPublicMembers()) { - elements = filter(elements, publicElement); + elements = filter(publicElement, elements); } - return flatMap(filter(elements, not(staticElements)), variableElementGen.ap(options, new Helpers.EnvDependent(processingEnv))); + return flatMap(variableElementGen.ap(options, new Helpers.EnvDependent(processingEnv)), filter(not(staticElements), elements)); } static final Transformer toReplacePattern = new Transformer() { @@ -98,21 +98,21 @@ public Iterable apply(Options options, Helpers.EnvDependent helper, Vari String returnType = resolveBoxedGenericType(field.asType(), helper.elementUtils); - Iterable allTypeParams = map(enclosingElement.getTypeParameters(), typeParameter2String); - List allTypeParamsWithoutConstraints = newList(map(enclosingElement.getTypeParameters(), simpleName)); + Iterable allTypeParams = map(typeParameter2String, enclosingElement.getTypeParameters()); + List allTypeParamsWithoutConstraints = newList(map(simpleName, enclosingElement.getTypeParameters())); final String rett = ("." + returnType + "."); - List> relevantTypeParamPairs = newList(filter(zip(allTypeParams, allTypeParamsWithoutConstraints), new Predicate>() { + List> relevantTypeParamPairs = newList(filter(new Predicate>() { @Override public boolean accept(Tuple2 candidate) { return rett.matches(".*[^a-zA-Z0-9_]" + Pattern.quote(candidate._2.toString()) + "[^a-zA-Z0-9_].*"); } - })); + }, zip(allTypeParams, allTypeParamsWithoutConstraints))); - List relevantTypeParams = newList(map(relevantTypeParamPairs, Helpers.left())); - relevantTypeParamsWithoutConstraints = newList(map(relevantTypeParamPairs, Helpers.right())); + List relevantTypeParams = newList(map(Helpers.left(), relevantTypeParamPairs)); + relevantTypeParamsWithoutConstraints = newList(map(Helpers.right(), relevantTypeParamPairs)); final List toReplace = newList(subtract(allTypeParamsWithoutConstraints, relevantTypeParamsWithoutConstraints)); - final List toReplacePatterns = newList(map(toReplace, toReplacePattern)); + final List toReplacePatterns = newList(map(toReplacePattern, toReplace)); Transformer doReplace = new Transformer() { @Override public String transform(String candidate) { @@ -127,7 +127,7 @@ public String transform(String candidate) { boolean isFinal = isFinal(field); needsToBeFunction = !relevantTypeParams.isEmpty(); - relevantTypeParamsString = isEmpty(relevantTypeParams) ? "" : "<" + mkString(", ", map(relevantTypeParams, doReplace)) + ">"; + relevantTypeParamsString = isEmpty(relevantTypeParams) ? "" : "<" + mkString(", ", map(doReplace, relevantTypeParams)) + ">"; returnType = doReplace.apply(returnType); String returnTypeImported = importTypes(returnType); @@ -157,14 +157,14 @@ public String transform(String candidate) { Iterable tryCatchBlock = isPrivate ? concat( Some("try {"), - map(tryBlock, padding), + map(padding, tryBlock), catchBlock, Some("}")) : tryBlock; Iterable applyBlock = concat( Some("public final " + implementedMethod + "(final " + (needsToBeFunction ? enclosingElementQualifiedNameImported : enclosingElementGenericQualifiedNameImported) + " $self) {"), - map(tryCatchBlock, padding), + map(padding, tryCatchBlock), Some("}") ); @@ -175,7 +175,7 @@ public String transform(String candidate) { Iterable setTryCatchBlock = isPrivate ? concat( Some("try {"), - map(setTryBlock, padding), + map(padding, setTryBlock), catchBlock, Some("}")) : setTryBlock; @@ -184,7 +184,7 @@ public String transform(String candidate) { needsToBeFunction ? Some("public final " + setterFundef + " setter(final " + (needsToBeFunction ? enclosingElementQualifiedNameImported : enclosingElementGenericQualifiedNameImported) + " $self) { return new " + setterFundef + "() { public Void apply(final Object $newValue) {") : Some("public final " + relevantTypeParamsString + " " + setterFundef + " setter(final " + (needsToBeFunction ? enclosingElementQualifiedNameImported : enclosingElementGenericQualifiedNameImported) + " $self) { return new " + setterFundef + "() { public Void apply(final " + returnTypeImported + " $newValue) {"), - map(setTryCatchBlock, padding), + map(padding, setTryCatchBlock), Some(" return null;"), Some("}};}") ); @@ -197,13 +197,13 @@ public String transform(String candidate) { ? newList(enclosingElementQualifiedNameImported.equals(enclosingElementGenericQualifiedNameImported) ? "" : "@SuppressWarnings(\"rawtypes\")", privateDeclaration + " = new " + privateFundef + initParams + " {") : Some(declaration + " = new " + fundef + initParams + " {"), - map(options.getAdditionalBodyLinesForInstanceFields(), padding), + map(padding, options.getAdditionalBodyLinesForInstanceFields()), EmptyLine, isPrivate && !needsToBeFunction && (field.getEnclosingElement().getAnnotation(SuppressWarnings.class) == null || !contains("unchecked", field.getEnclosingElement().getAnnotation(SuppressWarnings.class).value())) && (hasNonQmarkGenerics(returnType) || field.asType().getKind() == TypeKind.TYPEVAR) ? Some(" @SuppressWarnings(\"unchecked\")") : None, - map(applyBlock, padding), - isFinal ? None : map(setBlock, padding), + map(padding, applyBlock), + isFinal ? None : map(padding, setBlock), Some("};"), needsToBeFunction ? newList("@SuppressWarnings(\"unchecked\")", diff --git a/src/main/java/fi/solita/utils/meta/generators/InstanceFieldsAsTuple.java b/src/main/java/fi/solita/utils/meta/generators/InstanceFieldsAsTuple.java index a7e0deb..f6cada3 100644 --- a/src/main/java/fi/solita/utils/meta/generators/InstanceFieldsAsTuple.java +++ b/src/main/java/fi/solita/utils/meta/generators/InstanceFieldsAsTuple.java @@ -9,11 +9,11 @@ import static fi.solita.utils.meta.Helpers.typeParameter2String; import static fi.solita.utils.functional.Collections.emptyList; import static fi.solita.utils.functional.Collections.newList; +import static fi.solita.utils.functional.Functional.filter; import static fi.solita.utils.functional.Functional.isEmpty; +import static fi.solita.utils.functional.Functional.map; import static fi.solita.utils.functional.Functional.mkString; import static fi.solita.utils.functional.FunctionalA.concat; -import static fi.solita.utils.functional.FunctionalImpl.filter; -import static fi.solita.utils.functional.FunctionalImpl.map; import static fi.solita.utils.functional.Option.Some; import static fi.solita.utils.functional.Predicates.not; @@ -25,7 +25,6 @@ import javax.lang.model.element.VariableElement; import fi.solita.utils.meta.Helpers; -import fi.solita.utils.meta.Workaround; import fi.solita.utils.meta.Helpers.EnvDependent; import fi.solita.utils.functional.Apply; import fi.solita.utils.functional.Transformer; @@ -51,10 +50,10 @@ public static interface Options extends GeneratorOptions { public Iterable apply(ProcessingEnvironment processingEnv, final Options options, final TypeElement enclosingElement) { Iterable elements = Helpers.element2Fields.apply(enclosingElement); if (options.onlyPublicMembers()) { - elements = filter(elements, publicElement); + elements = filter(publicElement, elements); } - List fieldsToInclude = newList(filter(elements, not(staticElements))); + List fieldsToInclude = newList(filter(not(staticElements), elements)); if (fieldsToInclude.isEmpty() || fieldsToInclude.size() > Tuple.class.getDeclaredClasses().length) { return emptyList(); } @@ -63,13 +62,13 @@ public Iterable apply(ProcessingEnvironment processingEnv, final Options final String pack = options.generatedPackagePattern().replace("{}", getPackageName(enclosingElement)); final String metaClassQualifiedName = pack + "." + options.generatedClassNamePattern().replace("{}", qualifiedName.apply(enclosingElement).replaceFirst(Pattern.quote(getPackageName(enclosingElement) + "."), "").replace(".", "_.")); - List allTypeParams = newList(map(enclosingElement.getTypeParameters(), typeParameter2String)); + List allTypeParams = newList(map(typeParameter2String, enclosingElement.getTypeParameters())); String typeParamsString = isEmpty(allTypeParams) ? "" : "<" + mkString(", ", allTypeParams) + ">"; String tupleClass = Helpers.importTypes(Tuple.class.getName() + fieldsToInclude.size()); final EnvDependent helper = new Helpers.EnvDependent(processingEnv); - List> fieldData = newList(map(fieldsToInclude, new Transformer>() { + List> fieldData = newList(map(new Transformer>() { @Override public Tuple3 transform(VariableElement field) { f.apply((fi.solita.utils.meta.generators.InstanceFieldsAsFunctions.Options) options, helper, field); @@ -77,11 +76,11 @@ public Tuple3 transform(VariableElement field) { return Tuple.of(metaClassQualifiedName + "." + relevantTypeParamsString + simpleName.apply(field), f.fundef, f.needsToBeFunction); } - })); + }, fieldsToInclude)); return concat( - Some("public static final " + typeParamsString + " " + tupleClass + "<" + mkString(",", map(fieldData, getType)) + ">" + " $Fields() { return " + Helpers.importType(Tuple.class) + ".of("), - Some(mkString(", ", map(fieldData, getInvokation.andThen(padding)))), + Some("public static final " + typeParamsString + " " + tupleClass + "<" + mkString(",", map(getType, fieldData)) + ">" + " $Fields() { return " + Helpers.importType(Tuple.class) + ".of("), + Some(mkString(", ", map(getInvokation.andThen(padding), fieldData))), Some("); }"), Some("") ); diff --git a/src/main/java/fi/solita/utils/meta/generators/MethodsAsFunctions.java b/src/main/java/fi/solita/utils/meta/generators/MethodsAsFunctions.java index 8dd5a6c..5c21990 100644 --- a/src/main/java/fi/solita/utils/meta/generators/MethodsAsFunctions.java +++ b/src/main/java/fi/solita/utils/meta/generators/MethodsAsFunctions.java @@ -1,5 +1,21 @@ package fi.solita.utils.meta.generators; +import static fi.solita.utils.functional.Collections.newList; +import static fi.solita.utils.functional.Collections.newSet; +import static fi.solita.utils.functional.Functional.concat; +import static fi.solita.utils.functional.Functional.cons; +import static fi.solita.utils.functional.Functional.filter; +import static fi.solita.utils.functional.Functional.flatMap; +import static fi.solita.utils.functional.Functional.isEmpty; +import static fi.solita.utils.functional.Functional.map; +import static fi.solita.utils.functional.Functional.mkString; +import static fi.solita.utils.functional.Functional.repeat; +import static fi.solita.utils.functional.Functional.subtract; +import static fi.solita.utils.functional.Functional.zip; +import static fi.solita.utils.functional.FunctionalM.groupBy; +import static fi.solita.utils.functional.Option.Some; +import static fi.solita.utils.functional.Predicates.not; +import static fi.solita.utils.functional.Transformers.prepend; import static fi.solita.utils.meta.Helpers.allTypeParams; import static fi.solita.utils.meta.Helpers.allUsedTypeParameters; import static fi.solita.utils.meta.Helpers.boxedQualifiedName; @@ -31,23 +47,6 @@ import static fi.solita.utils.meta.generators.Content.None; import static fi.solita.utils.meta.generators.Content.catchBlock; import static fi.solita.utils.meta.generators.Content.reflectionInvokationArgs; -import static fi.solita.utils.functional.Collections.newList; -import static fi.solita.utils.functional.Collections.newSet; -import static fi.solita.utils.functional.Functional.concat; -import static fi.solita.utils.functional.Functional.cons; -import static fi.solita.utils.functional.Functional.isEmpty; -import static fi.solita.utils.functional.Functional.mkString; -import static fi.solita.utils.functional.Functional.repeat; -import static fi.solita.utils.functional.Functional.subtract; -import static fi.solita.utils.functional.Functional.zip; -import static fi.solita.utils.functional.FunctionalA.concat; -import static fi.solita.utils.functional.FunctionalImpl.filter; -import static fi.solita.utils.functional.FunctionalImpl.flatMap; -import static fi.solita.utils.functional.FunctionalImpl.groupBy; -import static fi.solita.utils.functional.FunctionalImpl.map; -import static fi.solita.utils.functional.Option.Some; -import static fi.solita.utils.functional.Predicates.not; -import static fi.solita.utils.functional.Transformers.prepend; import java.util.List; import java.util.Map; @@ -61,7 +60,6 @@ import javax.lang.model.element.VariableElement; import javax.lang.model.type.TypeKind; -import fi.solita.utils.meta.Helpers; import fi.solita.utils.functional.Apply; import fi.solita.utils.functional.Function1; import fi.solita.utils.functional.Function4; @@ -69,6 +67,7 @@ import fi.solita.utils.functional.Predicate; import fi.solita.utils.functional.Transformer; import fi.solita.utils.functional.Tuple2; +import fi.solita.utils.meta.Helpers; public class MethodsAsFunctions extends Generator { @@ -89,19 +88,19 @@ public static interface Options extends GeneratorOptions { public Iterable apply(ProcessingEnvironment processingEnv, Options options, TypeElement source) { Iterable elements = element2Methods.apply(source); if (options.onlyPublicMembers()) { - elements = filter(elements, publicElement); + elements = filter(publicElement, elements); } Iterable processedFields = element2Fields.apply(source); if (options.onlyPublicMembers()) { - processedFields = filter(processedFields, publicElement); + processedFields = filter(publicElement, processedFields); } - processedFields = filter(processedFields, not(staticElements)); + processedFields = filter(not(staticElements), processedFields); - Iterable> elementsByName = groupBy(elements, simpleName).values(); - Function1, Iterable> singleElementTransformer = executableElementGen.ap(new Helpers.EnvDependent(processingEnv), options, newSet(map(processedFields, simpleName))); + Iterable> elementsByName = groupBy(simpleName, elements).values(); + Function1, Iterable> singleElementTransformer = executableElementGen.ap(new Helpers.EnvDependent(processingEnv), options, newSet(map(simpleName, processedFields))); - return flatMap(flatMap(elementsByName, zipWithIndex), singleElementTransformer); + return flatMap(singleElementTransformer, flatMap(zipWithIndex, elementsByName)); } public static Function4, Map.Entry, Iterable> executableElementGen = new Function4, Map.Entry, Iterable>() { @@ -116,9 +115,9 @@ public Iterable apply(Helpers.EnvDependent helper, Options options, Set< List allTypeParamsForMethod = newList(allTypeParams(method)); List relevantTypeParamsForMethod = newList(relevantTypeParams(method)); - final List relevantTypeParams = newList(map(relevantTypeParamsForMethod, typeParameter2String)); - final Set relevantTypeParamsWithoutConstraints = newSet(map(relevantTypeParamsForMethod, simpleName)); - final List allTypeParamsWithoutConstraints = newList(map(allTypeParamsForMethod, simpleName)); + final List relevantTypeParams = newList(map(typeParameter2String, relevantTypeParamsForMethod)); + final Set relevantTypeParamsWithoutConstraints = newSet(map(simpleName, relevantTypeParamsForMethod)); + final List allTypeParamsWithoutConstraints = newList(map(simpleName, allTypeParamsForMethod)); List parameters = method.getParameters(); List typeParameters = method.getTypeParameters(); @@ -128,7 +127,7 @@ public Iterable apply(Helpers.EnvDependent helper, Options options, Set< String methodName = method.getSimpleName().toString(); String returnType = doReplace.transform(resolveBoxedGenericType(method.getReturnType(), helper.elementUtils)); String returnTypeImported = importTypes(returnType); - List argumentTypes = newList(map(parameters, boxedQualifiedName)); + List argumentTypes = newList(map(boxedQualifiedName, parameters)); List enclosingElementTypeParameters = enclosingElement.getTypeParameters(); boolean needsToBeFunction = !relevantTypeParams.isEmpty(); @@ -149,11 +148,11 @@ public Iterable apply(Helpers.EnvDependent helper, Options options, Set< : enclosingElementQualifiedName + (enclosingElementTypeParameters.isEmpty() ? "" : "<" + mkString(", ", repeat("?", enclosingElementTypeParameters.size())) + ">")); String instanceName = isInstanceMethod ? "$self" : importTypes(enclosingElementQualifiedName); - argumentTypes = newList(map(argumentTypes, doReplace)); - String relevantTypeParamsString = relevantTypeParams.isEmpty() ? " " : "<" + importTypes(mkString(", ", map(relevantTypeParams, doReplace))) + "> "; + argumentTypes = newList(map(doReplace, argumentTypes)); + String relevantTypeParamsString = relevantTypeParams.isEmpty() ? " " : "<" + importTypes(mkString(", ", map(doReplace, relevantTypeParams))) + "> "; List argTypes = zeroArgInstanceMethod ? newList(enclosingElementGenericQualifiedName) : argumentTypes; - Iterable argNames = zeroArgInstanceMethod ? newList("$self") : map(parameters, simpleName); + Iterable argNames = zeroArgInstanceMethod ? newList("$self") : map(simpleName, parameters); List argNamesWithCast = newList(paramsWithCast(parameters, isPrivate)); //boolean hasPlainGenericArguments = exists(map(argTypes, removeGenericPart), not(contains('.'))); @@ -165,7 +164,7 @@ public Iterable apply(Helpers.EnvDependent helper, Options options, Set< Class methodClass = usePredicate ? options.getPredicateClassForMethods() : options.getClassForMethods(argCount + (handleAsInstanceMethod ? 1 : 0)); Iterable implementedMethodArgTypes = handleAsInstanceMethod ? cons(enclosingElementGenericQualifiedName, argTypes) : argTypes; - Iterable privateImplementedMethodArgTypes = map(handleAsInstanceMethod ? cons(enclosingElementQualifiedName, argTypes) : argTypes, removeGenericPart.andThen(replaceTypeVarWithObject)); + Iterable privateImplementedMethodArgTypes = map(removeGenericPart.andThen(replaceTypeVarWithObject), handleAsInstanceMethod ? cons(enclosingElementQualifiedName, argTypes) : argTypes); String implementedMethod = Predicate.class.isAssignableFrom(methodClass) ? "boolean accept" : (optimize ? "Object" : returnTypeImported) + " apply"; String fundef = importType(methodClass) + "<" + importTypes(mkString(", ", usePredicate ? implementedMethodArgTypes : concat(implementedMethodArgTypes, newSet(returnType)))) + "> "; @@ -177,7 +176,7 @@ public Iterable apply(Helpers.EnvDependent helper, Options options, Set< Iterable tryBlock = concat( isPrivate ? Some(returnClause + "getMember().invoke(" + mkString(", ", cons(isInstanceMethod ? "$self" : "null", argNamesWithCast)) + ");") - : Some(returnClause + instanceName + "." + (typeParameters.isEmpty() || optimize ? "" : "<" + mkString(", ", map(typeParameters, simpleName)) + ">") + methodName + "(" + mkString(", ", argNamesWithCast) + ");"), + : Some(returnClause + instanceName + "." + (typeParameters.isEmpty() || optimize ? "" : "<" + mkString(", ", map(simpleName, typeParameters)) + ">") + methodName + "(" + mkString(", ", argNamesWithCast) + ");"), returnsVoid ? Some("return null;") : None @@ -185,18 +184,18 @@ public Iterable apply(Helpers.EnvDependent helper, Options options, Set< Iterable tryCatchBlock = isPrivate || throwsChecked ? concat( Some("try {"), - map(tryBlock, Helpers.padding), + map(Helpers.padding, tryBlock), catchBlock, Some("}")) : tryBlock; Iterable applyBlock = concat( - Some("public final " + implementedMethod + "(" + importTypes(mkString(", ", map(zip(map(optimize ? privateImplementedMethodArgTypes : implementedMethodArgTypes, prepend("final ")), handleAsInstanceMethod ? cons("$self", argNames) : argNames), joinWithSpace))) + ") { "), - map(tryCatchBlock, Helpers.padding), + Some("public final " + implementedMethod + "(" + importTypes(mkString(", ", map(joinWithSpace, zip(map(prepend("final "), optimize ? privateImplementedMethodArgTypes : implementedMethodArgTypes), handleAsInstanceMethod ? cons("$self", argNames) : argNames)))) + ") { "), + map(Helpers.padding, tryCatchBlock), Some("}") ); Iterable contentBlock = concat( - map(applyBlock, Helpers.padding), - map(options.getAdditionalBodyLinesForMethods(method), Helpers.padding) + map(Helpers.padding, applyBlock), + map(Helpers.padding, options.getAdditionalBodyLinesForMethods(method)) ); String initParams = "(" + importTypes(enclosingElementQualifiedName) + ".class, " + mkString(", ", cons("\"" + methodName + (index == 0 ? "" : index) + "\"", reflectionInvokationArgs(parameterTypesAsClasses(method, relevantTypeParamsForMethod)))) + ")";