Skip to content

Commit

Permalink
Potential fix for library inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
iexavl committed Mar 11, 2024
1 parent 9940c31 commit 896f288
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 31 deletions.
89 changes: 64 additions & 25 deletions src/main/java/org/bytedeco/javacpp/ClassProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,19 +193,51 @@ public void load(Class cls, boolean inherit) {
}
platforms = classProperties.value();
}

boolean checkOnlyLibrary=false;
if (classPlatform != null) {
if (platforms == null) {
platforms = new Platform[] { classPlatform };
} else {
platforms = Arrays.copyOf(platforms, platforms.length + 1);
platforms[platforms.length - 1] = classPlatform;
}
}else if(platforms==null|| platforms.length==0){
Class<?> parentWithPlatforms = Loader.getEnclosingClassWithPlatforms(cls);
if(parentWithPlatforms!=null){
boolean putStandAlonePlatform = parentWithPlatforms.isAnnotationPresent(Platform.class) &&
!parentWithPlatforms.getAnnotation(Platform.class).library().isEmpty();
org.bytedeco.javacpp.annotation.Properties parentProperties=parentWithPlatforms.getAnnotation(org.bytedeco.javacpp.annotation.Properties.class);
int[] withLibraryIndices=null;
int count=0;
Platform[] vals=null;
if(parentProperties!=null && (vals=parentProperties.value())!=null){
withLibraryIndices = new int[vals.length];
for(int i=0;i<vals.length;i++){
if(vals[i].library()!= null && !vals[i].library().isEmpty()){
withLibraryIndices[count++]=i;
}
}
}
platforms= new Platform[count+(putStandAlonePlatform?1:0)];
if(putStandAlonePlatform){
platforms[platforms.length-1]=parentWithPlatforms.getAnnotation(Platform.class);
}
if(count>0){
for(int i=0;i<count;i++){
platforms[i]=vals[withLibraryIndices[i]];
}
}
}
checkOnlyLibrary=true;

}
boolean hasPlatformProperties = platforms != null && platforms.length > (classProperties != null && classPlatform != null ? 1 : 0);

boolean hasPlatformProperties = platforms != null && !checkOnlyLibrary && platforms.length > (classProperties != null && classPlatform != null ? 1 : 0);

String[] pragma = {}, define = {}, exclude = {}, include = {}, cinclude = {}, includepath = {}, includeresource = {}, compiler = {},
linkpath = {}, linkresource = {}, link = {}, frameworkpath = {}, framework = {}, preloadpath = {}, preloadresource = {}, preload = {},
resourcepath = {}, resource = {}, extension = {}, executablepath = {}, executable = {};
linkpath = {}, linkresource = {}, link = {}, frameworkpath = {}, framework = {}, preloadpath = {}, preloadresource = {}, preload = {},
resourcepath = {}, resource = {}, extension = {}, executablepath = {}, executable = {};
String library = "jni" + c.getSimpleName();
if (hasPlatformProperties) {
if (ourTarget != null && ourTarget.length() > 0) {
Expand Down Expand Up @@ -241,28 +273,35 @@ public void load(Class cls, boolean inherit) {
if (!match) {
continue;
}
if (p.pragma() .length > 0) { pragma = p.pragma(); }
if (p.define() .length > 0) { define = p.define(); }
if (p.exclude() .length > 0) { exclude = p.exclude(); }
if (p.include() .length > 0) { include = p.include(); }
if (p.cinclude() .length > 0) { cinclude = p.cinclude(); }
if (p.includepath().length > 0) { includepath = p.includepath(); }
if (p.includeresource().length > 0) { includeresource = p.includeresource(); }
if (p.compiler() .length > 0) { compiler = p.compiler(); }
if (p.linkpath() .length > 0) { linkpath = p.linkpath(); }
if (p.linkresource() .length > 0) { linkresource = p.linkresource(); }
if (p.link() .length > 0) { link = p.link(); }
if (p.frameworkpath().length > 0) { frameworkpath = p.frameworkpath(); }
if (p.framework() .length > 0) { framework = p.framework(); }
if (p.preloadresource().length > 0) { preloadresource = p.preloadresource(); }
if (p.preloadpath().length > 0) { preloadpath = p.preloadpath(); }
if (p.preload() .length > 0) { preload = p.preload(); }
if (p.resourcepath().length > 0) { resourcepath = p.resourcepath(); }
if (p.resource() .length > 0) { resource = p.resource(); }
if (p.extension() .length > 0) { extension = p.extension(); }
if (p.executablepath().length > 0) { executablepath = p.executablepath(); }
if (p.executable() .length > 0) { executable = p.executable(); }
if (p.library().length() > 0) { library = p.library(); }

if (p.library().length() > 0) { library = p.library(); }
if(checkOnlyLibrary){
continue;
}
if (p.pragma() .length > 0) { pragma = p.pragma(); }
if (p.define() .length > 0) { define = p.define(); }
if (p.exclude() .length > 0) { exclude = p.exclude(); }
if (p.include() .length > 0) { include = p.include(); }
if (p.cinclude() .length > 0) { cinclude = p.cinclude(); }
if (p.includepath().length > 0) { includepath = p.includepath(); }
if (p.includeresource().length > 0) { includeresource = p.includeresource(); }
if (p.compiler() .length > 0) { compiler = p.compiler(); }
if (p.linkpath() .length > 0) { linkpath = p.linkpath(); }
if (p.linkresource() .length > 0) { linkresource = p.linkresource(); }
if (p.link() .length > 0) { link = p.link(); }
if (p.frameworkpath().length > 0) { frameworkpath = p.frameworkpath(); }
if (p.framework() .length > 0) { framework = p.framework(); }
if (p.preloadresource().length > 0) { preloadresource = p.preloadresource(); }
if (p.preloadpath().length > 0) { preloadpath = p.preloadpath(); }
if (p.preload() .length > 0) { preload = p.preload(); }
if (p.resourcepath().length > 0) { resourcepath = p.resourcepath(); }
if (p.resource() .length > 0) { resource = p.resource(); }
if (p.extension() .length > 0) { extension = p.extension(); }
if (p.executablepath().length > 0) { executablepath = p.executablepath(); }
if (p.executable() .length > 0) { executable = p.executable(); }



}
}
for (int i = 0; i < includeresource.length; i++) {
Expand Down
62 changes: 57 additions & 5 deletions src/main/java/org/bytedeco/javacpp/Loader.java
Original file line number Diff line number Diff line change
Expand Up @@ -341,18 +341,70 @@ public static Class getEnclosingClass(Class cls) {
}
if (c.isAnnotationPresent(Platform.class)) {
Platform p = c.getAnnotation(Platform.class);
if (p.pragma().length > 0 || p.define().length > 0 || p.exclude().length > 0 || p.include().length > 0 || p.cinclude().length > 0
|| p.includepath().length > 0 || p.includeresource().length > 0 || p.compiler().length > 0
|| p.linkpath().length > 0 || p.linkresource().length > 0 || p.link().length > 0 || p.frameworkpath().length > 0
|| p.framework().length > 0 || p.preloadresource().length > 0 || p.preloadpath().length > 0 || p.preload().length > 0
|| p.resourcepath().length > 0 || p.resource().length > 0 || p.library().length() > 0) {
if(!isPlatformEmpty(p)){
break;
}
}
c = c.getEnclosingClass();
}
return c;
}
/**
* If annotated with Platform, or Properties with platforms, returns the argument as "enclosing Class".
* If no Platform, or properties with platforms, are found on the Class, makes a search for the first Class
* with platforms that we can use, and returns it as the enclosing Class found.
*
* @param cls the Class to start the search from
* @return the enclosing Class
* @see org.bytedeco.javacpp.annotation.Platform
* @see org.bytedeco.javacpp.annotation.Properties
*/
public static Class getEnclosingClassWithPlatforms(Class cls){
Class<?> c = cls;

if(c.isAnnotationPresent(Platform.class) && !isPlatformEmpty(c.getAnnotation(Platform.class))){
return c;
}

org.bytedeco.javacpp.annotation.Properties classProperties = c.getAnnotation(org.bytedeco.javacpp.annotation.Properties.class);
if(classProperties!=null){

Platform[] propertyPlatforms=classProperties.value();
if(propertyPlatforms!=null){
for (Platform platform : propertyPlatforms) {
if (!isPlatformEmpty(platform)) {
return c;
}
}
}
if(classProperties.inherit()!=null){
for (Class parent:classProperties.inherit()) {
Class ret = getEnclosingClassWithPlatforms(parent);
if(ret!=null){
return ret;
}
}
}

}

c=c.getEnclosingClass();
if(c==null){
return null;
}
return getEnclosingClassWithPlatforms(c);



}
public static boolean isPlatformEmpty(Platform p){
return p.pragma().length <= 0 && p.define().length <= 0 && p.exclude().length <= 0 && p.include().length <= 0 && p.cinclude().length <= 0
&& p.includepath().length <= 0 && p.includeresource().length <= 0 && p.compiler().length <= 0
&& p.linkpath().length <= 0 && p.linkresource().length <= 0 && p.link().length <= 0 && p.frameworkpath().length <= 0
&& p.framework().length <= 0 && p.preloadresource().length <= 0 && p.preloadpath().length <= 0 && p.preload().length <= 0
&& p.resourcepath().length <= 0 && p.resource().length <= 0 && p.library().length() <= 0;

}


/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/bytedeco/javacpp/tools/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ boolean classes(boolean handleExceptions, boolean defineAdapters, boolean conver
out.println("static JavaCPP_noinline jclass JavaCPP_getClass(JNIEnv* env, int i);");
out.println("static inline void JavaCPP_loadGlobal(JNIEnv* env, jclass cls, const char* filename) {");
out.println("#ifdef _WIN32");
out.println(" HMODULE handle = LoadLibrary(filename);");
out.println(" HMODULE handle = LoadLibraryA(filename);");
out.println(" if (handle == NULL) {");
out.println(" char temp[256];");
out.println(" sprintf(temp, \"LoadLibrary() failed with 0x%lx\", GetLastError());");
Expand Down

0 comments on commit 896f288

Please sign in to comment.