This is the mail archive of the kawa@sourceware.org mailing list for the Kawa project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Ateempt to rewrite a example from bytecode package on scheme


Example is attached

I get error at code
(*:getMethod helloWorldClass "add" argTypes)
Argument #3 ([Ljava.lang.Class;@54c4ad) to 'java.lang.Class.getMethod(java.lang.String,java.lang.Class[])' has wrong type ([Ljava.lang.Class;) (expected: java.lang.Class)
at gnu.mapping.MethodProc.matchFailAsException(MethodProc.java:97)
at gnu.mapping.Procedure.checkN(Procedure.java:359)
at gnu.kawa.reflect.Invoke.apply(Invoke.java:96)
at gnu.mapping.CallContext.runUntilDone(CallContext.java:251)
at gnu.expr.ModuleExp.evalModule(ModuleExp.java:300)
at kawa.Shell.run(Shell.java:273)
at kawa.Shell.run(Shell.java:184)
at kawa.Shell.run(Shell.java:165)
at kawa.repl.apply0(repl.java:28)
at gnu.mapping.RunnableClosure.run(RunnableClosure.java:105)
at gnu.mapping.Future.run(Future.java:40)


What is wrong? How to resolve problem?

--
WBR, Yaroslav Kavenchuk.
;; import section

(define-alias ClassType gnu.bytecode.ClassType)
(define-alias Access gnu.bytecode.Access)
(define-alias Method gnu.bytecode.Method)
(define-alias CodeAttr gnu.bytecode.CodeAttr)
(define-alias Type gnu.bytecode.Type)
(define-alias Variable gnu.bytecode.Variable)
(define-alias ClassTypeWriter gnu.bytecode.ClassTypeWriter)
(define-alias ArrayClassLoader gnu.bytecode.ArrayClassLoader)

(define-alias System java.lang.System)
(define-alias Class java.lang.Class)
(define-alias Class[] java.lang.Class[])
(define-alias Integer java.lang.Integer)

;; code

;; "public class HelloWorld extends java.lang.Object".
(define c :: ClassType (ClassType "HelloWorld"))
(*:setSuper c "java.lang.Object")
(*:setModifiers c Access:PUBLIC)


;; "public static int add(int, int)".
(define m :: Method
  (*:addMethod c "add" "(II)I" (logior Access:PUBLIC Access:STATIC)))
(define code :: CodeAttr (m:startCode))
(code:pushScope)
(code:emitLoad (code:getArg 0))
(code:emitLoad (code:getArg 1))
(code:emitAdd Type:intType)
(define resultVar :: Variable (code:addLocal Type:intType "result"))
(code:emitDup)
(code:emitStore resultVar)
(code:emitReturn)
(code:popScope)


;; Get a byte[] representing the class file.
;; We could write this to disk if we wanted.
(define classFile :: byte[] (*:writeToArray c))

;; Disassemble this class.
;; The output is similar to javap(1).
(ClassTypeWriter:print c System:out 0)

;; Load the generated class into this JVM.
;; gnu.bytecode provides ArrayClassLoader, or you can use your own.
(define cl :: ArrayClassLoader (ArrayClassLoader))
(cl:addClass "HelloWorld" classFile)
		
;; Actual invocation is just the usual reflection code.
(define helloWorldClass :: Class (cl:loadClass "HelloWorld" #t))
(define argTypes :: Class[]
  (Class[] java.lang.Integer:TYPE java.lang.Integer:TYPE))
(define result :: int
 (as Integer
     ((*:getMethod helloWorldClass "add" argTypes):invoke null 1 2)))
(System:err:println result)

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]