71 72 class GlobalVarInfo extends MemberInfo<Layout> { 73 74 AccessorKind accessorKind; 75 76 GlobalVarInfo(String symbolName, Layout layout, AccessorKind accessorKind) { 77 super(symbolName, layout); 78 this.accessorKind = accessorKind; 79 } 80 } 81 82 class FunctionInfo extends MemberInfo<Function> { 83 public FunctionInfo(String symbolName, Function descriptor) { 84 super(symbolName, descriptor); 85 } 86 } 87 88 @Override 89 protected void generateMembers(BinderClassWriter cw) { 90 Class<?> headerClass = interfaces[0]; 91 String declarations = headerClass.getAnnotation(NativeHeader.class).declarations(); 92 for (Map.Entry<String, Object> declEntry : DescriptorParser.parseHeaderDeclarations(declarations).entrySet()) { 93 if (declEntry.getValue() instanceof Layout) { 94 Layout l = (Layout)declEntry.getValue(); 95 for (Map.Entry<AccessorKind, String> accessorEntry : AccessorKind.from(l).entrySet()) { 96 nameToInfo.put(accessorEntry.getValue(), new GlobalVarInfo(declEntry.getKey(), l, accessorEntry.getKey())); 97 } 98 } else { 99 Function f = (Function)declEntry.getValue(); 100 nameToInfo.put(declEntry.getKey(), new FunctionInfo(declEntry.getKey(), f)); 101 } 102 } 103 super.generateMembers(cw); 104 } 105 106 @Override 107 protected void generateConstructor(BinderClassWriter cw) { 108 MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); 109 mv.visitCode(); 110 mv.visitVarInsn(ALOAD, 0); 111 mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); 112 mv.visitInsn(RETURN); 113 mv.visitMaxs(1,1); 114 mv.visitEnd(); 115 } 116 117 @Override 118 protected void generateMethodImplementation(BinderClassWriter cw, Method method) { 119 MemberInfo<?> memberInfo = nameToInfo.get(method.getName()); 120 if (memberInfo instanceof FunctionInfo) { 121 generateFunctionMethod(cw, method, (FunctionInfo)memberInfo); 122 } else if (memberInfo instanceof GlobalVarInfo) { 123 generateGlobalVariableMethod(cw, method, (GlobalVarInfo)memberInfo); | 71 72 class GlobalVarInfo extends MemberInfo<Layout> { 73 74 AccessorKind accessorKind; 75 76 GlobalVarInfo(String symbolName, Layout layout, AccessorKind accessorKind) { 77 super(symbolName, layout); 78 this.accessorKind = accessorKind; 79 } 80 } 81 82 class FunctionInfo extends MemberInfo<Function> { 83 public FunctionInfo(String symbolName, Function descriptor) { 84 super(symbolName, descriptor); 85 } 86 } 87 88 @Override 89 protected void generateMembers(BinderClassWriter cw) { 90 Class<?> headerClass = interfaces[0]; 91 handleDeclarations(headerClass); 92 super.generateMembers(cw); 93 } 94 95 private void handleDeclarations(Class<?> cls) { 96 for (Class<?> si : cls.getInterfaces()) { 97 handleDeclarations(si); 98 } 99 if (!cls.isAnnotationPresent(NativeHeader.class)) { 100 // nothing to do! 101 return; 102 } 103 String declarations = cls.getAnnotation(NativeHeader.class).declarations(); 104 for (Map.Entry<String, Object> declEntry : DescriptorParser.parseHeaderDeclarations(declarations).entrySet()) { 105 if (declEntry.getValue() instanceof Layout) { 106 Layout l = (Layout)declEntry.getValue(); 107 for (Map.Entry<AccessorKind, String> accessorEntry : AccessorKind.from(l).entrySet()) { 108 nameToInfo.put(accessorEntry.getValue(), new GlobalVarInfo(declEntry.getKey(), l, accessorEntry.getKey())); 109 } 110 } else { 111 Function f = (Function)declEntry.getValue(); 112 nameToInfo.put(declEntry.getKey(), new FunctionInfo(declEntry.getKey(), f)); 113 } 114 } 115 } 116 117 @Override 118 protected void generateConstructor(BinderClassWriter cw) { 119 MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); 120 mv.visitCode(); 121 mv.visitVarInsn(ALOAD, 0); 122 mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); 123 mv.visitInsn(RETURN); 124 mv.visitMaxs(1,1); 125 mv.visitEnd(); 126 } 127 128 @Override 129 protected void generateMethodImplementation(BinderClassWriter cw, Method method) { 130 MemberInfo<?> memberInfo = nameToInfo.get(method.getName()); 131 if (memberInfo instanceof FunctionInfo) { 132 generateFunctionMethod(cw, method, (FunctionInfo)memberInfo); 133 } else if (memberInfo instanceof GlobalVarInfo) { 134 generateGlobalVariableMethod(cw, method, (GlobalVarInfo)memberInfo); |