Skip to content

Commit

Permalink
Generate *ProtoFields classes instead of value objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewdu authored and squarejesse committed Jun 25, 2016
1 parent 7e834cb commit beef383
Show file tree
Hide file tree
Showing 31 changed files with 430 additions and 46 deletions.
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
<module>wire-maven-plugin</module>
<module>wire-runtime</module>
<module>wire-schema</module>
<module>wire-schema-tests</module>
<module>wire-testing</module>
</modules>

<properties>
Expand Down Expand Up @@ -104,7 +106,6 @@
<groupId>com.google.jimfs</groupId>
<artifactId>jimfs</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
Expand Down
39 changes: 36 additions & 3 deletions wire-compiler/src/main/java/com/squareup/wire/WireCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@
*/
package com.squareup.wire;

import com.google.common.collect.ImmutableMap;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeSpec;
import com.squareup.wire.java.AdapterConstant;
import com.squareup.wire.java.JavaGenerator;
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 @@ -33,7 +37,9 @@
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 @@ -94,6 +100,7 @@ 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 @@ -111,10 +118,14 @@ 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) {
boolean namedFilesOnly, boolean emitAndroid, boolean emitCompact,
Map<ProtoType, ClassName> protoTypeToJavaClassName,
Map<ProtoType, AdapterConstant> protoTypeToAdapterConstant) {
this.fs = fs;
this.log = log;
this.protoPaths = protoPaths;
Expand All @@ -125,6 +136,8 @@ 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 @@ -153,6 +166,8 @@ 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 @@ -176,6 +191,18 @@ 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 @@ -200,7 +227,8 @@ static WireCompiler forArgs(
logger.setQuiet(quiet);

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

private static List<String> splitArg(String arg, int flagLength) {
Expand Down Expand Up @@ -229,6 +257,7 @@ void compile() throws IOException {
}

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

Expand Down Expand Up @@ -277,7 +306,11 @@ public Void call() throws IOException {
}

TypeSpec typeSpec = javaGenerator.generateType(type);
ClassName javaTypeName = (ClassName) javaGenerator.typeName(type.type());
TypeName typeName = javaGenerator.protoFieldsTypeName(type.type());
if (typeName == null) {
typeName = javaGenerator.typeName(type.type());
}
ClassName javaTypeName = (ClassName) typeName;
Location location = type.location();

JavaFile.Builder builder = JavaFile.builder(javaTypeName.packageName(), typeSpec)
Expand Down
6 changes: 6 additions & 0 deletions wire-java-generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@
<artifactId>javapoet</artifactId>
</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
@@ -0,0 +1,43 @@
/*
* Copyright (C) 2016 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.squareup.wire.java;

import com.squareup.javapoet.ClassName;
import com.squareup.wire.ProtoAdapter;

/**
* A constant field that identifies a {@link ProtoAdapter}. This should be a string like like {@code
* com.squareup.dinosaurs.Dinosaur#ADAPTER} with a fully qualified class name, a {@code #}, and a
* field name.
*/
public final class AdapterConstant {
public final ClassName className;
public final String adapterName;

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

public AdapterConstant(String adapter) {
String[] names = adapter.split("#");
if (names.length != 2) {
throw new IllegalArgumentException("Illegally formatted adapter: " + adapter + ".");
}
this.className = ClassName.bestGuess(names[0]);
this.adapterName = names[1];
}
}
Loading

0 comments on commit beef383

Please sign in to comment.