Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static annotation for factory methods #101

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/main/java/org/bridj/MethodCallInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import org.bridj.ann.DisableDirect;
import org.bridj.ann.Ptr;
import org.bridj.ann.SetsLastError;
import org.bridj.ann.Static;
import org.bridj.ann.Virtual;
import org.bridj.dyncall.DyncallLibrary.DCstruct;
import org.bridj.util.Utils;
Expand Down Expand Up @@ -143,7 +144,8 @@ public MethodCallInfo(Method method, Method definition) {
symbolName = methodName;

int modifiers = method.getModifiers();
isStatic = Modifier.isStatic(modifiers);
isStatic = method.getAnnotation(Static.class) != null
|| Modifier.isStatic(modifiers);
isVarArgs = method.isVarArgs();
boolean isNative = Modifier.isNative(modifiers);
boolean isVirtual = isAnnotationPresent(Virtual.class, definition);
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/bridj/StructFieldDeclaration.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.bridj.ann.Array;
import org.bridj.ann.Bits;
import org.bridj.ann.Field;
import org.bridj.ann.Static;
import org.bridj.ann.Union;

class StructFieldDeclaration {
Expand All @@ -69,7 +70,8 @@ protected static boolean acceptFieldGetter(Member member, boolean getter) {

int modifiers = member.getModifiers();

return !Modifier.isStatic(modifiers);
return !(((AnnotatedElement) member).getAnnotation(Static.class) != null
|| Modifier.isStatic(modifiers));
}

/**
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/bridj/ann/Static.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.bridj.ann;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Applies to native methods to indicate that they should be treated as
* static.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface Static {
}
7 changes: 5 additions & 2 deletions src/main/java/org/bridj/objc/ObjectiveCRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@
import org.bridj.Pointer;
import org.bridj.Pointer.Releaser;
import org.bridj.ann.Library;
import org.bridj.ann.Name;
import org.bridj.ann.Ptr;
import org.bridj.ann.Static;
import org.bridj.demangling.Demangler;
import org.bridj.util.Utils;

Expand Down Expand Up @@ -283,7 +285,7 @@ protected void registerNativeMethod(

try {
MethodCallInfo mci = methodCallInfoBuilder.apply(method);
boolean isStatic = Modifier.isStatic(method.getModifiers());
boolean isStatic = method.getAnnotation(Static.class) != null || Modifier.isStatic(method.getModifiers());

if (isStatic) {
Pointer<ObjCClass> pObjcClass = getObjCClass((Class) type).as(ObjCClass.class);
Expand Down Expand Up @@ -416,6 +418,7 @@ private Pointer<? extends ObjCObject> getObjCClass(Class<? extends NativeObject>
return getObjCClass("NSObject", false);
}

return getObjCClass(cls.getSimpleName(), false);
Name name = cls.getAnnotation(Name.class);
return getObjCClass(name == null ? cls.getSimpleName() : name.value(), false);
}
}