< prev index next >

src/java.base/share/classes/jdk/internal/nicl/HeaderImplGenerator.java

Print this page




  59                 case ptr: return MethodType.methodType(Pointer.class);
  60             }
  61 
  62             throw new IllegalArgumentException("Unhandled type: " + this);
  63         }
  64     }
  65 
  66     @Override
  67     protected void generateDefaultConstructor(BinderClassWriter cw) {
  68         MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
  69         mv.visitCode();
  70         mv.visitVarInsn(ALOAD, 0);
  71         mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
  72         mv.visitInsn(RETURN);
  73         mv.visitMaxs(1,1);
  74         mv.visitEnd();
  75     }
  76 
  77     @Override
  78     protected void generateMethodImplementation(BinderClassWriter cw, Method method) {
  79         if (method.isAnnotationPresent(NativeType.class) && !Util.isFunction(method)) {
  80             generateGlobalVariableMethods(cw, method, getSymbolName(method, getGetterBaseName(method)));
  81         } else if (method.isAnnotationPresent(C.class) && method.isAnnotationPresent(CallingConvention.class)) {
  82             MethodType methodType = Util.methodTypeFor(method);
  83             Function function = Util.functionof(method);
  84             NativeInvoker invoker;
  85             try {
  86                 invoker = new NativeInvoker(function, methodType, method.isVarArgs(), lookup, getSymbolName(method), method.toString(), method.getGenericReturnType());
  87             } catch (NoSuchMethodException | IllegalAccessException e) {
  88                 throw new IllegalStateException(e);
  89             }
  90             addMethodFromHandle(cw, method.getName(), methodType, method.isVarArgs(), invoker.getBoundMethodHandle());
  91         } else {



  92             super.generateMethodImplementation(cw, method);
  93         }
  94     }
  95 
  96     private void generateGlobalVariableMethods(BinderClassWriter cw, Method method, String symbolName) {
  97         Class<?> c = method.getReturnType();
  98         java.lang.reflect.Type type = method.getGenericReturnType();
  99         Layout l = new DescriptorParser(method.getAnnotation(NativeType.class).layout()).parseLayout().findFirst().get();
 100         LayoutType<?> lt = Util.makeType(type, l);
 101 
 102         int dollarIndex = method.getName().indexOf("$");
 103         String methodBaseName = method.getName().substring(0, dollarIndex);
 104         Pointer<?> p;
 105 
 106         try {
 107             p = lookup.lookup(symbolName).getAddress().cast(lt);
 108         } catch (NoSuchMethodException e) {
 109             throw new IllegalStateException(e);
 110         }
 111 




  59                 case ptr: return MethodType.methodType(Pointer.class);
  60             }
  61 
  62             throw new IllegalArgumentException("Unhandled type: " + this);
  63         }
  64     }
  65 
  66     @Override
  67     protected void generateDefaultConstructor(BinderClassWriter cw) {
  68         MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
  69         mv.visitCode();
  70         mv.visitVarInsn(ALOAD, 0);
  71         mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
  72         mv.visitInsn(RETURN);
  73         mv.visitMaxs(1,1);
  74         mv.visitEnd();
  75     }
  76 
  77     @Override
  78     protected void generateMethodImplementation(BinderClassWriter cw, Method method) {
  79         if (method.isAnnotationPresent(NativeType.class)) {
  80             if (Util.isFunction(method)) {

  81                 MethodType methodType = Util.methodTypeFor(method);
  82                 Function function = Util.functionof(method);
  83                 NativeInvoker invoker;
  84                 try {
  85                     invoker = new NativeInvoker(function, methodType, method.isVarArgs(), lookup, getSymbolName(method), method.toString(), method.getGenericReturnType());
  86                 } catch (NoSuchMethodException | IllegalAccessException e) {
  87                     throw new IllegalStateException(e);
  88                 }
  89                 addMethodFromHandle(cw, method.getName(), methodType, method.isVarArgs(), invoker.getBoundMethodHandle());
  90             } else {
  91                 generateGlobalVariableMethods(cw, method, getSymbolName(method, getGetterBaseName(method)));
  92             }
  93         } else {
  94             super.generateMethodImplementation(cw, method);
  95         }
  96     }
  97 
  98     private void generateGlobalVariableMethods(BinderClassWriter cw, Method method, String symbolName) {
  99         Class<?> c = method.getReturnType();
 100         java.lang.reflect.Type type = method.getGenericReturnType();
 101         Layout l = new DescriptorParser(method.getAnnotation(NativeType.class).layout()).parseLayout().findFirst().get();
 102         LayoutType<?> lt = Util.makeType(type, l);
 103 
 104         int dollarIndex = method.getName().indexOf("$");
 105         String methodBaseName = method.getName().substring(0, dollarIndex);
 106         Pointer<?> p;
 107 
 108         try {
 109             p = lookup.lookup(symbolName).getAddress().cast(lt);
 110         } catch (NoSuchMethodException e) {
 111             throw new IllegalStateException(e);
 112         }
 113 


< prev index next >