Skip to content

Commit 141b338

Browse files
committed
Update all deprecations to new API and fix a Java newInstance deprecation
1 parent 59ba043 commit 141b338

24 files changed

+381
-329
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<url>http://github.com/jruby/activerecord-jdbc-adapter/wiki</url>
1313

1414
<properties>
15-
<jruby.version>9.2.6.0</jruby.version>
15+
<jruby.version>10.0.0.1</jruby.version>
1616
</properties>
1717

1818
<issueManagement>

src/java/arjdbc/ArJdbcModule.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343

4444
import arjdbc.jdbc.RubyJdbcConnection;
4545
import static arjdbc.jdbc.RubyJdbcConnection.getJdbcConnection;
46+
import static org.jruby.api.Access.getModule;
47+
import static org.jruby.api.Access.objectClass;
48+
import static org.jruby.api.Create.allocArray;
4649

4750
/**
4851
* ::ArJdbc
@@ -52,9 +55,10 @@
5255
public class ArJdbcModule {
5356

5457
public static RubyModule load(final Ruby runtime) {
55-
final RubyModule arJdbc = runtime.getOrCreateModule("ArJdbc");
56-
arJdbc.defineAnnotatedMethods( ArJdbcModule.class );
57-
return arJdbc;
58+
var context = runtime.getCurrentContext();
59+
return objectClass(context).
60+
defineModuleUnder(context, "ArJdbc").
61+
defineMethods(context, ArJdbcModule.class);
5862
}
5963

6064
public static RubyModule get(final Ruby runtime) {
@@ -142,7 +146,7 @@ public static IRubyObject load_java_part(final ThreadContext context,
142146
catch (NoSuchMethodException e) {
143147
// "old" e.g. MySQLRubyJdbcConnection.createMySQLJdbcConnectionClass(runtime, jdbcConnection)
144148
connection.getMethod("create" + moduleName + "JdbcConnectionClass", Ruby.class, RubyClass.class).
145-
invoke(null, runtime, getJdbcConnection(runtime));
149+
invoke(null, runtime, getJdbcConnection(context));
146150
}
147151
}
148152
}
@@ -162,20 +166,19 @@ public static IRubyObject load_java_part(final ThreadContext context,
162166
*/
163167
@JRubyMethod(name = "modules", meta = true)
164168
public static IRubyObject modules(final ThreadContext context, final IRubyObject self) {
165-
final Ruby runtime = context.getRuntime();
166169
final RubyModule arJdbc = (RubyModule) self;
167170

168171
final Collection<String> constants = arJdbc.getConstantNames();
169-
final RubyArray modules = runtime.newArray( constants.size() );
172+
final RubyArray modules = allocArray(context, constants.size());
170173

171174
for ( final String name : constants ) {
172-
final IRubyObject constant = arJdbc.getConstant(name, false);
175+
final IRubyObject constant = arJdbc.getConstant(context, name, false);
173176
// isModule: return false for Ruby Classes
174177
if ( constant != null && constant.isModule() ) {
175178
if ( "Util".equals(name) ) continue;
176179
if ( "SerializedAttributesHelper".equals(name) ) continue; // deprecated
177180
if ( "Version".equals(name) ) continue; // deprecated
178-
modules.append( constant );
181+
modules.append(context, constant);
179182
}
180183
}
181184
return modules;
@@ -230,9 +233,9 @@ private static IRubyObject loadDriver(final ThreadContext context, final IRubyOb
230233
return null;
231234
}
232235

233-
final RubyModule jdbc = runtime.getModule("Jdbc");
236+
final RubyModule jdbc = getModule(context, "Jdbc");
234237
if ( jdbc != null ) { // Jdbc::MySQL
235-
final RubyModule constant = (RubyModule) jdbc.getConstantAt(constName);
238+
final RubyModule constant = jdbc.getModule(context, constName);
236239
if ( constant != null ) { // ::Jdbc::MySQL.load_driver :
237240
if ( constant.respondsTo("load_driver") ) {
238241
IRubyObject result = constant.callMethod("load_driver");

src/java/arjdbc/db2/DB2Module.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import static arjdbc.util.QuotingUtils.BYTES_0;
2727
import static arjdbc.util.QuotingUtils.BYTES_1;
2828
import static arjdbc.util.QuotingUtils.quoteSingleQuotesWithFallback;
29+
import static org.jruby.api.Create.newString;
2930

3031
import org.jruby.Ruby;
3132
import org.jruby.RubyModule;
@@ -42,9 +43,8 @@
4243
public class DB2Module {
4344

4445
public static RubyModule load(final RubyModule arJdbc) {
45-
RubyModule db2 = arJdbc.defineModuleUnder("DB2");
46-
db2.defineAnnotatedMethods( DB2Module.class );
47-
return db2;
46+
var context = arJdbc.getRuntime().getCurrentContext();
47+
return arJdbc.defineModuleUnder(context, "DB2").defineMethods(context, DB2Module.class);
4848
}
4949

5050
public static RubyModule load(final Ruby runtime) {
@@ -63,7 +63,7 @@ public static IRubyObject quote_string(
6363
public static IRubyObject quoted_true(
6464
final ThreadContext context,
6565
final IRubyObject self) {
66-
return RubyString.newString(context.runtime, BYTES_1);
66+
return newString(context, BYTES_1);
6767
}
6868

6969
@JRubyMethod(name = "quoted_false", required = 0, frame = false)

src/java/arjdbc/db2/DB2RubyJdbcConnection.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,16 @@ public DB2RubyJdbcConnection(Ruby runtime, RubyClass metaClass) {
5454
super(runtime, metaClass);
5555
}
5656

57-
public static RubyClass createDB2JdbcConnectionClass(Ruby runtime, RubyClass jdbcConnection) {
58-
RubyClass clazz = getConnectionAdapters(runtime).
59-
defineClassUnder("DB2JdbcConnection", jdbcConnection, ALLOCATOR);
60-
clazz.defineAnnotatedMethods(DB2RubyJdbcConnection.class);
61-
62-
return clazz;
57+
public static RubyClass createDB2JdbcConnectionClass(ThreadContext context, RubyClass jdbcConnection) {
58+
return getConnectionAdapters(context).
59+
defineClassUnder(context, "DB2JdbcConnection", jdbcConnection, ALLOCATOR).
60+
defineMethods(context, DB2RubyJdbcConnection.class);
6361
}
6462

6563
public static RubyClass load(final Ruby runtime) {
66-
RubyClass jdbcConnection = getJdbcConnection(runtime);
67-
return createDB2JdbcConnectionClass(runtime, jdbcConnection);
64+
var context = runtime.getCurrentContext();
65+
RubyClass jdbcConnection = getJdbcConnection(context);
66+
return createDB2JdbcConnectionClass(context, jdbcConnection);
6867
}
6968

7069
protected static ObjectAllocator ALLOCATOR = new ObjectAllocator() {
@@ -112,7 +111,7 @@ public IRubyObject call(final Connection connection) throws SQLException {
112111
try {
113112
statement = connection.prepareStatement("VALUES IDENTITY_VAL_LOCAL()");
114113
genKeys = statement.executeQuery();
115-
return doMapGeneratedKeys(context.runtime, genKeys, true);
114+
return doMapGeneratedKeys(context, genKeys, true);
116115
}
117116
catch (final SQLException e) {
118117
debugMessage(context.runtime, "failed to get generated keys: ", e);

src/java/arjdbc/h2/H2Module.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
public class H2Module {
3838

3939
public static RubyModule load(final RubyModule arJdbc) {
40-
RubyModule h2 = arJdbc.defineModuleUnder("H2");
40+
var context = arJdbc.getRuntime().getCurrentContext();
41+
RubyModule h2 = arJdbc.defineModuleUnder(context, "H2");
4142
// NOTE: currently no Java implemented Ruby methods
4243
// h2.defineAnnotatedMethods( H2Module.class );
4344
return h2;

src/java/arjdbc/h2/H2RubyJdbcConnection.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@
2828
import java.sql.Connection;
2929
import java.sql.SQLException;
3030

31+
import arjdbc.db2.DB2RubyJdbcConnection;
3132
import org.jruby.Ruby;
3233
import org.jruby.RubyClass;
3334
import org.jruby.runtime.ObjectAllocator;
35+
import org.jruby.runtime.ThreadContext;
3436
import org.jruby.runtime.builtin.IRubyObject;
3537

3638
/**
@@ -45,16 +47,15 @@ public H2RubyJdbcConnection(Ruby runtime, RubyClass metaClass) {
4547
super(runtime, metaClass);
4648
}
4749

48-
public static RubyClass createH2JdbcConnectionClass(Ruby runtime, RubyClass jdbcConnection) {
49-
RubyClass clazz = getConnectionAdapters(runtime).
50-
defineClassUnder("H2JdbcConnection", jdbcConnection, ALLOCATOR);
51-
clazz.defineAnnotatedMethods(H2RubyJdbcConnection.class);
52-
return clazz;
50+
public static RubyClass createH2JdbcConnectionClass(ThreadContext context, RubyClass jdbcConnection) {
51+
return getConnectionAdapters(context).
52+
defineClassUnder(context, "H2JdbcConnection", jdbcConnection, ALLOCATOR).
53+
defineMethods(context, H2RubyJdbcConnection.class);
5354
}
5455

5556
public static RubyClass load(final Ruby runtime) {
56-
RubyClass jdbcConnection = getJdbcConnection(runtime);
57-
return createH2JdbcConnectionClass(runtime, jdbcConnection);
57+
var context = runtime.getCurrentContext();
58+
return createH2JdbcConnectionClass(context, getJdbcConnection(context));
5859
}
5960

6061
protected static ObjectAllocator ALLOCATOR = new ObjectAllocator() {

src/java/arjdbc/hsqldb/HSQLDBModule.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,19 @@
2828
import static arjdbc.util.QuotingUtils.BYTES_0;
2929
import static arjdbc.util.QuotingUtils.BYTES_1;
3030
import static arjdbc.util.QuotingUtils.quoteSingleQuotesWithFallback;
31+
import static org.jruby.api.Create.newString;
3132

3233
import org.jruby.Ruby;
3334
import org.jruby.RubyModule;
34-
import org.jruby.RubyString;
3535
import org.jruby.anno.JRubyMethod;
3636
import org.jruby.runtime.ThreadContext;
3737
import org.jruby.runtime.builtin.IRubyObject;
3838

3939
public class HSQLDBModule {
4040

4141
public static RubyModule load(final RubyModule arJdbc) {
42-
RubyModule hsqldb = arJdbc.defineModuleUnder("HSQLDB");
43-
hsqldb.defineAnnotatedMethods( HSQLDBModule.class );
44-
return hsqldb;
42+
var context = arJdbc.getRuntime().getCurrentContext();
43+
return arJdbc.defineModuleUnder(context, "HSQLDB").defineMethods(context, HSQLDBModule.class);
4544
}
4645

4746
public static RubyModule load(final Ruby runtime) {
@@ -60,14 +59,14 @@ public static IRubyObject quote_string(
6059
public static IRubyObject quoted_true(
6160
final ThreadContext context,
6261
final IRubyObject self) {
63-
return RubyString.newString(context.getRuntime(), BYTES_1);
62+
return newString(context, BYTES_1);
6463
}
6564

6665
@JRubyMethod(name = "quoted_false", required = 0, frame = false)
6766
public static IRubyObject quoted_false(
6867
final ThreadContext context,
6968
final IRubyObject self) {
70-
return RubyString.newString(context.getRuntime(), BYTES_0);
69+
return newString(context, BYTES_0);
7170
}
7271

7372
}

src/java/arjdbc/jdbc/DataSourceConnectionFactory.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
import static arjdbc.jdbc.RubyJdbcConnection.getConnectionNotEstablished;
3838
import static arjdbc.jdbc.RubyJdbcConnection.wrapException;
39+
import static org.jruby.api.Access.runtimeErrorClass;
3940

4041
/**
4142
*
@@ -103,7 +104,7 @@ static DataSource lookupDataSource(final ThreadContext context, final String nam
103104
if ( ! ( bound instanceof DataSource ) ) {
104105
if ( bound == null ) throw new NameNotFoundException(); // unlikely to happen
105106
final String msg = "bound object at '" + name + "' is not a " + DataSource.class.getName() + " but a " + bound.getClass().getName() + "\n" + bound;
106-
throw wrapException(context, getConnectionNotEstablished(context.runtime), new ClassCastException(msg), msg);
107+
throw wrapException(context, getConnectionNotEstablished(context), new ClassCastException(msg), msg);
107108
}
108109
return (DataSource) bound;
109110
}
@@ -124,10 +125,10 @@ else if ( name.indexOf("env", 0) == -1 ) {
124125
else {
125126
message = "unable to lookup data source - name: '" + name + "' not found";
126127
}
127-
throw wrapException(context, getConnectionNotEstablished(context.runtime), e, message);
128+
throw wrapException(context, getConnectionNotEstablished(context), e, message);
128129
}
129130
catch (NamingException e) {
130-
throw wrapException(context, context.runtime.getRuntimeError(), e);
131+
throw wrapException(context, runtimeErrorClass(context), e);
131132
}
132133
}
133134

src/java/arjdbc/jdbc/DriverWrapper.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
package arjdbc.jdbc;
2525

26+
import java.lang.reflect.InvocationTargetException;
2627
import java.sql.Connection;
2728
import java.sql.Driver;
2829
import java.sql.SQLException;
@@ -45,7 +46,7 @@ public class DriverWrapper {
4546
private final Properties properties;
4647

4748
DriverWrapper(final Ruby runtime, final String name, final Properties properties)
48-
throws ClassCastException, InstantiationException, IllegalAccessException {
49+
throws ClassCastException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
4950
this.driver = allocateDriver( loadDriver(runtime, name) );
5051
this.properties = properties == null ? new Properties() : properties;
5152
}
@@ -59,8 +60,8 @@ public Driver getDriverInstance() {
5960
}
6061

6162
private Driver allocateDriver(final Class<? extends Driver> driverClass)
62-
throws InstantiationException, IllegalAccessException {
63-
return driverClass.newInstance();
63+
throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
64+
return driverClass.getDeclaredConstructor().newInstance();
6465
}
6566

6667
protected static Class<? extends Driver> loadDriver(final Ruby runtime, final String name)

src/java/arjdbc/jdbc/JdbcResult.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
import org.jruby.runtime.ThreadContext;
1515
import org.jruby.runtime.builtin.IRubyObject;
1616

17+
import static org.jruby.api.Create.newArray;
18+
import static org.jruby.api.Create.newArrayNoCopy;
19+
import static org.jruby.api.Create.newHash;
20+
1721
/**
1822
* This is a base Result class to be returned as the "raw" result.
1923
* It should be overridden for specific adapters to manage type maps
@@ -31,7 +35,7 @@ public class JdbcResult extends RubyObject {
3135
protected JdbcResult(ThreadContext context, RubyClass clazz, RubyJdbcConnection connection, ResultSet resultSet) throws SQLException {
3236
super(context.runtime, clazz);
3337

34-
values = context.runtime.newArray();
38+
values = newArray(context);
3539
this.connection = connection;
3640

3741
final ResultSetMetaData resultMetaData = resultSet.getMetaData();
@@ -86,7 +90,7 @@ protected void populateTuples(final ThreadContext context) {
8690

8791
for (int i = 0; i < tuples.length; i++) {
8892
RubyArray currentRow = (RubyArray) values.eltInternal(i);
89-
RubyHash hash = RubyHash.newHash(context.runtime);
93+
RubyHash hash = newHash(context);
9094
for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
9195
hash.fastASet(columnNames[columnIndex], currentRow.eltInternal(columnIndex));
9296
}
@@ -101,17 +105,16 @@ protected void populateTuples(final ThreadContext context) {
101105
* @throws SQLException throws!
102106
*/
103107
private void processResultSet(final ThreadContext context, final ResultSet resultSet) throws SQLException {
104-
Ruby runtime = context.runtime;
105108
int columnCount = columnNames.length;
106109

107110
while (resultSet.next()) {
108111
final IRubyObject[] row = new IRubyObject[columnCount];
109112

110113
for (int i = 0; i < columnCount; i++) {
111-
row[i] = connection.jdbcToRuby(context, runtime, i + 1, columnTypes[i], resultSet); // Result Set is 1 based
114+
row[i] = connection.jdbcToRuby(context, context.runtime, i + 1, columnTypes[i], resultSet); // Result Set is 1 based
112115
}
113116

114-
values.append(RubyArray.newArrayNoCopy(context.runtime, row));
117+
values.append(context, newArrayNoCopy(context, row));
115118
}
116119
}
117120

@@ -122,9 +125,9 @@ private void processResultSet(final ThreadContext context, final ResultSet resul
122125
* @throws SQLException can be caused by postgres generating its type map
123126
*/
124127
public IRubyObject toARResult(final ThreadContext context) throws SQLException {
125-
final RubyClass Result = RubyJdbcConnection.getResult(context.runtime);
128+
final RubyClass Result = RubyJdbcConnection.getResult(context);
126129
// FIXME: Is this broken? no copy of an array AR::Result can modify? or should it be frozen?
127-
final RubyArray rubyColumnNames = RubyArray.newArrayNoCopy(context.runtime, getColumnNames());
130+
final RubyArray rubyColumnNames = newArrayNoCopy(context, getColumnNames());
128131
return Result.newInstance(context, rubyColumnNames, values, columnTypeMap(context), Block.NULL_BLOCK);
129132
}
130133
}

0 commit comments

Comments
 (0)