提交 edd6df0d authored 作者: Thomas Mueller's avatar Thomas Mueller

Reflection utilities.

上级 a2ea25e0
...@@ -261,9 +261,7 @@ ShutdownHandler { ...@@ -261,9 +261,7 @@ ShutdownHandler {
private boolean createTrayIcon() { private boolean createTrayIcon() {
try { try {
// SystemTray.isSupported(); // SystemTray.isSupported();
Boolean supported = (Boolean) Class.forName("java.awt.SystemTray"). boolean supported = (Boolean) Utils.callStaticMethod("java.awt.SystemTray.isSupported");
getMethod("isSupported").
invoke(null);
if (!supported) { if (!supported) {
return false; return false;
} }
...@@ -285,14 +283,11 @@ ShutdownHandler { ...@@ -285,14 +283,11 @@ ShutdownHandler {
menuConsole.add(itemExit); menuConsole.add(itemExit);
// SystemTray tray = SystemTray.getSystemTray(); // SystemTray tray = SystemTray.getSystemTray();
Object tray = Class.forName("java.awt.SystemTray"). Object tray = Utils.callStaticMethod("java.awt.SystemTray.getSystemTray");
getMethod("getSystemTray").
invoke(null);
// Dimension d = tray.getTrayIconSize(); // Dimension d = tray.getTrayIconSize();
Dimension d = (Dimension) Class.forName("java.awt.SystemTray"). Dimension d = (Dimension) Utils.callMethod(tray, "getTrayIconSize");
getMethod("getTrayIconSize").
invoke(tray);
String iconFile; String iconFile;
if (d.width >= 24 && d.height >= 24) { if (d.width >= 24 && d.height >= 24) {
iconFile = "/org/h2/res/h2-24.png"; iconFile = "/org/h2/res/h2-24.png";
...@@ -302,20 +297,15 @@ ShutdownHandler { ...@@ -302,20 +297,15 @@ ShutdownHandler {
iconFile = "/org/h2/res/h2.png"; iconFile = "/org/h2/res/h2.png";
} }
Image icon = loadImage(iconFile); Image icon = loadImage(iconFile);
// TrayIcon icon = new TrayIcon(image, "H2 Database Engine", menuConsole);
Object ti = Class.forName("java.awt.TrayIcon"). // TrayIcon ti = new TrayIcon(image, "H2 Database Engine", menuConsole);
getConstructor(Image.class, String.class, PopupMenu.class). Object ti = Utils.newInstance("java.awt.TrayIcon", icon, "H2 Database Engine", menuConsole);
newInstance(icon, "H2 Database Engine", menuConsole);
// ti.addMouseListener(this);
// trayIcon.addMouseListener(this); Utils.callMethod(ti, "addMouseListener", this);
ti.getClass().
getMethod("addMouseListener", MouseListener.class). // tray.add(ti);
invoke(ti, this); Utils.callMethod(tray, "add", ti);
// tray.add(icon);
tray.getClass().
getMethod("add", Class.forName("java.awt.TrayIcon")).
invoke(tray, ti);
this.trayIcon = true; this.trayIcon = true;
......
...@@ -12,7 +12,6 @@ import java.io.InputStream; ...@@ -12,7 +12,6 @@ import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.PrintStream; import java.io.PrintStream;
import java.io.StringReader; import java.io.StringReader;
import java.lang.reflect.Method;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
...@@ -24,13 +23,13 @@ import java.util.ArrayList; ...@@ -24,13 +23,13 @@ import java.util.ArrayList;
import java.util.Properties; import java.util.Properties;
import org.h2.engine.Constants; import org.h2.engine.Constants;
import org.h2.server.web.ConnectionInfo; import org.h2.server.web.ConnectionInfo;
import org.h2.util.ScriptReader;
import org.h2.util.Utils;
import org.h2.util.IOUtils; import org.h2.util.IOUtils;
import org.h2.util.JdbcUtils; import org.h2.util.JdbcUtils;
import org.h2.util.New; import org.h2.util.New;
import org.h2.util.ScriptReader;
import org.h2.util.SortedProperties; import org.h2.util.SortedProperties;
import org.h2.util.Tool; import org.h2.util.Tool;
import org.h2.util.Utils;
/** /**
* Interactive command line tool to access a database using JDBC. * Interactive command line tool to access a database using JDBC.
...@@ -414,11 +413,9 @@ public class Shell extends Tool implements Runnable { ...@@ -414,11 +413,9 @@ public class Shell extends Tool implements Runnable {
private String readPassword() throws IOException { private String readPassword() throws IOException {
try { try {
Method getConsole = System.class.getMethod("console"); Object console = Utils.callStaticMethod("java.lang.System.console");
Object console = getConsole.invoke(null);
Method readPassword = console.getClass().getMethod("readPassword");
print("Password "); print("Password ");
char[] password = (char[]) readPassword.invoke(console); char[] password = (char[]) Utils.callMethod(console, "readPassword");
return password == null ? null : new String(password); return password == null ? null : new String(password);
} catch (Exception e) { } catch (Exception e) {
// ignore, use the default solution // ignore, use the default solution
......
...@@ -64,9 +64,9 @@ public class DbUpgradeNonPageStoreToCurrent { ...@@ -64,9 +64,9 @@ public class DbUpgradeNonPageStoreToCurrent {
oldUrl = oldUrl.replaceAll(";IFEXISTS=FALSE", ""); oldUrl = oldUrl.replaceAll(";IFEXISTS=FALSE", "");
oldUrl += ";IGNORE_UNKNOWN_SETTINGS=TRUE"; oldUrl += ";IGNORE_UNKNOWN_SETTINGS=TRUE";
Object ci = Utils.newInstance("org.h2.upgrade.v1_1.engine.ConnectionInfo", oldUrl, info); Object ci = Utils.newInstance("org.h2.upgrade.v1_1.engine.ConnectionInfo", oldUrl, info);
boolean isRemote = (Boolean) Utils.callMethod("isRemote", ci); boolean isRemote = (Boolean) Utils.callMethod(ci, "isRemote");
boolean isPersistent = (Boolean) Utils.callMethod("isPersistent", ci); boolean isPersistent = (Boolean) Utils.callMethod(ci, "isPersistent");
String dbName = (String) Utils.callMethod("getName", ci); String dbName = (String) Utils.callMethod(ci, "getName");
// remove stackable file systems // remove stackable file systems
int colon = dbName.indexOf(':'); int colon = dbName.indexOf(':');
while (colon != -1) { while (colon != -1) {
......
...@@ -768,6 +768,9 @@ public class DataType { ...@@ -768,6 +768,9 @@ public class DataType {
if (x == null) { if (x == null) {
return Value.NULL; return Value.NULL;
} }
if (x.isPrimitive()) {
x = Utils.getNonPrimitiveClass(x);
}
if (ResultSet.class.isAssignableFrom(x)) { if (ResultSet.class.isAssignableFrom(x)) {
return Value.RESULT_SET; return Value.RESULT_SET;
} else if (Value.ValueBlob.class.isAssignableFrom(x)) { } else if (Value.ValueBlob.class.isAssignableFrom(x)) {
...@@ -778,21 +781,21 @@ public class DataType { ...@@ -778,21 +781,21 @@ public class DataType {
return Value.STRING; return Value.STRING;
} else if (BigDecimal.class.isAssignableFrom(x)) { } else if (BigDecimal.class.isAssignableFrom(x)) {
return Value.DECIMAL; return Value.DECIMAL;
} else if (Boolean.class.isAssignableFrom(x) || boolean.class.isAssignableFrom(x)) { } else if (Boolean.class == x) {
return Value.BOOLEAN; return Value.BOOLEAN;
} else if (Byte.class.isAssignableFrom(x) || byte.class.isAssignableFrom(x)) { } else if (Byte.class == x) {
return Value.BYTE; return Value.BYTE;
} else if (Short.class.isAssignableFrom(x) || short.class.isAssignableFrom(x)) { } else if (Short.class == x) {
return Value.SHORT; return Value.SHORT;
} else if (Integer.class.isAssignableFrom(x) || int.class.isAssignableFrom(x)) { } else if (Integer.class == x) {
return Value.INT; return Value.INT;
} else if (Character.class.isAssignableFrom(x) || char.class.isAssignableFrom(x)) { } else if (Character.class == x) {
throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, "char (not supported)"); throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, "char (not supported)");
} else if (Long.class.isAssignableFrom(x) || long.class.isAssignableFrom(x)) { } else if (Long.class == x) {
return Value.LONG; return Value.LONG;
} else if (Float.class.isAssignableFrom(x) || float.class.isAssignableFrom(x)) { } else if (Float.class == x) {
return Value.FLOAT; return Value.FLOAT;
} else if (Double.class.isAssignableFrom(x) || double.class.isAssignableFrom(x)) { } else if (Double.class == x) {
return Value.DOUBLE; return Value.DOUBLE;
} else if (byte[].class.isAssignableFrom(x)) { } else if (byte[].class.isAssignableFrom(x)) {
return Value.BYTES; return Value.BYTES;
......
...@@ -35,12 +35,12 @@ public class TestUtils extends TestBase { ...@@ -35,12 +35,12 @@ public class TestUtils extends TestBase {
long currentTimeMillis2 = (Long) Utils.callStaticMethod("java.lang.System.currentTimeMillis"); long currentTimeMillis2 = (Long) Utils.callStaticMethod("java.lang.System.currentTimeMillis");
assertTrue(currentTimeMillis1 <= currentTimeMillis2); assertTrue(currentTimeMillis1 <= currentTimeMillis2);
// New Instance with Integer parameter (Autoboxing) // New Instance with Integer parameter (Autoboxing)
StringBuilder instance = (StringBuilder) Utils.newInstance("java.lang.StringBuilder", new Integer(10)); Object instance = Utils.newInstance("java.lang.StringBuilder", 10);
// New Instance with int parameter // New Instance with int parameter
instance = (StringBuilder) Utils.newInstance("java.lang.StringBuilder", 10); instance = Utils.newInstance("java.lang.StringBuilder", 10);
// Instance methods // Instance methods
Utils.callMethod("append", instance, "abc"); Utils.callMethod(instance, "append", "abc");
int length = (Integer) Utils.callMethod("length", instance); int length = (Integer) Utils.callMethod(instance, "length");
assertEquals(3, length); assertEquals(3, length);
// Static fields // Static fields
String pathSeparator = (String) Utils.getStaticField("java.io.File.pathSeparator"); String pathSeparator = (String) Utils.getStaticField("java.io.File.pathSeparator");
...@@ -51,7 +51,9 @@ public class TestUtils extends TestBase { ...@@ -51,7 +51,9 @@ public class TestUtils extends TestBase {
// Class present? // Class present?
assertFalse(Utils.isClassPresent("abc")); assertFalse(Utils.isClassPresent("abc"));
assertTrue(Utils.isClassPresent(getClass().getName())); assertTrue(Utils.isClassPresent(getClass().getName()));
Utils.callStaticMethod("java.lang.String.valueOf", "a");
Utils.callStaticMethod("java.awt.AWTKeyStroke.getAWTKeyStroke",
'x', java.awt.event.InputEvent.SHIFT_DOWN_MASK);
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论