Permalink
Browse files

Merge #223 pull request into 2.3.0 branch, add support for handling J…

…ava 8 default interface methods.
  • Loading branch information...
brettwooldridge committed Jan 8, 2015
1 parent 42a79a7 commit e3316e8618a1b00a180468041734a042738d161b
@@ -59,6 +59,7 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>src</id>
@@ -16,12 +16,15 @@
package com.zaxxer.hikari.proxy;
import java.lang.reflect.Array;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javassist.ClassPool;
@@ -71,8 +74,7 @@ public static void initialize()
// no-op
}
private JavassistProxyFactory() throws Exception
{
private JavassistProxyFactory() throws Exception {
classPool = new ClassPool();
classPool.importPackage("java.sql");
classPool.appendClassPath(new LoaderClassPath(this.getClass().getClassLoader()));
@@ -153,6 +155,11 @@ else if ("getProxyResultSet".equals(methodName)) {
continue;
}
// Ignore default methods (only for Jre8 or later)
if (isDefaultMethod(intf, intfCt, intfMethod)) {
continue;
}
// Track what methods we've added
methods.add(signature);
@@ -206,4 +213,58 @@ private boolean isThrowsSqlException(CtMethod method)
return false;
}
private boolean isDefaultMethod(Class<?> intf, CtClass intfCt, CtMethod intfMethod) throws Exception
{
List<Class<?>> paramTypes = new ArrayList<Class<?>>();
for (CtClass pt : intfMethod.getParameterTypes()) {
paramTypes.add(toJavaClass(pt));
}
return intf.getDeclaredMethod(intfMethod.getName(), paramTypes.toArray(new Class[0])).toString().contains("default ");
}
private Class<?> toJavaClass(CtClass cls) throws Exception
{
if (cls.getName().endsWith("[]")) {
return Array.newInstance(toJavaClass(cls.getName().replace("[]", "")), 0).getClass();
}
else {
return toJavaClass(cls.getName());
}
}
private Class<?> toJavaClass(String cn) throws Exception
{
if ("int".equals(cn)) {
return int.class;
}
if ("long".equals(cn)) {
return long.class;
}
if ("short".equals(cn)) {
return short.class;
}
if ("byte".equals(cn)) {
return byte.class;
}
if ("float".equals(cn)) {
return float.class;
}
if ("double".equals(cn)) {
return double.class;
}
if ("boolean".equals(cn)) {
return boolean.class;
}
if ("char".equals(cn)) {
return char.class;
}
if ("void".equals(cn)) {
return void.class;
}
return Class.forName(cn);
}
}
@@ -48,7 +48,7 @@
systemProperty("org.ops4j.pax.logging.DefaultServiceLog.level").value("WARN"),
mavenBundle("org.slf4j","slf4j-api","1.7.5"),
mavenBundle("org.slf4j","slf4j-simple","1.7.5").noStart(),
mavenBundle("org.javassist", "javassist", "3.18.1-GA"),
mavenBundle("org.javassist", "javassist", "3.19.0-GA"),
new File("target/classes").exists()
? bundle("reference:file:target/classes")
: bundle("reference:file:../target/classes"),
@@ -126,6 +126,7 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>src</id>
@@ -16,12 +16,15 @@
package com.zaxxer.hikari.proxy;
import java.lang.reflect.Array;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javassist.ClassPool;
@@ -154,6 +157,11 @@ private void modifyProxyFactory() throws Exception
continue;
}
// Ignore default methods (only for Jre8 or later)
if (isDefaultMethod(intf, intfCt, intfMethod)) {
continue;
}
// Track what methods we've added
methods.add(signature);
@@ -207,4 +215,51 @@ private boolean isThrowsSqlException(CtMethod method)
return false;
}
private boolean isDefaultMethod(Class<?> intf, CtClass intfCt, CtMethod intfMethod) throws Exception
{
List<Class<?>> paramTypes = new ArrayList<Class<?>>();
for (CtClass pt : intfMethod.getParameterTypes()) {
paramTypes.add(toJavaClass(pt));
}
return intf.getDeclaredMethod(intfMethod.getName(), paramTypes.toArray(new Class[0])).toString().contains("default ");
}
private Class<?> toJavaClass(CtClass cls) throws Exception
{
if (cls.getName().endsWith("[]")) {
return Array.newInstance(toJavaClass(cls.getName().replace("[]", "")), 0).getClass();
}
else {
return toJavaClass(cls.getName());
}
}
private Class<?> toJavaClass(String cn) throws Exception
{
switch (cn) {
case "int":
return int.class;
case "long":
return long.class;
case "short":
return short.class;
case "byte":
return byte.class;
case "float":
return float.class;
case "double":
return double.class;
case "boolean":
return boolean.class;
case "char":
return char.class;
case "void":
return void.class;
default:
return Class.forName(cn);
}
}
}
@@ -48,7 +48,7 @@
systemProperty("org.ops4j.pax.logging.DefaultServiceLog.level").value("WARN"),
mavenBundle("org.slf4j","slf4j-api","1.7.5"),
mavenBundle("org.slf4j","slf4j-simple","1.7.5").noStart(),
mavenBundle("org.javassist", "javassist", "3.18.1-GA"),
mavenBundle("org.javassist", "javassist", "3.19.0-GA"),
new File("target/classes").exists()
? bundle("reference:file:target/classes")
: bundle("reference:file:../target/classes"),
@@ -77,7 +77,7 @@
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.18.1-GA</version>
<version>3.19.0-GA</version>
<scope>compile</scope>
</dependency>
<dependency>

0 comments on commit e3316e8

Please sign in to comment.