Skip to content

Commit

Permalink
generate performance api directory matching package
Browse files Browse the repository at this point in the history
  • Loading branch information
viqtor authored and Sunjeet committed Sep 8, 2023
1 parent 7f6c9c9 commit 391578b
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public String generate() {
"import com.netflix.hollow.api.perfapi.Ref;\n" +
"import com.netflix.hollow.core.read.dataaccess.HollowDataAccess;\n\n");

builder.append("@SuppressWarnings(\"all\")\n");
builder.append("public class " + schema.getName() + "PerfAPI extends HollowObjectTypePerfAPI {\n\n");

builder.append(" public static final String fieldNames[] = { ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public String generate() {
"import java.util.Set;\n" +
"\n");


builder.append("@SuppressWarnings(\"all\")\n");
builder.append("public class " + apiClassName + " extends HollowPerformanceAPI {\n\n");

List<HollowSchema> schemas = new ArrayList<>(dataset.getSchemas());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.netflix.hollow.core.schema.HollowObjectSchema;
import com.netflix.hollow.core.schema.HollowSchema;
import com.netflix.hollow.core.schema.HollowSchema.SchemaType;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
Expand Down Expand Up @@ -91,6 +92,10 @@ public void generateSourceFiles() throws IOException {
}

private void generate(HollowDataset dataset, String packageName, String apiClassName, Path destination, Set<String> checkFieldExistsMethods) throws IOException {
Path packagePath = Paths.get(packageName.replace(".", File.separator));
if (!destination.toAbsolutePath().endsWith(packagePath)) {
destination = destination.resolve(packagePath);
}
Path apiClassDestination = destination.resolve(apiClassName + ".java");
if (!Files.exists(apiClassDestination)) {
Files.createDirectories(destination);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@

public class AbstractHollowAPIGeneratorTest {
private String tmpFolder = System.getProperty("java.io.tmpdir");
private String sourceFolder = String.format("%s/src", tmpFolder);
private String clazzFolder = String.format("%s/classes", tmpFolder);
protected String sourceFolder = String.format("%s/src", tmpFolder);
protected String clazzFolder = String.format("%s/classes", tmpFolder);

void runGenerator(String apiClassName, String packageName, Class<?> clazz,
UnaryOperator<HollowAPIGenerator.Builder> generatorCustomizer) throws Exception {
Expand All @@ -53,7 +53,7 @@ void runGenerator(String apiClassName, String packageName, Class<?> clazz,
HollowCodeGenerationCompileUtil.compileSrcFiles(sourceFolder, clazzFolder);
}

void assertNonEmptyFileExists(String relativePath) throws IOException {
protected void assertNonEmptyFileExists(String relativePath) throws IOException {
if (relativePath.startsWith("/")) {
throw new IllegalArgumentException("Relative paths should not start with /");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,11 @@ public static void compileSrcFiles(String sourceDirPath, String classDirPath) th
*/
private static void runFindbugs(File classDir) throws Exception {
ClassLoader classLoader = HollowCodeGenerationCompileUtil.class.getClassLoader();
FindBugs2.main(new String[]{"-auxclasspath", System.getProperty(PROPERTY_CLASSPATH), "-output",
classLoader.getResource("").getFile() + FILENAME_FINDBUGS, classDir.getAbsolutePath()});
FindBugs2.main(
new String[]{"-auxclasspath", System.getProperty(PROPERTY_CLASSPATH), "-output",
classLoader.getResource("").getFile() + FILENAME_FINDBUGS,
"-exclude", classLoader.getResource("findbugs_exclude.xml").getFile(),
classDir.getAbsolutePath()});
BufferedReader reader =
new BufferedReader(new InputStreamReader(classLoader.getResourceAsStream(FILENAME_FINDBUGS)));
String line = "";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.netflix.hollow.api.codegen.perfapi;

import com.netflix.hollow.api.codegen.AbstractHollowAPIGeneratorTest;
import com.netflix.hollow.api.codegen.HollowCodeGenerationCompileUtil;
import com.netflix.hollow.core.schema.SimpleHollowDataset;
import java.io.File;
import org.junit.Test;

public class HollowPerformanceAPIGeneratorTest extends AbstractHollowAPIGeneratorTest {

@Test
public void testGeneratePerformanceApi() throws Exception {
runGenerator("API", "com.netflix.hollow.example.api.performance.generated", MyClass.class);
}

@Test
public void testGeneratedFilesArePlacedInPackageDirectory() throws Exception {
runGenerator("API", "codegen.api", MyClass.class);
assertNonEmptyFileExists("codegen/api/MyClassPerfAPI.java");
assertNonEmptyFileExists("codegen/api/StringPerfAPI.java");
assertNonEmptyFileExists("codegen/api/API.java");
}

private void runGenerator(String apiClassName, String packageName, Class<?> clazz) throws Exception {
System.out.println(String.format("Folders (%s) : \n\tsource=%s \n\tclasses=%s",
getClass().getSimpleName(), sourceFolder, clazzFolder));

// Setup Folders
HollowCodeGenerationCompileUtil.cleanupFolder(new File(sourceFolder), null);
HollowCodeGenerationCompileUtil.cleanupFolder(new File(clazzFolder), null);

// Run generator
HollowPerformanceAPIGenerator generator = HollowPerformanceAPIGenerator.newBuilder()
.withDestination(sourceFolder)
.withPackageName(packageName)
.withAPIClassname(apiClassName)
.withDataset(SimpleHollowDataset.fromClassDefinitions(clazz))
.build();
generator.generateSourceFiles();

// Compile to validate generated files
HollowCodeGenerationCompileUtil.compileSrcFiles(sourceFolder, clazzFolder);
}

@SuppressWarnings("unused")
private static class MyClass {
int id;
String foo;

public MyClass(int id, String foo) {
this.id = id;
this.foo = foo;
}
}
}
12 changes: 12 additions & 0 deletions hollow/src/test/resources/findbugs_exclude.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<FindBugsFilter
xmlns="https://github.com/spotbugs/filter/3.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://github.com/spotbugs/filter/3.0.0 https://raw.githubusercontent.com/spotbugs/spotbugs/3.1.0/spotbugs/etc/findbugsfilter.xsd">

<Match>
<Class name="~.*\.*PerfAPI" />
<Field name="fieldNames" />
<Bug pattern="MS_PKGPROTECT" />
</Match>
</FindBugsFilter>

0 comments on commit 391578b

Please sign in to comment.