Improve JavassistProxyFactory (#2244)

1. use `"target" + File.separator + "classes"` instead of `"target/classes"`
2. print target directory
pull/2238/head
Yanming Zhou 4 months ago committed by GitHub
parent aa1e138fbd
commit 4bc96d16c2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -421,7 +421,7 @@
<argument>-cp</argument> <argument>-cp</argument>
<argument>${project.build.outputDirectory}${path.separator}${maven.compile.classpath}</argument> <argument>${project.build.outputDirectory}${path.separator}${maven.compile.classpath}</argument>
<argument>com.zaxxer.hikari.util.JavassistProxyFactory</argument> <argument>com.zaxxer.hikari.util.JavassistProxyFactory</argument>
<argument>${project.basedir}${file.separator}</argument> <argument>${project.basedir}</argument>
</arguments> </arguments>
</configuration> </configuration>
</plugin> </plugin>

@ -20,6 +20,7 @@ import com.zaxxer.hikari.pool.*;
import javassist.*; import javassist.*;
import javassist.bytecode.ClassFile; import javassist.bytecode.ClassFile;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.sql.*; import java.sql.*;
@ -35,11 +36,12 @@ import java.util.Set;
* instances of the generated proxies. * instances of the generated proxies.
* *
* @author Brett Wooldridge * @author Brett Wooldridge
* @author Yanming Zhou
*/ */
public final class JavassistProxyFactory public final class JavassistProxyFactory
{ {
private static ClassPool classPool; private static ClassPool classPool;
private static String genDirectory = ""; private static String genDirectory = "target" + File.separator + "classes";
public static void main(String... args) throws Exception { public static void main(String... args) throws Exception {
classPool = new ClassPool(); classPool = new ClassPool();
@ -47,8 +49,14 @@ public final class JavassistProxyFactory
classPool.appendClassPath(new LoaderClassPath(JavassistProxyFactory.class.getClassLoader())); classPool.appendClassPath(new LoaderClassPath(JavassistProxyFactory.class.getClassLoader()));
if (args.length > 0) { if (args.length > 0) {
genDirectory = args[0]; String parentDir = args[0];
if (!parentDir.endsWith(File.separator)) {
parentDir += File.separator;
} }
genDirectory = parentDir + genDirectory;
}
System.out.println("Generating following classes to " + genDirectory);
// Cast is not needed for these // Cast is not needed for these
String methodBody = "{ try { return delegate.method($$); } catch (SQLException e) { throw checkException(e); } }"; String methodBody = "{ try { return delegate.method($$); } catch (SQLException e) { throw checkException(e); } }";
@ -96,7 +104,7 @@ public final class JavassistProxyFactory
} }
} }
proxyCt.writeFile(genDirectory + "target/classes"); proxyCt.writeFile(genDirectory);
} }
/** /**
@ -173,7 +181,7 @@ public final class JavassistProxyFactory
} }
targetCt.getClassFile().setMajorVersion(ClassFile.JAVA_8); targetCt.getClassFile().setMajorVersion(ClassFile.JAVA_8);
targetCt.writeFile(genDirectory + "target/classes"); targetCt.writeFile(genDirectory);
} }
private static boolean isThrowsSqlException(CtMethod method) private static boolean isThrowsSqlException(CtMethod method)

@ -19,7 +19,7 @@ public class TestJavassistCodegen {
@Test @Test
public void testCodegen() throws Exception { public void testCodegen() throws Exception {
String tmp = System.getProperty("java.io.tmpdir"); String tmp = System.getProperty("java.io.tmpdir");
JavassistProxyFactory.main(tmp + (tmp.endsWith("/") ? "" : "/")); JavassistProxyFactory.main(tmp);
Path base = Paths.get(tmp, "target/classes/com/zaxxer/hikari/pool".split("/")); Path base = Paths.get(tmp, "target/classes/com/zaxxer/hikari/pool".split("/"));
Assert.assertTrue("", Files.isRegularFile(base.resolve("HikariProxyConnection.class"))); Assert.assertTrue("", Files.isRegularFile(base.resolve("HikariProxyConnection.class")));

Loading…
Cancel
Save