Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #199 from google/more.tests
Browse files Browse the repository at this point in the history
Test functionality against all feature sets
  • Loading branch information
j-baker authored Oct 5, 2016
2 parents 1ba1e5c + 83efd0f commit 36a2ee8
Show file tree
Hide file tree
Showing 34 changed files with 1,117 additions and 765 deletions.
31 changes: 28 additions & 3 deletions src/main/java/org/inferred/freebuilder/processor/Processor.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,22 @@
*/
package org.inferred.freebuilder.processor;

import static com.google.common.base.MoreObjects.firstNonNull;
import static javax.lang.model.util.ElementFilter.typesIn;
import static org.inferred.freebuilder.processor.util.ModelUtils.findAnnotationMirror;
import static org.inferred.freebuilder.processor.util.RoundEnvironments.annotatedElementsIn;

import com.google.auto.service.AutoService;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Objects;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableSet;

import org.inferred.freebuilder.FreeBuilder;
import org.inferred.freebuilder.processor.util.CompilationUnitBuilder;
import org.inferred.freebuilder.processor.util.FilerUtils;
import org.inferred.freebuilder.processor.util.feature.EnvironmentFeatureSet;
import org.inferred.freebuilder.processor.util.feature.FeatureSet;

import java.io.IOException;
import java.util.Set;
Expand All @@ -50,6 +55,18 @@ public class Processor extends AbstractProcessor {

private Analyser analyser;
private final CodeGenerator codeGenerator = new CodeGenerator();
private final FeatureSet features;

private transient FeatureSet environmentFeatures;

public Processor() {
this.features = null;
}

@VisibleForTesting
public Processor(FeatureSet features) {
this.features = features;
}

@Override
public Set<String> getSupportedAnnotationTypes() {
Expand All @@ -69,6 +86,9 @@ public synchronized void init(ProcessingEnvironment processingEnv) {
processingEnv.getMessager(),
MethodIntrospector.instance(processingEnv),
processingEnv.getTypeUtils());
if (features == null) {
environmentFeatures = new EnvironmentFeatureSet(processingEnv);
}
}

@Override
Expand All @@ -79,7 +99,8 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
CompilationUnitBuilder code = new CompilationUnitBuilder(
processingEnv,
metadata.getGeneratedBuilder().getQualifiedName(),
metadata.getVisibleNestedTypes());
metadata.getVisibleNestedTypes(),
firstNonNull(features, environmentFeatures));
codeGenerator.writeBuilderSource(code, metadata);
FilerUtils.writeCompilationUnit(
processingEnv.getFiler(),
Expand Down Expand Up @@ -113,11 +134,15 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment

@Override
public boolean equals(Object obj) {
return (obj instanceof Processor);
if (!(obj instanceof Processor)) {
return false;
}
Processor other = (Processor) obj;
return Objects.equal(features, other.features);
}

@Override
public int hashCode() {
return Processor.class.hashCode();
return Objects.hashCode(Processor.class, features);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.googlejavaformat.java.Formatter;

import org.inferred.freebuilder.processor.util.feature.EnvironmentFeatureSet;
import org.inferred.freebuilder.processor.util.feature.Feature;
import org.inferred.freebuilder.processor.util.feature.FeatureSet;
import org.inferred.freebuilder.processor.util.feature.FeatureType;

import java.util.Collection;
Expand All @@ -37,13 +37,15 @@ public class CompilationUnitBuilder implements SourceBuilder {
private final QualifiedName classToWrite;

/**
* Returns a {@link CompilationUnitBuilder} for {@code classToWrite}. The file preamble (package
* and imports) will be generated automatically; the feature set will be taken from {@code env}..
* Returns a {@link CompilationUnitBuilder} for {@code classToWrite} using {@code features}. The
* file preamble (package and imports) will be generated automatically, and {@code env} will be
* inspected for potential import collisions.
*/
public CompilationUnitBuilder(
ProcessingEnvironment env,
QualifiedName classToWrite,
Collection<QualifiedName> nestedClasses) {
Collection<QualifiedName> nestedClasses,
FeatureSet features) {
this.classToWrite = classToWrite;
// Write the source code into an intermediate SourceStringBuilder, as the imports need to be
// written first, but aren't known yet.
Expand All @@ -57,7 +59,7 @@ public CompilationUnitBuilder(
importManagerBuilder.addImplicitImport(nestedClass);
}
importManager = importManagerBuilder.build();
source = new SourceStringBuilder(importManager, new EnvironmentFeatureSet(env));
source = new SourceStringBuilder(importManager, features);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
public enum FunctionPackage implements Feature<FunctionPackage> {

AVAILABLE, UNAVAILABLE;
AVAILABLE("Lambdas"), UNAVAILABLE("No lambdas");

/**
* Constant to pass to {@link SourceBuilder#feature(FeatureType)} to get the current status of
Expand Down Expand Up @@ -51,6 +51,12 @@ protected FunctionPackage forEnvironment(ProcessingEnvironment env) {
private static final ParameterizedType UNARY_OPERATOR =
QualifiedName.of("java.util.function", "UnaryOperator").withParameters("T");

private final String humanReadableFormat;

FunctionPackage(String humanReadableFormat) {
this.humanReadableFormat = humanReadableFormat;
}

/**
* Parameterized type for {@code java.util.function.Consumer<T>}, if available.
*/
Expand All @@ -72,6 +78,11 @@ public Optional<ParameterizedType> unaryOperator() {
return ifAvailable(UNARY_OPERATOR);
}

@Override
public String toString() {
return humanReadableFormat;
}

private static boolean runningInEclipse() {
// If we're running in Eclipse, we will have been invoked by the Eclipse round dispatcher.
Throwable t = new Throwable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
public enum GuavaLibrary implements Feature<GuavaLibrary> {

AVAILABLE, UNAVAILABLE;
AVAILABLE("Guava"), UNAVAILABLE("No Guava");

/**
* Constant to pass to {@link SourceBuilder#feature(FeatureType)} to get the current status of
Expand All @@ -34,7 +34,18 @@ protected GuavaLibrary forEnvironment(ProcessingEnvironment env) {
}
};

private final String humanReadableFormat;

GuavaLibrary(String humanReadableFormat) {
this.humanReadableFormat = humanReadableFormat;
}

public boolean isAvailable() {
return this != UNAVAILABLE;
}

@Override
public String toString() {
return humanReadableFormat;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
*/
public enum SourceLevel implements Feature<SourceLevel> {

JAVA_6, JAVA_7;
JAVA_6("Java 6"), JAVA_7("Java 7+");

/**
* Constant to pass to {@link SourceBuilder#feature(FeatureType)} to get the current
Expand Down Expand Up @@ -89,6 +89,12 @@ protected void addFields(FieldReceiver fields) {
}
}

private final String humanReadableFormat;

SourceLevel(String humanReadableFormat) {
this.humanReadableFormat = humanReadableFormat;
}

public Optional<QualifiedName> javaUtilObjects() {
switch (this) {
case JAVA_6:
Expand All @@ -98,4 +104,9 @@ public Optional<QualifiedName> javaUtilObjects() {
return Optional.of(QualifiedName.of("java.util", "Objects"));
}
}

@Override
public String toString() {
return humanReadableFormat;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.inferred.freebuilder.processor.util.feature;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableMap;

/**
Expand Down Expand Up @@ -38,4 +39,9 @@ public <T extends Feature<T>> T get(FeatureType<T> featureType) {
}
return featureType.testDefault();
}

@Override
public String toString() {
return Joiner.on(", ").join(featuresByType.values());
}
}
Loading

0 comments on commit 36a2ee8

Please sign in to comment.