github-code-scanning[bot] commented on code in PR #2322:
URL: https://github.com/apache/avro/pull/2322#discussion_r1298098555
##########
lang/java/compiler/src/test/java/org/apache/avro/compiler/specific/TestSpecificCompiler.java:
##########
@@ -922,6 +927,61 @@
}
}
+ @Test
+ void inheritance() throws IOException, ClassNotFoundException,
InstantiationException, IllegalAccessException {
+ File parentFile = new File("src/test/resources/inheritance/parent.avsc");
+ Assertions.assertTrue(parentFile.exists());
+ File childFile = new File("src/test/resources/inheritance/child.avsc");
+ SpecificCompiler.compileSchema(new File[] { parentFile, childFile },
OUTPUT_DIR);
+
+ File f1 = new File(this.OUTPUT_DIR, "Parent.java");
+ File f2 = new File(this.OUTPUT_DIR, "Child.java");
+ Assertions.assertTrue(f2.exists());
+
+ DiagnosticCollector<JavaFileObject> diagnostics = new
DiagnosticCollector<>();
+ JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+ StandardJavaFileManager fileManager =
compiler.getStandardFileManager(diagnostics, null, null);
+
+ // This sets up the class path that the compiler will use.
+ // I've added the .jar file that contains the DoStuff interface within in
it...
+ List<String> optionList = Collections.emptyList(); //
Arrays.asList("-classpath", this.OUTPUT_DIR.getName());
+
+ Iterable<? extends JavaFileObject> compilationUnit =
fileManager.getJavaFileObjectsFromFiles(Arrays.asList(f1, f2));
+ JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager,
diagnostics, optionList, null,
+ compilationUnit);
+
/*********************************************************************************************
+ * Compilation Requirements
+ **/
+ if (task.call()) {
+ /**
+ * Load and execute
+
*************************************************************************************************/
+ // Create a new custom class loader, pointing to the directory that
contains the
+ // compiled
+ // classes, this should point to the top of the package structure!
+ URLClassLoader classLoader = new URLClassLoader(new URL[] {
this.OUTPUT_DIR.toURI().toURL() },
+ Thread.currentThread().getContextClassLoader());
+ // Load the class from the classloader by name....
+ Class<?> parentClass = classLoader.loadClass("Parent");
+ // Create a new instance...
+ Object obj = parentClass.newInstance();
+ Assertions.assertNotNull(obj);
+
+ Class<?> childClass = classLoader.loadClass("Child");
+ Assertions.assertEquals(parentClass, childClass.getSuperclass());
+ Object childObj = childClass.newInstance();
Review Comment:
## Deprecated method or constructor invocation
Invoking [Class.newInstance](1) should be avoided because it has been
deprecated.
[Show more
details](https://github.com/apache/avro/security/code-scanning/3132)
##########
lang/java/compiler/src/test/java/org/apache/avro/compiler/specific/TestSpecificCompiler.java:
##########
@@ -922,6 +927,61 @@
}
}
+ @Test
+ void inheritance() throws IOException, ClassNotFoundException,
InstantiationException, IllegalAccessException {
+ File parentFile = new File("src/test/resources/inheritance/parent.avsc");
+ Assertions.assertTrue(parentFile.exists());
+ File childFile = new File("src/test/resources/inheritance/child.avsc");
+ SpecificCompiler.compileSchema(new File[] { parentFile, childFile },
OUTPUT_DIR);
+
+ File f1 = new File(this.OUTPUT_DIR, "Parent.java");
+ File f2 = new File(this.OUTPUT_DIR, "Child.java");
+ Assertions.assertTrue(f2.exists());
+
+ DiagnosticCollector<JavaFileObject> diagnostics = new
DiagnosticCollector<>();
+ JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+ StandardJavaFileManager fileManager =
compiler.getStandardFileManager(diagnostics, null, null);
+
+ // This sets up the class path that the compiler will use.
+ // I've added the .jar file that contains the DoStuff interface within in
it...
+ List<String> optionList = Collections.emptyList(); //
Arrays.asList("-classpath", this.OUTPUT_DIR.getName());
+
+ Iterable<? extends JavaFileObject> compilationUnit =
fileManager.getJavaFileObjectsFromFiles(Arrays.asList(f1, f2));
+ JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager,
diagnostics, optionList, null,
+ compilationUnit);
+
/*********************************************************************************************
+ * Compilation Requirements
+ **/
+ if (task.call()) {
+ /**
+ * Load and execute
+
*************************************************************************************************/
+ // Create a new custom class loader, pointing to the directory that
contains the
+ // compiled
+ // classes, this should point to the top of the package structure!
+ URLClassLoader classLoader = new URLClassLoader(new URL[] {
this.OUTPUT_DIR.toURI().toURL() },
+ Thread.currentThread().getContextClassLoader());
+ // Load the class from the classloader by name....
+ Class<?> parentClass = classLoader.loadClass("Parent");
+ // Create a new instance...
+ Object obj = parentClass.newInstance();
Review Comment:
## Deprecated method or constructor invocation
Invoking [Class.newInstance](1) should be avoided because it has been
deprecated.
[Show more
details](https://github.com/apache/avro/security/code-scanning/3131)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]