/** * Returns true if {@code identifier} or any of its enclosing identifiers is included. If any * enclosing identifier is excluded, that takes precedence and this returns false. */ private boolean includes(String identifier) { if (includes.isEmpty()) return !exclude(identifier); String includeMatch = null; String excludeMatch = null; for (String rule = identifier; rule != null; rule = enclosing(rule)) { if (excludes.contains(rule)) { excludeMatch = rule; } if (includes.contains(rule)) { includeMatch = rule; } } if (excludeMatch != null) { usedExcludes.add(excludeMatch); return false; } if (includeMatch != null) { usedIncludes.add(includeMatch); return true; } return false; }
/** * Marks a type as transitively reachable by the includes set. Returns true if the mark is new, * the type will be retained, and its own dependencies should be traversed. */ boolean mark(ProtoType type) { if (type == null) throw new NullPointerException("type == null"); if (identifierSet.excludes(type)) return false; return types.add(type); }
/** Returns true if {@code protoMember} is a root. */ public boolean includes(ProtoMember protoMember) { return includes(protoMember.toString()); }
private Schema retainRoots(IdentifierSet identifierSet, Schema schema) { Stopwatch stopwatch = Stopwatch.createStarted(); int oldSize = countTypes(schema); Schema prunedSchema = schema.prune(identifierSet); int newSize = countTypes(prunedSchema); for (String rule : identifierSet.unusedIncludes()) { getLog().warn(String.format("Unused include: %s", rule)); } for (String rule : identifierSet.unusedExcludes()) { getLog().warn(String.format("Unused exclude: %s", rule)); } getLog().info(String.format("Pruned schema from %s types to %s types in %s", oldSize, newSize, stopwatch)); return prunedSchema; }
private Policy policy(IdentifierSet set, String identifier) { if (identifier.contains("#")) { ProtoMember protoMember = ProtoMember.get(identifier); if (set.includes(protoMember)) return Policy.INCLUDED; if (set.excludes(protoMember)) return Policy.EXCLUDED; return Policy.UNSPECIFIED; } else { ProtoType protoType = ProtoType.get(identifier); if (set.includes(protoType)) return Policy.INCLUDED; if (set.excludes(protoType)) return Policy.EXCLUDED; return Policy.UNSPECIFIED; } }
@Test public void trackingUnusedPrecedence() throws Exception { IdentifierSet set = new IdentifierSet.Builder() .include("a.*") .exclude("a.*") .build(); set.includes(ProtoType.get("a.Message")); assertThat(set.unusedExcludes()).isEmpty(); assertThat(set.unusedIncludes()).containsExactly("a.*"); }
/** Returns true if {@code identifier} or any of its enclosing identifiers is excluded. */ private boolean exclude(String identifier) { String excludeMatch = null; for (String rule = identifier; rule != null; rule = enclosing(rule)) { if (excludes.contains(rule)) { excludeMatch = rule; } } if (excludeMatch != null) { usedExcludes.add(excludeMatch); return true; } return false; }
@Test public void trackingUnusedExcludes() throws Exception { IdentifierSet set = new IdentifierSet.Builder() .exclude("a.*") .exclude("b.ExcludedType") .exclude("c.ExcludedMember#member") .build(); assertThat(set.unusedExcludes()).containsExactly( "a.*", "b.ExcludedType", "c.ExcludedMember#member"); set.includes(ProtoType.get("a.*")); assertThat(set.unusedExcludes()).containsExactly( "b.ExcludedType", "c.ExcludedMember#member"); set.includes(ProtoType.get("b.ExcludedType")); assertThat(set.unusedExcludes()).containsExactly( "c.ExcludedMember#member"); set.includes(ProtoMember.get("c.ExcludedMember#member")); assertThat(set.unusedExcludes()).isEmpty(); }
@Test public void trackingUnusedIncludes() throws Exception { IdentifierSet set = new IdentifierSet.Builder() .include("a.*") .include("b.IncludedType") .include("c.IncludedMember#member") .build(); assertThat(set.unusedIncludes()).containsExactly( "a.*", "b.IncludedType", "c.IncludedMember#member"); set.includes(ProtoType.get("a.*")); assertThat(set.unusedIncludes()).containsExactly( "b.IncludedType", "c.IncludedMember#member"); set.includes(ProtoType.get("b.IncludedType")); assertThat(set.unusedIncludes()).containsExactly( "c.IncludedMember#member"); set.includes(ProtoMember.get("c.IncludedMember#member")); assertThat(set.unusedIncludes()).isEmpty(); }
/** * Returns true if {@code type} should be excluded, even if it is a transitive dependency of a * root. In that case, the referring member is also excluded. */ public boolean excludes(ProtoType type) { return exclude(type.toString()); }
public IdentifierSet build() { return new IdentifierSet(this); } }
if (!identifierSet.isEmpty()) { schema = retainRoots(identifierSet, schema);
@Test public void enclosing() throws Exception { assertThat(IdentifierSet.enclosing("a.b.Outer#member")).isEqualTo("a.b.Outer"); assertThat(IdentifierSet.enclosing("a.b.Outer")).isEqualTo("a.b.*"); assertThat(IdentifierSet.enclosing("a.b.*")).isEqualTo("a.*"); assertThat(IdentifierSet.enclosing("a.*")).isEqualTo("*"); assertThat(IdentifierSet.enclosing("*")).isNull(); }
@Test public void trackingUnusedExcludesPrecedence() throws Exception { IdentifierSet set = new IdentifierSet.Builder() .exclude("a.*") .exclude("a.IncludedType") .build(); set.includes(ProtoMember.get("a.IncludedType#member")); assertThat(set.unusedExcludes()).containsExactly("a.IncludedType"); }
@Test public void trackingUnusedIncludesPrecedence() throws Exception { IdentifierSet set = new IdentifierSet.Builder() .include("a.*") .include("a.IncludedType") .build(); set.includes(ProtoMember.get("a.IncludedType#member")); assertThat(set.unusedIncludes()).containsExactly("a.IncludedType"); }
/** Returns true if {@code protoMember} should be excluded. */ public boolean excludes(ProtoMember protoMember) { return exclude(protoMember.toString()); }
public IdentifierSet build() { return new IdentifierSet(this); } }
public void execute() throws IOException { Schema schema = loadSchema(); if (!identifierSet.isEmpty()) { schema = retainRoots(schema); } JavaGenerator javaGenerator = JavaGenerator.get(schema); ServiceGenerator serviceGenerator = new ServiceGenerator(javaGenerator); for (ProtoFile protoFile : schema.protoFiles()) { for (Type type : protoFile.types()) { Stopwatch stopwatch = Stopwatch.createStarted(); TypeSpec typeSpec = javaGenerator.generateType(type); ClassName javaTypeName = (ClassName) javaGenerator.typeName(type.type()); writeJavaFile(javaTypeName, typeSpec, type.location(), stopwatch); } for (Service service : protoFile.services()) { Stopwatch stopwatch = Stopwatch.createStarted(); ClassName javaTypeName = (ClassName) javaGenerator.typeName(service.type()); TypeSpec typeSpec = serviceGenerator.api(service); writeJavaFile(javaTypeName, typeSpec, service.location(), stopwatch); } } }
/** Returns true if {@code type} is a root. */ public boolean includes(ProtoType type) { return includes(type.toString()); }
/** * Marks {@code type}, throwing if it is explicitly excluded, or if any of its members are also * specifically included. */ void root(ProtoType type) { if (type == null) throw new NullPointerException("type == null"); checkArgument(!identifierSet.excludes(type)); checkArgument(!members.containsKey(type)); types.add(type); }