Skip to content

Commit

Permalink
Merge pull request square#621 from square/jwilson.0728.honor_profiles
Browse files Browse the repository at this point in the history
Honor profiles for Wire configuration.
  • Loading branch information
swankjesse authored Jul 29, 2016
2 parents b2ec55c + 7d9a435 commit a697c58
Show file tree
Hide file tree
Showing 33 changed files with 490 additions and 565 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
<module>wire-maven-plugin</module>
<module>wire-runtime</module>
<module>wire-schema</module>
<module>wire-schema-tests</module>
<module>wire-testing</module>
<module>wire-tests</module>
</modules>

<properties>
Expand Down
41 changes: 10 additions & 31 deletions wire-compiler/src/main/java/com/squareup/wire/WireCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@
*/
package com.squareup.wire;

import com.google.common.collect.ImmutableMap;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.TypeSpec;
import com.squareup.wire.java.AdapterConstant;
import com.squareup.wire.java.JavaGenerator;
import com.squareup.wire.java.Profile;
import com.squareup.wire.java.ProfileLoader;
import com.squareup.wire.schema.IdentifierSet;
import com.squareup.wire.schema.Location;
import com.squareup.wire.schema.ProtoFile;
import com.squareup.wire.schema.ProtoType;
import com.squareup.wire.schema.Schema;
import com.squareup.wire.schema.SchemaLoader;
import com.squareup.wire.schema.Type;
Expand All @@ -36,9 +35,7 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentLinkedQueue;
Expand Down Expand Up @@ -99,7 +96,6 @@ public final class WireCompiler {
public static final String NAMED_FILES_ONLY = "--named_files_only";
public static final String ANDROID = "--android";
public static final String COMPACT = "--compact";
public static final String PROTO_ADAPTER = "--proto_adapter=";
public static final int MAX_WRITE_CONCURRENCY = 8;

private static final String CODE_GENERATED_BY_WIRE =
Expand All @@ -117,14 +113,10 @@ public final class WireCompiler {
final boolean namedFilesOnly;
final boolean emitAndroid;
final boolean emitCompact;
final ImmutableMap<ProtoType, ClassName> protoTypeToJavaClassName;
final ImmutableMap<ProtoType, AdapterConstant> protoTypeToAdapterConstant;

WireCompiler(FileSystem fs, WireLogger log, List<String> protoPaths, String javaOut,
List<String> sourceFileNames, IdentifierSet identifierSet, boolean dryRun,
boolean namedFilesOnly, boolean emitAndroid, boolean emitCompact,
Map<ProtoType, ClassName> protoTypeToJavaClassName,
Map<ProtoType, AdapterConstant> protoTypeToAdapterConstant) {
boolean namedFilesOnly, boolean emitAndroid, boolean emitCompact) {
this.fs = fs;
this.log = log;
this.protoPaths = protoPaths;
Expand All @@ -135,8 +127,6 @@ public final class WireCompiler {
this.namedFilesOnly = namedFilesOnly;
this.emitAndroid = emitAndroid;
this.emitCompact = emitCompact;
this.protoTypeToJavaClassName = ImmutableMap.copyOf(protoTypeToJavaClassName);
this.protoTypeToAdapterConstant = ImmutableMap.copyOf(protoTypeToAdapterConstant);
}

public static void main(String... args) throws IOException {
Expand Down Expand Up @@ -165,8 +155,6 @@ static WireCompiler forArgs(
boolean namedFilesOnly = false;
boolean emitAndroid = false;
boolean emitCompact = false;
Map<ProtoType, ClassName> protoTypeToJavaClass = new LinkedHashMap<>();
Map<ProtoType, AdapterConstant> protoTypeToAdapter = new LinkedHashMap<>();

for (String arg : args) {
if (arg.startsWith(PROTO_PATH_FLAG)) {
Expand All @@ -190,19 +178,6 @@ static WireCompiler forArgs(
for (String identifier : splitArg(arg, EXCLUDES_FLAG.length())) {
identifierSetBuilder.exclude(identifier);
}
} else if (arg.startsWith(PROTO_ADAPTER)) {
List<String> customProtoAdapterArgs = splitArg(arg, PROTO_ADAPTER.length());
if (customProtoAdapterArgs.size() != 3) {
throw new IllegalArgumentException(
"Expected <proto type>,<class name>,<adapter constant> but was " + arg);
}

ProtoType protoType = ProtoType.get(customProtoAdapterArgs.get(0).trim());
ClassName javaClassName = ClassName.bestGuess(customProtoAdapterArgs.get(1).trim());

protoTypeToJavaClass.put(protoType, javaClassName);
protoTypeToAdapter.put(protoType, new AdapterConstant(
customProtoAdapterArgs.get(2).trim()));
} else if (arg.equals(QUIET_FLAG)) {
quiet = true;
} else if (arg.equals(DRY_RUN_FLAG)) {
Expand All @@ -227,8 +202,7 @@ static WireCompiler forArgs(
logger.setQuiet(quiet);

return new WireCompiler(fileSystem, logger, protoPaths, javaOut, sourceFileNames,
identifierSetBuilder.build(), dryRun, namedFilesOnly, emitAndroid, emitCompact,
protoTypeToJavaClass, protoTypeToAdapter);
identifierSetBuilder.build(), dryRun, namedFilesOnly, emitAndroid, emitCompact);
}

private static List<String> splitArg(String arg, int flagLength) {
Expand All @@ -245,6 +219,11 @@ void compile() throws IOException {
}
Schema schema = schemaLoader.load();

String profileName = emitAndroid ? "android" : "java";
Profile profile = new ProfileLoader(profileName)
.schema(schema)
.load();

if (!identifierSet.isEmpty()) {
log.info("Analyzing dependencies of root types.");
schema = schema.prune(identifierSet);
Expand All @@ -257,7 +236,7 @@ void compile() throws IOException {
}

JavaGenerator javaGenerator = JavaGenerator.get(schema)
.withCustomProtoAdapter(protoTypeToJavaClassName, protoTypeToAdapterConstant)
.withProfile(profile)
.withAndroid(emitAndroid)
.withCompact(emitCompact);

Expand Down
6 changes: 0 additions & 6 deletions wire-java-generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.squareup.wire</groupId>
<artifactId>wire-testing</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
*/
public final class AdapterConstant {
public final ClassName className;
public final String adapterName;
public final String memberName;

public AdapterConstant(ClassName className, String adapterName) {
public AdapterConstant(ClassName className, String memberName) {
this.className = className;
this.adapterName = adapterName;
this.memberName = memberName;
}

public AdapterConstant(String adapter) {
Expand All @@ -38,6 +38,16 @@ public AdapterConstant(String adapter) {
throw new IllegalArgumentException("Illegally formatted adapter: " + adapter + ".");
}
this.className = ClassName.bestGuess(names[0]);
this.adapterName = names[1];
this.memberName = names[1];
}

@Override public boolean equals(Object o) {
return o instanceof AdapterConstant
&& ((AdapterConstant) o).className.equals(className)
&& ((AdapterConstant) o).memberName.equals(memberName);
}

@Override public int hashCode() {
return className.hashCode() * 37 + memberName.hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,59 +163,30 @@ public final class JavaGenerator {
});

private final Schema schema;
private final ImmutableMap<ProtoType, ClassName> nameToAbstractAdapterName;
private final ImmutableMap<ProtoType, ClassName> nameToJavaName;
private final ImmutableMap<ProtoType, AdapterConstant> protoTypeToAdapterConstant;
private final Profile profile;
private final boolean emitAndroid;
private final boolean emitCompact;

private JavaGenerator(Schema schema,
ImmutableMap<ProtoType, ClassName> nameToAbstractAdapterName,
ImmutableMap<ProtoType, ClassName> nameToJavaName,
ImmutableMap<ProtoType, AdapterConstant> protoTypeToAdapterConstant,
private JavaGenerator(Schema schema, Map<ProtoType, ClassName> nameToJavaName, Profile profile,
boolean emitAndroid, boolean emitCompact) {
this.schema = schema;
this.nameToAbstractAdapterName = nameToAbstractAdapterName;
this.nameToJavaName = nameToJavaName;
this.protoTypeToAdapterConstant = protoTypeToAdapterConstant;
this.nameToJavaName = ImmutableMap.copyOf(nameToJavaName);
this.profile = profile;
this.emitAndroid = emitAndroid;
this.emitCompact = emitCompact;
}

public JavaGenerator withAndroid(boolean emitAndroid) {
return new JavaGenerator(schema, nameToAbstractAdapterName, nameToJavaName,
protoTypeToAdapterConstant, emitAndroid, emitCompact);
return new JavaGenerator(schema, nameToJavaName, profile, emitAndroid, emitCompact);
}

public JavaGenerator withCompact(boolean compactGeneration) {
return new JavaGenerator(schema, nameToAbstractAdapterName, nameToJavaName,
protoTypeToAdapterConstant, emitAndroid, compactGeneration);
public JavaGenerator withCompact(boolean emitCompact) {
return new JavaGenerator(schema, nameToJavaName, profile, emitAndroid, emitCompact);
}

public JavaGenerator withCustomProtoAdapter(
Map<ProtoType, ClassName> protoTypeToCustomJavaName,
Map<ProtoType, AdapterConstant> protoTypeToAdapterConstant) {
if (!nameToAbstractAdapterName.isEmpty()) {
throw new IllegalStateException("Custom proto adapters already defined");
}

Map<ProtoType, ClassName> nameToJavaName = new LinkedHashMap<>(this.nameToJavaName);
Map<ProtoType, ClassName> nameToAbstractAdapterName = new LinkedHashMap<>();

for (Map.Entry<ProtoType, ClassName> entry : protoTypeToCustomJavaName.entrySet()) {
ClassName javaName = nameToJavaName.put(entry.getKey(), entry.getValue());
if (javaName == null) {
throw new IllegalArgumentException("Custom adapter specified for " + entry.getKey()
+ " but no proto was found!");
}

nameToAbstractAdapterName.put(entry.getKey(),
javaName.peerClass("Abstract" + javaName.simpleName() + "Adapter"));
}

return new JavaGenerator(schema, ImmutableMap.copyOf(nameToAbstractAdapterName),
ImmutableMap.copyOf(nameToJavaName), ImmutableMap.copyOf(protoTypeToAdapterConstant),
emitAndroid, emitCompact);
public JavaGenerator withProfile(Profile profile) {
return new JavaGenerator(schema, nameToJavaName, profile, emitAndroid, emitCompact);
}

public static JavaGenerator get(Schema schema) {
Expand All @@ -232,9 +203,7 @@ public static JavaGenerator get(Schema schema) {
}
}

return new JavaGenerator(schema, ImmutableMap.<ProtoType, ClassName>of(),
ImmutableMap.copyOf(nameToJavaName),
ImmutableMap.<ProtoType, AdapterConstant>of(), false, false);
return new JavaGenerator(schema, nameToJavaName, new Profile(), false, false);
}

private static void putAll(Map<ProtoType, ClassName> wireToJava, String javaPackage,
Expand All @@ -259,6 +228,8 @@ public Schema schema() {
* if that type wasn't in this generator's schema.
*/
public TypeName typeName(ProtoType protoType) {
TypeName profileJavaName = profile.getTarget(protoType);
if (profileJavaName != null) return profileJavaName;
TypeName candidate = nameToJavaName.get(protoType);
checkArgument(candidate != null, "unexpected type %s", protoType);
return candidate;
Expand All @@ -269,7 +240,11 @@ public TypeName typeName(ProtoType protoType) {
* protoType}. Returns null if {@code protoType} is not using a custom proto adapter.
*/
public ClassName abstractAdapterName(ProtoType protoType) {
return nameToAbstractAdapterName.get(protoType);
TypeName profileJavaName = profile.getTarget(protoType);
if (profileJavaName == null) return null;

ClassName javaName = nameToJavaName.get(protoType);
return javaName.peerClass("Abstract" + javaName.simpleName() + "Adapter");
}

private CodeBlock singleAdapterFor(Field field) {
Expand All @@ -285,9 +260,9 @@ private CodeBlock singleAdapterFor(ProtoType type) {
} else if (type.isMap()) {
throw new IllegalArgumentException("Cannot create single adapter for map type " + type);
} else {
AdapterConstant adapterConstant = protoTypeToAdapterConstant.get(type);
AdapterConstant adapterConstant = profile.getAdapter(type);
if (adapterConstant != null) {
result.add("$T.$L", adapterConstant.className, adapterConstant.adapterName);
result.add("$T.$L", adapterConstant.className, adapterConstant.memberName);
} else {
result.add("$T.ADAPTER", typeName(type));
}
Expand Down Expand Up @@ -368,7 +343,8 @@ public ClassName generatedTypeName(Type type) {
/** Returns the generated code for {@code type}, which may be a top-level or a nested type. */
public TypeSpec generateType(Type type) {
if (type instanceof MessageType) {
if (protoTypeToAdapterConstant.containsKey(type.type())) {
AdapterConstant adapterConstant = profile.getAdapter(type.type());
if (adapterConstant != null) {
return generateAbstractAdapter((MessageType) type);
}
//noinspection deprecation: Only deprecated as a public API.
Expand Down Expand Up @@ -632,7 +608,7 @@ public TypeSpec generateAbstractAdapter(MessageType type) {
nameAllocator, type, typeName, adapterTypeName, null).toBuilder();

for (Type nestedType : type.nestedTypes()) {
if (!protoTypeToAdapterConstant.containsKey(nestedType.type())) {
if (profile.getAdapter(nestedType.type()) == null) {
throw new IllegalArgumentException("Missing custom proto adapter for "
+ nestedType.type().enclosingTypeOrPackage() + "." + nestedType.type().simpleName()
+ " when enclosing proto has custom proto adapter.");
Expand Down Expand Up @@ -1038,9 +1014,9 @@ private String adapterString(ProtoType type) {
return ProtoAdapter.class.getName() + '#' + type.toString().toUpperCase(Locale.US);
}

AdapterConstant adapterConstant = protoTypeToAdapterConstant.get(type);
AdapterConstant adapterConstant = profile.getAdapter(type);
if (adapterConstant != null) {
return reflectionName(adapterConstant.className) + "#" + adapterConstant.adapterName;
return reflectionName(adapterConstant.className) + "#" + adapterConstant.memberName;
}

return reflectionName((ClassName) typeName(type)) + "#ADAPTER";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,18 @@ public final class Profile {
this.profileFiles = profileFiles;
}

public Profile() {
this(ImmutableList.<ProfileFileElement>of());
}

public TypeName getTarget(ProtoType type) {
TypeConfigElement typeConfig = typeConfig(type);
return typeConfig != null ? ClassName.bestGuess(typeConfig.target()) : null;
}

public String getAdapter(ProtoType type) {
public AdapterConstant getAdapter(ProtoType type) {
TypeConfigElement typeConfig = typeConfig(type);
return typeConfig != null ? typeConfig.adapter() : null;
return typeConfig != null ? new AdapterConstant(typeConfig.adapter()) : null;
}

/** Returns the config for {@code type}, or null if it is not configured. */
Expand Down

This file was deleted.

Loading

0 comments on commit a697c58

Please sign in to comment.