for 的构造函数SQLiteDatabase
是私有的,因此您将无法扩展它并将“包装器”添加到类本身。但是,您可以像这样编写一个“助手”包装器:
public class WALWrapper {
private boolean mAvailable;
private Method mIsWriteAheadLoggingEnabled;
private Method mEnableWriteAheadLogging;
private Method mDisableWriteAheadLogging;
private final SQLiteDatabase mDb;
public WALWrapper(SQLiteDatabase db) {
mDb = db;
mAvailable = false;
try {
mIsWriteAheadLoggingEnabled =
SQLiteDatabase.class.getMethod("isWriteAheadLoggingEnabled");
mEnableWriteAheadLogging =
SQLiteDatabase.class.getMethod("enableWriteAheadLogging");
mDisableWriteAheadLogging =
SQLiteDatabase.class.getMethod("disableWriteAheadLogging");
mAvailable = true;
} catch (NoSuchMethodException e) {
}
}
/**
* Returns <code>true</code> if the {@link #isWriteAheadLoggingEnabled()},
* {@link #enableWriteAheadLogging()} and {@link #disableWriteAheadLogging()}
* are available.
* @return <code>true</code> if the WALWrapper is functional, <code>false</code>
* otherwise.
*/
public boolean isWALAvailable() {
return mAvailable;
}
public boolean isWriteAheadLoggingEnabled() {
boolean result = false;
if (mIsWriteAheadLoggingEnabled != null) {
try {
result = (Boolean) mIsWriteAheadLoggingEnabled.invoke(mDb);
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
}
return result;
}
public boolean enableWriteAheadLogging() {
boolean result = false;
if (mEnableWriteAheadLogging != null) {
try {
result = (Boolean) mEnableWriteAheadLogging.invoke(mDb);
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
}
return result;
}
public void disableWriteAheadLogging() {
if (mDisableWriteAheadLogging != null) {
try {
mDisableWriteAheadLogging.invoke(mDb);
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
}
}
}