io.qt.core.QPluginLoader Maven / Gradle / Ivy
Show all versions of qtjambi Show documentation
package io.qt.core;
import io.qt.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.function.*;
import java.util.logging.*;
import io.qt.core.internal.*;
/**
* Loads a plugin at run-time
* Java wrapper for Qt class QPluginLoader
*/
public class QPluginLoader extends io.qt.core.QObject
{
/**
* This variable stores the meta-object for the class.
*/
public static final io.qt.core.@NonNull QMetaObject staticMetaObject = io.qt.core.QMetaObject.forType(QPluginLoader.class);
/**
* See QPluginLoader:: QPluginLoader(QObject*)
*/
public QPluginLoader(io.qt.core.@Nullable QObject parent){
super((QPrivateConstructor)null);
initialize_native(this, parent);
}
private native static void initialize_native(QPluginLoader instance, io.qt.core.QObject parent);
/**
* See QPluginLoader:: QPluginLoader(QString, QObject*)
*/
public QPluginLoader(java.lang.@NonNull String fileName, io.qt.core.@Nullable QObject parent){
super((QPrivateConstructor)null);
initialize_native(this, fileName, parent);
}
private native static void initialize_native(QPluginLoader instance, java.lang.String fileName, io.qt.core.QObject parent);
/**
* See QPluginLoader:: errorString()const
*/
@QtUninvokable
public final java.lang.@NonNull String errorString(){
return errorString_native_constfct(QtJambi_LibraryUtilities.internal.nativeId(this));
}
@QtUninvokable
private native java.lang.String errorString_native_constfct(long __this__nativeId);
/**
* See QPluginLoader:: fileName()const
*/
@QtPropertyReader(name="fileName")
@QtUninvokable
public final java.lang.@NonNull String fileName(){
return fileName_native_constfct(QtJambi_LibraryUtilities.internal.nativeId(this));
}
@QtUninvokable
private native java.lang.String fileName_native_constfct(long __this__nativeId);
/**
*
*/
@QtUninvokable
public final io.qt.core.@Nullable QObject instance(){
return instance_native(QtJambi_LibraryUtilities.internal.nativeId(this));
}
@QtUninvokable
private native io.qt.core.QObject instance_native(long __this__nativeId);
/**
* See QPluginLoader:: isLoaded()const
*/
@QtUninvokable
public final boolean isLoaded(){
return isLoaded_native_constfct(QtJambi_LibraryUtilities.internal.nativeId(this));
}
@QtUninvokable
private native boolean isLoaded_native_constfct(long __this__nativeId);
/**
*
*/
@QtUninvokable
public final boolean load(){
return load_native(QtJambi_LibraryUtilities.internal.nativeId(this));
}
@QtUninvokable
private native boolean load_native(long __this__nativeId);
/**
* See QPluginLoader:: loadHints()const
*/
@QtPropertyReader(name="loadHints")
@QtUninvokable
public final io.qt.core.QLibrary.@NonNull LoadHints loadHints(){
return new io.qt.core.QLibrary.LoadHints(loadHints_native_constfct(QtJambi_LibraryUtilities.internal.nativeId(this)));
}
@QtUninvokable
private native int loadHints_native_constfct(long __this__nativeId);
/**
* See QPluginLoader:: metaData()const
*/
@QtUninvokable
public final io.qt.core.@NonNull QJsonObject metaData(){
return metaData_native_constfct(QtJambi_LibraryUtilities.internal.nativeId(this));
}
@QtUninvokable
private native io.qt.core.QJsonObject metaData_native_constfct(long __this__nativeId);
/**
* See QPluginLoader:: setFileName(QString)
*/
@QtPropertyWriter(name="fileName")
@QtUninvokable
public final void setFileName(java.lang.@NonNull String fileName){
setFileName_native_cref_QString(QtJambi_LibraryUtilities.internal.nativeId(this), fileName);
}
@QtUninvokable
private native void setFileName_native_cref_QString(long __this__nativeId, java.lang.String fileName);
/**
* See QPluginLoader:: setLoadHints(QLibrary::LoadHints)
*/
@QtPropertyWriter(name="loadHints")
@QtUninvokable
public final void setLoadHints(io.qt.core.QLibrary.@NonNull LoadHints loadHints){
setLoadHints_native_QLibrary_LoadHints(QtJambi_LibraryUtilities.internal.nativeId(this), loadHints.value());
}
@QtUninvokable
private native void setLoadHints_native_QLibrary_LoadHints(long __this__nativeId, int loadHints);
/**
*
*/
@QtUninvokable
public final boolean unload(){
return unload_native(QtJambi_LibraryUtilities.internal.nativeId(this));
}
@QtUninvokable
private native boolean unload_native(long __this__nativeId);
/**
* See QPluginLoader:: staticInstances()
*/
public native static io.qt.core.@NonNull QList staticInstances();
/**
* See QPluginLoader:: staticPlugins()
*/
public native static io.qt.core.@NonNull QList staticPlugins();
/**
* Constructor for internal use only.
* @param p expected to be null
.
* @hidden
*/
@NativeAccess
protected QPluginLoader(QPrivateConstructor p) { super(p); }
/**
* Constructor for internal use only.
* It is not allowed to call the declarative constructor from inside Java.
* @hidden
*/
@NativeAccess
protected QPluginLoader(QDeclarativeConstructor constructor) {
super((QPrivateConstructor)null);
initialize_native(this, constructor);
}
@QtUninvokable
private static native void initialize_native(QPluginLoader instance, QDeclarativeConstructor constructor);
/**
* @deprecated Use {@link #qRegisterStaticPluginFunction(QObject)} instead.
*/
@Deprecated
public static void registerStaticPluginFunction(QObject instance){
qRegisterStaticPluginFunction(instance);
}
/**
* @deprecated Use {@link #qRegisterStaticPluginFunction(QObject, QJsonObject)} instead.
*/
@Deprecated
public static void registerStaticPluginFunction(QObject instance, QJsonObject metaData){
qRegisterStaticPluginFunction(instance, metaData);
}
/**
* @deprecated Use {@link #qRegisterStaticPluginFunction(QObject, java.util.Map)} instead.
*/
@Deprecated
public static void registerStaticPluginFunction(QObject instance, java.util.Map metaData){
qRegisterStaticPluginFunction(instance, metaData);
}
/**
* @deprecated Use {@link #qRegisterStaticPluginFunction(Class)} instead.
*/
@Deprecated
public static void registerStaticPluginFunction(Class extends QObject> pluginClass){
qRegisterStaticPluginFunction(pluginClass, (QJsonObject)null);
}
/**
* @deprecated Use {@link #qRegisterStaticPluginFunction(Class, QJsonObject)} instead.
*/
@Deprecated
public static void registerStaticPluginFunction(Class extends QObject> pluginClass, QJsonObject metaData){
qRegisterStaticPluginFunction(pluginClass, metaData);
}
/**
* @deprecated Use {@link #qRegisterStaticPluginFunction(Class, java.util.Map)} instead.
*/
@Deprecated
public static void registerStaticPluginFunction(Class extends QObject> pluginClass, java.util.Map metaData){
qRegisterStaticPluginFunction(pluginClass, metaData);
}
/**
* @deprecated Use {@link #qRegisterPluginInterface(Class)} instead.
*/
@Deprecated
public static void registerPluginInterface(Class extends QtObjectInterface> factoryClass){
qRegisterPluginInterface(factoryClass);
}
public static native void qRegisterPluginInterface(Class extends QtObjectInterface> iface);
/**
*
*/
@QtUninvokable
public final T instance(Class type){
return QMetaObject.cast(type, instance());
}
private static class PluginClassLoader extends URLClassLoader {
public PluginClassLoader() {
super(new URL[0]);
}
protected void addURLs(Collection urls) {
for (URL url : urls) {
super.addURL(url);
QResource.addClassPath(url);
}
}
}
private static final PluginClassLoader pluginClassLoader = new PluginClassLoader();
/**
* See qRegisterStaticPluginFunction(QStaticPlugin)
*/
public static void qRegisterStaticPluginFunction(QObject instance){
qRegisterStaticPluginFunction(instance, (QJsonObject)null);
}
/**
* See qRegisterStaticPluginFunction(QStaticPlugin)
*/
public static void qRegisterStaticPluginFunction(QObject instance, QJsonObject metaData) {
if (metaData == null) {
metaData = loadMetaDataFromClass(QtJambi_LibraryUtilities.internal.getClass(instance));
}
qRegisterStaticPluginFunctionInstance(instance, QtJambi_LibraryUtilities.internal.nativeId(metaData));
}
/**
* See qRegisterStaticPluginFunction(QStaticPlugin)
*/
public static void qRegisterStaticPluginFunction(QObject instance, java.util.Map metaData){
qRegisterStaticPluginFunction(instance, QJsonObject.fromVariantHash(metaData));
}
/**
* See qRegisterStaticPluginFunction(QStaticPlugin)
*/
public static void qRegisterStaticPluginFunction(Class extends QObject> pluginClass){
qRegisterStaticPluginFunction(pluginClass, (QJsonObject)null);
}
/**
* See qRegisterStaticPluginFunction(QStaticPlugin)
*/
public static void qRegisterStaticPluginFunction(Class extends QObject> pluginClass, java.util.Map metaData){
qRegisterStaticPluginFunction(pluginClass, QJsonObject.fromVariantHash(metaData));
}
/**
* See qRegisterStaticPluginFunction(QStaticPlugin)
*/
public static void qRegisterStaticPluginFunction(Class extends QObject> pluginClass, QJsonObject metaData) {
try {
if (metaData == null) {
metaData = loadMetaDataFromClass(pluginClass);
}
qRegisterStaticPluginFunctionClass(pluginClass, QtJambi_LibraryUtilities.internal.nativeId(metaData));
} catch (RuntimeException | Error e) {
throw e;
} catch (Exception e) {
throw new RuntimeException("Unable to register plugin " + pluginClass.getName(), e);
}
}
private static native void qRegisterStaticPluginFunctionClass(Class extends QObject> pluginClass, long metaData);
private static native void qRegisterStaticPluginFunctionInstance(QObject instance, long metaData);
private static native void qRegisterStaticPluginFunction(Supplier> classSupplier, String className, String iid, long metaData, long pluginInfo);
private static QJsonObject loadMetaDataFromClass(Class extends QObject> pluginClass) {
QtPluginMetaData pluginMetaData = pluginClass.getAnnotation(QtPluginMetaData.class);
if (pluginMetaData != null) {
QJsonDocument.FromJsonResult result;
if (!pluginMetaData.file().isEmpty()) {
QFile pluginMetaDataFile = new QFile(":" + pluginClass.getPackage().getName().replace('.', '/') + "/" + pluginMetaData.file());
if (pluginMetaDataFile.exists() && pluginMetaDataFile.open(QIODevice.OpenModeFlag.ReadOnly)) {
result = QJsonDocument.fromJson(pluginMetaDataFile.readAll());
if (result.document != null)
return result.document.object();
} else {
try {
InputStream stream = pluginClass.getClassLoader().getResourceAsStream(pluginClass.getPackage().getName().replace('.', '/') + "/" + pluginMetaData.file());
if (stream != null) {
ByteArrayOutputStream bas = new ByteArrayOutputStream();
try {
byte[] buffer = new byte[1024];
int length = stream.read(buffer);
while (length > 0) {
bas.write(buffer, 0, length);
length = stream.read(buffer);
}
} finally {
stream.close();
}
result = QJsonDocument.fromJson(new QByteArray(bas.toByteArray()));
if (result.error == null || result.error.error() == QJsonParseError.ParseError.NoError)
return result.document.object();
else
throw new IOException(result.error.errorString());
}
} catch (IOException t) {
Logger.getLogger("internal").throwing(QPluginLoader.class.getName(), "loadMetaDataFromClass", t);
}
}
} else if (!pluginMetaData.json().isEmpty()) {
result = QJsonDocument.fromJson(new QByteArray(pluginMetaData.json()));
if (result.document != null)
return result.document.object();
}
}
return new QJsonObject();
}
@NativeAccess
private static QObject loadPluginInstance(String libPath, String className, String pluginName) throws Throwable {
Class> foundClass = null;
try {
foundClass = Class.forName(className);
} catch (ClassNotFoundException e) {
List classNameSplit = new ArrayList<>();
classNameSplit.addAll(Arrays.asList(className.split("\\.")));
StringBuilder name = new StringBuilder();
while(!classNameSplit.isEmpty()) {
if(name.length()!=0)
name.append('.');
name.append(classNameSplit.remove(0));
try {
Class.forName(name.toString());
while(!classNameSplit.isEmpty()) {
name.append('$');
name.append(classNameSplit.remove(0));
}
foundClass = Class.forName(name.toString());
} catch (ClassNotFoundException e2) {}
}
}
if(foundClass==null) {
QFileInfo libFile = new QFileInfo(QDir.fromNativeSeparators(libPath));
if (libFile.exists()) {
List urls = new ArrayList();
if(QOperatingSystemVersion.current().isAnyOfType(QOperatingSystemVersion.OSType.Android)) {
String fileName = libFile.fileName();
String suffix;
switch (QSysInfo.currentCpuArchitecture()) {
case "armeabi-v7a":
case "arm":
suffix = pluginName+"_armeabi-v7a.so";
break;
case "aarch64":
case "arm64-v8a":
case "arm64":
suffix = pluginName+"_arm64-v8a.so";
break;
case "x86":
case "i386":
suffix = pluginName+"_x86.so";
break;
default:
suffix = pluginName+"_x86_64.so";
break;
}
if(fileName.startsWith("lib") && fileName.endsWith(suffix)) {
fileName = fileName.substring(3, fileName.length()-suffix.length());
fileName = fileName.replace('_', '/');
URL url = QPluginLoader.class.getClassLoader().getResource(fileName + pluginName + ".jar");
if(url!=null)
urls.add(url);
else {
QDir subdir = new QDir(":/" + fileName + pluginName);
if (subdir.exists()) {
for (String jar : subdir.entryList(Arrays.asList("*.jar"), QDir.Filter.Files)) {
url = QPluginLoader.class.getClassLoader().getResource(fileName + pluginName + jar);
if(url!=null)
urls.add(url);
}
}
}
}
}else {
QDir dir = libFile.dir();
if (dir.exists(pluginName + ".jar")) {
QFileInfo jarFile = new QFileInfo(dir.filePath(pluginName + ".jar"));
if (jarFile.isFile()) {
urls.add(new File(QDir.toNativeSeparators(jarFile.absoluteFilePath())).toURI().toURL());
}
} else {
QDir subdir = new QDir(dir.filePath(pluginName));
if (subdir.exists()) {
for (String jar : subdir.entryList(Arrays.asList("*.jar"), QDir.Filter.Files)) {
urls.add(new File(QDir.toNativeSeparators(subdir.absoluteFilePath(jar))).toURI().toURL());
}
if (!urls.isEmpty()) {
System.setProperty("java.library.path", System.getProperty("java.library.path", "")
+ File.pathSeparatorChar + QDir.toNativeSeparators(subdir.absolutePath()));
}
}
}
}
if (!urls.isEmpty()) {
pluginClassLoader.addURLs(urls);
try{
foundClass = pluginClassLoader.loadClass(className);
} catch (Throwable e) {
e.printStackTrace();
}
}
}
}
if(foundClass!=null) {
try {
QObject result = (QObject)QtJambi_LibraryUtilities.internal.invokeContructor(foundClass.getDeclaredConstructor());
QtJambi_LibraryUtilities.internal.setCppOwnership(result);
return result;
} catch (Throwable e) {
e.printStackTrace();
throw e;
}
}
return null;
}
/**
* Overloaded constructor for {@link #QPluginLoader(io.qt.core.QObject)}
* with parent = null
.
*/
public QPluginLoader() {
this((io.qt.core.QObject)null);
}
/**
* Overloaded constructor for {@link #QPluginLoader(java.lang.String, io.qt.core.QObject)}
* with parent = null
.
*/
public QPluginLoader(java.lang.@NonNull String fileName) {
this(fileName, (io.qt.core.QObject)null);
}
/**
* @hidden
* Kotlin property getter. In Java use {@link #fileName()} instead.
*/
@QtPropertyReader(enabled=false)
@QtUninvokable
public final java.lang.@NonNull String getFileName() {
return fileName();
}
/**
* @hidden
* Kotlin property getter. In Java use {@link #loadHints()} instead.
*/
@QtPropertyReader(enabled=false)
@QtUninvokable
public final io.qt.core.QLibrary.@NonNull LoadHints getLoadHints() {
return loadHints();
}
/**
* Overloaded function for {@link #setLoadHints(io.qt.core.QLibrary.LoadHints)}.
*/
@QtUninvokable
public final void setLoadHints(io.qt.core.QLibrary.@NonNull LoadHint @NonNull... loadHints) {
setLoadHints(new io.qt.core.QLibrary.LoadHints(loadHints));
}
}