Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #33503 > unrolled thread

methodhandles lookup

Started byPhilipp Kraus <philipp.kraus.flashpixx@gmail.com>
First post2016-01-22 00:19 -0800
Last post2016-01-22 16:49 +0100
Articles 12 — 4 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  methodhandles lookup Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2016-01-22 00:19 -0800
    Re: methodhandles lookup Jeff Higgins <jeff@invalid.invalid> - 2016-01-22 08:57 +0000
    Re: methodhandles lookup Jeff Higgins <jeff@invalid.invalid> - 2016-01-22 11:01 +0000
      Re: methodhandles lookup Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2016-01-22 03:13 -0800
        Re: methodhandles lookup Jeff Higgins <jeff@invalid.invalid> - 2016-01-22 11:43 +0000
          Re: methodhandles lookup Jeff Higgins <jeff@invalid.invalid> - 2016-01-22 12:03 +0000
            Re: methodhandles lookup Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2016-01-24 01:59 -0800
        Re: methodhandles lookup Jeff Higgins <jeff@invalid.invalid> - 2016-01-22 12:16 +0000
          Re: methodhandles lookup Jan Burse <janburse@fastmail.fm> - 2016-01-22 17:12 +0100
    Re: methodhandles lookup Martin Gregorie <martin@address-in-sig.invalid> - 2016-01-22 11:27 +0000
      Re: methodhandles lookup Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2016-01-24 01:56 -0800
    Re: methodhandles lookup Jan Burse <janburse@fastmail.fm> - 2016-01-22 16:49 +0100

#33503 — methodhandles lookup

FromPhilipp Kraus <philipp.kraus.flashpixx@gmail.com>
Date2016-01-22 00:19 -0800
Subjectmethodhandles lookup
Message-ID<1abadfac-e34b-4ff0-8ba5-613355fa928b@googlegroups.com>
Hello,

I'm trying to create a flexible (dynamic) binding for method calls and I have got a problem with overloaded method binding.
In detail: I have created an own programming language which should call a java method. In my own language I have got grammar constructs like "foo/bar( "myobject" );". Internal I have got a Map<String, Object> which maps the string parameter to an object which will be invoked. foo/bar points to the method, in this case foo is the classname and bar the method name. So in this example I need a binding to the method "public void bar();".
My language should also use calls like "foo/bar( "myobject", 1 );". In this case the Java method "void bar( int x );" should be called.

The basic invoking call works well, but I overloaded method creates a problem. If I have got the class

class foo
{
     public void bar()
     {
          System.out.println("test");
     }
}

I do the following:

final Multimap<String, MethodHandle> y = HashMultimap.create();
for ( final Method x : foo.class.getDeclaredMethods() )
    y.put( x.getSimpleName(), MethodHandles.lookup().unreflect(x) );

This multimap is put to the parser / lexer of my language to resolve method names but on calling / invoking I get the problem, that I need the correct methodhandle within the multimap depend on the signature. 

The problem is a class like
class foo
{
     public void bar()
     {
          System.out.println("test");
     }

     public void bar( int x )
     {
          System.out.println("test " + x);
     }
}

So my question is, how can I create a dynamic binding on method handles depend on the signature?
And can I cache the method handles / is it a good choice of caching all handles? In my case I know during runtime the concrete signature of the method, "foo/bar" is executed and on this time I know all arguments of the method, so I can create at this point the binding. How can I do this in a fast way?

Thanks

Phil

[toc] | [next] | [standalone]


#33504

FromJeff Higgins <jeff@invalid.invalid>
Date2016-01-22 08:57 +0000
Message-ID<n7sqtr$29m$1@dont-email.me>
In reply to#33503
On Fri, 22 Jan 2016 00:19:07 -0800, Philipp Kraus wrote:

> Hello,
> 
> I'm trying to create a flexible (dynamic) binding for method calls and I
> have got a problem with overloaded method binding.
> In detail: I have created an own programming language which should call
> a java method. In my own language I have got grammar constructs like
> "foo/bar( "myobject" );". Internal I have got a Map<String, Object>
> which maps the string parameter to an object which will be invoked.
> foo/bar points to the method, in this case foo is the classname and bar
> the method name. So in this example I need a binding to the method
> "public void bar();".
> My language should also use calls like "foo/bar( "myobject", 1 );". In
> this case the Java method "void bar( int x );" should be called.

Not to answer your question.

Xtext - Language Engineering Made Easy! - Eclipse
<https://eclipse.org/Xtext/#intro-quotes>

> 
> The basic invoking call works well, but I overloaded method creates a
> problem. If I have got the class
> 
> class foo {
>      public void bar()
>      {
>           System.out.println("test");
>      }
> }
> 
> I do the following:
> 
> final Multimap<String, MethodHandle> y = HashMultimap.create();
> for ( final Method x : foo.class.getDeclaredMethods() )
>     y.put( x.getSimpleName(), MethodHandles.lookup().unreflect(x) );
> 
> This multimap is put to the parser / lexer of my language to resolve
> method names but on calling / invoking I get the problem, that I need
> the correct methodhandle within the multimap depend on the signature.
> 
> The problem is a class like class foo {
>      public void bar()
>      {
>           System.out.println("test");
>      }
> 
>      public void bar( int x )
>      {
>           System.out.println("test " + x);
>      }
> }
> 
> So my question is, how can I create a dynamic binding on method handles
> depend on the signature?
> And can I cache the method handles / is it a good choice of caching all
> handles? In my case I know during runtime the concrete signature of the
> method, "foo/bar" is executed and on this time I know all arguments of
> the method, so I can create at this point the binding. How can I do this
> in a fast way?

You might look to the org.eclipse.gef4.common.adapt package for 
inspiration.
<http://wiki.eclipse.org/GEF/GEF4/Common#Adapt>
> 
> Thanks
> 
> Phil

[toc] | [prev] | [next] | [standalone]


#33505

FromJeff Higgins <jeff@invalid.invalid>
Date2016-01-22 11:01 +0000
Message-ID<n7t26f$29m$2@dont-email.me>
In reply to#33503
On Fri, 22 Jan 2016 00:19:07 -0800, Philipp Kraus wrote:

> Hello,
> 
> I'm trying to create a flexible (dynamic) binding for method calls and I
> have got a problem with overloaded method binding.
> In detail: I have created an own programming language which should call
> a java method. In my own language I have got grammar constructs like
> "foo/bar( "myobject" );". Internal I have got a Map<String, Object>
> which maps the string parameter to an object which will be invoked.
> foo/bar points to the method, in this case foo is the classname and bar
> the method name. So in this example I need a binding to the method
> "public void bar();".
> My language should also use calls like "foo/bar( "myobject", 1 );". In
> this case the Java method "void bar( int x );" should be called.
> 
> The basic invoking call works well, but I overloaded method creates a
> problem. If I have got the class
> 
> class foo {
>      public void bar()
>      {
>           System.out.println("test");
>      }
> }
> 
> I do the following:
> 
> final Multimap<String, MethodHandle> y = HashMultimap.create();
> for ( final Method x : foo.class.getDeclaredMethods() )
>     y.put( x.getSimpleName(), MethodHandles.lookup().unreflect(x) );
> 
> This multimap is put to the parser / lexer of my language to resolve
> method names but on calling / invoking I get the problem, that I need
> the correct methodhandle within the multimap depend on the signature.
> 
> The problem is a class like class foo {
>      public void bar()
>      {
>           System.out.println("test");
>      }
> 
>      public void bar( int x )
>      {
>           System.out.println("test " + x);
>      }
> }
> 
> So my question is, how can I create a dynamic binding on method handles
> depend on the signature?

For each MethodHandle examine its MethodType and proceed from there.

> And can I cache the method handles / is it a good choice of caching all
> handles? In my case I know during runtime the concrete signature of the
> method, "foo/bar" is executed and on this time I know all arguments of
> the method, so I can create at this point the binding. How can I do this
> in a fast way?
> 
> Thanks
> 
> Phil

[toc] | [prev] | [next] | [standalone]


#33506

FromPhilipp Kraus <philipp.kraus.flashpixx@gmail.com>
Date2016-01-22 03:13 -0800
Message-ID<0bf0e18b-b787-4ce2-b0cc-f1706d786127@googlegroups.com>
In reply to#33505
Okay thanks. 

In my system I have got a component which creates the MethodHandle objects from the Method object, so can I create based on the Method object the handle object on its signature?
Can you explain how? In my case I will cache all Method objects and build the handlers during runtime, is this fast enough?

Thanks

Phil


Am Freitag, 22. Januar 2016 12:04:37 UTC+1 schrieb Jeff Higgins:
> On Fri, 22 Jan 2016 00:19:07 -0800, Philipp Kraus wrote:
> 
> > Hello,
> > 
> > I'm trying to create a flexible (dynamic) binding for method calls and I
> > have got a problem with overloaded method binding.
> > In detail: I have created an own programming language which should call
> > a java method. In my own language I have got grammar constructs like
> > "foo/bar( "myobject" );". Internal I have got a Map<String, Object>
> > which maps the string parameter to an object which will be invoked.
> > foo/bar points to the method, in this case foo is the classname and bar
> > the method name. So in this example I need a binding to the method
> > "public void bar();".
> > My language should also use calls like "foo/bar( "myobject", 1 );". In
> > this case the Java method "void bar( int x );" should be called.
> > 
> > The basic invoking call works well, but I overloaded method creates a
> > problem. If I have got the class
> > 
> > class foo {
> >      public void bar()
> >      {
> >           System.out.println("test");
> >      }
> > }
> > 
> > I do the following:
> > 
> > final Multimap<String, MethodHandle> y = HashMultimap.create();
> > for ( final Method x : foo.class.getDeclaredMethods() )
> >     y.put( x.getSimpleName(), MethodHandles.lookup().unreflect(x) );
> > 
> > This multimap is put to the parser / lexer of my language to resolve
> > method names but on calling / invoking I get the problem, that I need
> > the correct methodhandle within the multimap depend on the signature.
> > 
> > The problem is a class like class foo {
> >      public void bar()
> >      {
> >           System.out.println("test");
> >      }
> > 
> >      public void bar( int x )
> >      {
> >           System.out.println("test " + x);
> >      }
> > }
> > 
> > So my question is, how can I create a dynamic binding on method handles
> > depend on the signature?
> 
> For each MethodHandle examine its MethodType and proceed from there.
> 
> > And can I cache the method handles / is it a good choice of caching all
> > handles? In my case I know during runtime the concrete signature of the
> > method, "foo/bar" is executed and on this time I know all arguments of
> > the method, so I can create at this point the binding. How can I do this
> > in a fast way?
> > 
> > Thanks
> > 
> > Phil

[toc] | [prev] | [next] | [standalone]


#33508

FromJeff Higgins <jeff@invalid.invalid>
Date2016-01-22 11:43 +0000
Message-ID<n7t4l7$29m$3@dont-email.me>
In reply to#33506
On Fri, 22 Jan 2016 03:13:31 -0800, Philipp Kraus wrote:

> Okay thanks.
> 
> In my system I have got a component which creates the MethodHandle
> objects from the Method object, so can I create based on the Method
> object the handle object on its signature?

If you can obtain the correct Class<?> from Method you might use one of 
the MethodType.methodType(Class<?>, ...) methods. Not sure how fuzzy this 
might get when considering such things as bridge or synthetic methods.

> Can you explain how? In my case I will cache all Method objects and
> build the handlers during runtime, is this fast enough?

Fast enough for what?
Not fair answering a question with a question?
Ok then, try it.

> 
> Thanks
> 
> Phil
> 
> 
> Am Freitag, 22. Januar 2016 12:04:37 UTC+1 schrieb Jeff Higgins:
>> On Fri, 22 Jan 2016 00:19:07 -0800, Philipp Kraus wrote:
>> 
>> > Hello,
>> > 
>> > I'm trying to create a flexible (dynamic) binding for method calls
>> > and I have got a problem with overloaded method binding.
>> > In detail: I have created an own programming language which should
>> > call a java method. In my own language I have got grammar constructs
>> > like "foo/bar( "myobject" );". Internal I have got a Map<String,
>> > Object> which maps the string parameter to an object which will be
>> > invoked. foo/bar points to the method, in this case foo is the
>> > classname and bar the method name. So in this example I need a
>> > binding to the method "public void bar();".
>> > My language should also use calls like "foo/bar( "myobject", 1 );".
>> > In this case the Java method "void bar( int x );" should be called.
>> > 
>> > The basic invoking call works well, but I overloaded method creates a
>> > problem. If I have got the class
>> > 
>> > class foo {
>> >      public void bar()
>> >      {
>> >           System.out.println("test");
>> >      }
>> > }
>> > 
>> > I do the following:
>> > 
>> > final Multimap<String, MethodHandle> y = HashMultimap.create();
>> > for ( final Method x : foo.class.getDeclaredMethods() )
>> >     y.put( x.getSimpleName(), MethodHandles.lookup().unreflect(x) );
>> > 
>> > This multimap is put to the parser / lexer of my language to resolve
>> > method names but on calling / invoking I get the problem, that I need
>> > the correct methodhandle within the multimap depend on the signature.
>> > 
>> > The problem is a class like class foo {
>> >      public void bar()
>> >      {
>> >           System.out.println("test");
>> >      }
>> > 
>> >      public void bar( int x )
>> >      {
>> >           System.out.println("test " + x);
>> >      }
>> > }
>> > 
>> > So my question is, how can I create a dynamic binding on method
>> > handles depend on the signature?
>> 
>> For each MethodHandle examine its MethodType and proceed from there.
>> 
>> > And can I cache the method handles / is it a good choice of caching
>> > all handles? In my case I know during runtime the concrete signature
>> > of the method, "foo/bar" is executed and on this time I know all
>> > arguments of the method, so I can create at this point the binding.
>> > How can I do this in a fast way?
>> > 
>> > Thanks
>> > 
>> > Phil

[toc] | [prev] | [next] | [standalone]


#33509

FromJeff Higgins <jeff@invalid.invalid>
Date2016-01-22 12:03 +0000
Message-ID<n7t5qd$29m$4@dont-email.me>
In reply to#33508
On Fri, 22 Jan 2016 11:43:35 +0000, Jeff Higgins wrote:

> On Fri, 22 Jan 2016 03:13:31 -0800, Philipp Kraus wrote:
> 
>> Okay thanks.
>> 
>> In my system I have got a component which creates the MethodHandle
>> objects from the Method object, so can I create based on the Method
>> object the handle object on its signature?
> 
> If you can obtain the correct Class<?> from Method you might use one of
> the MethodType.methodType(Class<?>, ...) methods. Not sure how fuzzy
> this might get when considering such things as bridge or synthetic
> methods.
> 
>> Can you explain how? 

I still think you might be able to leverage some ideas from
<http://wiki.eclipse.org/GEF/GEF4/Common#Adapt>.
Or more generically from the Adapter Pattern.

>> In my case I will cache all Method objects and
>> build the handlers during runtime, is this fast enough?
> 
> Fast enough for what?
> Not fair answering a question with a question?
> Ok then, try it.
> 
> 

[toc] | [prev] | [next] | [standalone]


#33531

FromPhilipp Kraus <philipp.kraus.flashpixx@gmail.com>
Date2016-01-24 01:59 -0800
Message-ID<6e5162a4-6185-45ca-995b-e7f2eda01177@googlegroups.com>
In reply to#33509
Am Freitag, 22. Januar 2016 13:06:22 UTC+1 schrieb Jeff Higgins:
> On Fri, 22 Jan 2016 11:43:35 +0000, Jeff Higgins wrote:
> 
> > On Fri, 22 Jan 2016 03:13:31 -0800, Philipp Kraus wrote:
> > 
> >> Okay thanks.
> >> 
> >> In my system I have got a component which creates the MethodHandle
> >> objects from the Method object, so can I create based on the Method
> >> object the handle object on its signature?
> > 
> > If you can obtain the correct Class<?> from Method you might use one of
> > the MethodType.methodType(Class<?>, ...) methods. Not sure how fuzzy
> > this might get when considering such things as bridge or synthetic
> > methods.
> > 
> >> Can you explain how? 
> 
> I still think you might be able to leverage some ideas from
> <http://wiki.eclipse.org/GEF/GEF4/Common#Adapt>.
> Or more generically from the Adapter Pattern.
> 
> >> In my case I will cache all Method objects and
> >> build the handlers during runtime, is this fast enough?
> > 
> > Fast enough for what?
> > Not fair answering a question with a question?
> > Ok then, try it.

Thanks Jeff, I think you're right with the adapt pattern. I need to redesign my code so that I can
add the signature to get the correct method from the class.

Thanks that helps

Phil

[toc] | [prev] | [next] | [standalone]


#33510

FromJeff Higgins <jeff@invalid.invalid>
Date2016-01-22 12:16 +0000
Message-ID<n7t6ii$29m$5@dont-email.me>
In reply to#33506
On Fri, 22 Jan 2016 03:13:31 -0800, Philipp Kraus wrote:

> Okay thanks.
> 
> In my system I have got a component which creates the MethodHandle
> objects from the Method object, so can I create based on the Method
> object the handle object on its signature?
> Can you explain how? In my case I will cache all Method objects and
> build the handlers during runtime, is this fast enough?
> 
An interesting article, I'll leave a link here as a memento for me.
<http://mydailyjava.blogspot.com/2015/03/dismantling-invokedynamic.html>

[toc] | [prev] | [next] | [standalone]


#33513

FromJan Burse <janburse@fastmail.fm>
Date2016-01-22 17:12 +0100
Message-ID<n7tkcm$hm8$1@news.albasani.net>
In reply to#33510
MethodHandle is a wrapper for Method (findStatic, etc..), Constructor 
(findConstructor, etc..) and Field (findSetter, findGetter etc..),
whos invokation translates to

the new byte code, which gives faster execution than using the
original Method, Constructor or Field API. But I don't have
enough experience to say more.


Jeff Higgins schrieb:
> On Fri, 22 Jan 2016 03:13:31 -0800, Philipp Kraus wrote:
>
>> Okay thanks.
>>
>> In my system I have got a component which creates the MethodHandle
>> objects from the Method object, so can I create based on the Method
>> object the handle object on its signature?
>> Can you explain how? In my case I will cache all Method objects and
>> build the handlers during runtime, is this fast enough?
>>
> An interesting article, I'll leave a link here as a memento for me.
> <http://mydailyjava.blogspot.com/2015/03/dismantling-invokedynamic.html>
>

[toc] | [prev] | [next] | [standalone]


#33507

FromMartin Gregorie <martin@address-in-sig.invalid>
Date2016-01-22 11:27 +0000
Message-ID<n7t3nc$qov$1@dont-email.me>
In reply to#33503
On Fri, 22 Jan 2016 00:19:07 -0800, Philipp Kraus wrote:

> Hello,
> 
> I'm trying to create a flexible (dynamic) binding for method calls and I
> have got a problem with overloaded method binding.
> In detail: I have created an own programming language which should call
> a java method. In my own language I have got grammar constructs like
> "foo/bar( "myobject" );". Internal I have got a Map<String, Object>
> which maps the string parameter to an object which will be invoked.
> foo/bar points to the method, in this case foo is the classname and bar
> the method name. So in this example I need a binding to the method
> "public void bar();".
> My language should also use calls like "foo/bar( "myobject", 1 );". In
> this case the Java method "void bar( int x );" should be called.
> 
> The basic invoking call works well, but I overloaded method creates a
> problem. If I have got the class
> 
> class foo {
>      public void bar()
>      {
>           System.out.println("test");
>      }
> }
> 
> I do the following:
> 
> final Multimap<String, MethodHandle> y = HashMultimap.create();
> for ( final Method x : foo.class.getDeclaredMethods() )
>     y.put( x.getSimpleName(), MethodHandles.lookup().unreflect(x) );
> 
> This multimap is put to the parser / lexer of my language to resolve
> method names but on calling / invoking I get the problem, that I need
> the correct methodhandle within the multimap depend on the signature.
> 
> The problem is a class like class foo {
>      public void bar()
>      {
>           System.out.println("test");
>      }
> 
>      public void bar( int x )
>      {
>           System.out.println("test " + x);
>      }
> }
> 
> So my question is, how can I create a dynamic binding on method handles
> depend on the signature?
> And can I cache the method handles / is it a good choice of caching all
> handles? In my case I know during runtime the concrete signature of the
> method, "foo/bar" is executed and on this time I know all arguments of
> the method, so I can create at this point the binding. How can I do this
> in a fast way?
>
Also not answering your specific question, but have you considered using 
a compiler generator such as Coco/R?

http://www.ssw.uni-linz.ac.at/Research/Projects/Coco/

Compiler generators read a definition of the input language and generate
the equivalent output which, in the case of the Java version of Coco/R, 
would be a Java program that can parse and process files written in the 
input language. 

What the generated program does with its input is entirely up to its 
author: in your case you'd probably want it to be a preprocessor that can 
read your language and output the equivalent logic as compilable Java 
source code, but it could equally interpret the input as a script and 
execute it as its being read, or even generate bytecode directly.

Personal use cases:

I've used lex and yacc, C equivalents of Coco/R, to parse a complex data 
definition language and use SQL statements to create database tables from 
it which formed an active data dictionary that managed the input of data 
into a data warehouse.

I used Coco/R to generate Java code to parsed and interpret C 
preprocessor directives as part of a C documentation generator that does 
a similar job to Javadocs.


-- 
martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

[toc] | [prev] | [next] | [standalone]


#33530

FromPhilipp Kraus <philipp.kraus.flashpixx@gmail.com>
Date2016-01-24 01:56 -0800
Message-ID<430a09fb-3781-46f1-9ac8-6ec588dc0631@googlegroups.com>
In reply to#33507
Am Freitag, 22. Januar 2016 12:30:47 UTC+1 schrieb Martin Gregorie:
> On Fri, 22 Jan 2016 00:19:07 -0800, Philipp Kraus wrote:
> 
> > Hello,
> > 
> > I'm trying to create a flexible (dynamic) binding for method calls and I
> > have got a problem with overloaded method binding.
> > In detail: I have created an own programming language which should call
> > a java method. In my own language I have got grammar constructs like
> > "foo/bar( "myobject" );". Internal I have got a Map<String, Object>
> > which maps the string parameter to an object which will be invoked.
> > foo/bar points to the method, in this case foo is the classname and bar
> > the method name. So in this example I need a binding to the method
> > "public void bar();".
> > My language should also use calls like "foo/bar( "myobject", 1 );". In
> > this case the Java method "void bar( int x );" should be called.
> > 
> > The basic invoking call works well, but I overloaded method creates a
> > problem. If I have got the class
> > 
> > class foo {
> >      public void bar()
> >      {
> >           System.out.println("test");
> >      }
> > }
> > 
> > I do the following:
> > 
> > final Multimap<String, MethodHandle> y = HashMultimap.create();
> > for ( final Method x : foo.class.getDeclaredMethods() )
> >     y.put( x.getSimpleName(), MethodHandles.lookup().unreflect(x) );
> > 
> > This multimap is put to the parser / lexer of my language to resolve
> > method names but on calling / invoking I get the problem, that I need
> > the correct methodhandle within the multimap depend on the signature.
> > 
> > The problem is a class like class foo {
> >      public void bar()
> >      {
> >           System.out.println("test");
> >      }
> > 
> >      public void bar( int x )
> >      {
> >           System.out.println("test " + x);
> >      }
> > }
> > 
> > So my question is, how can I create a dynamic binding on method handles
> > depend on the signature?
> > And can I cache the method handles / is it a good choice of caching all
> > handles? In my case I know during runtime the concrete signature of the
> > method, "foo/bar" is executed and on this time I know all arguments of
> > the method, so I can create at this point the binding. How can I do this
> > in a fast way?
> >
> Also not answering your specific question, but have you considered using 
> a compiler generator such as Coco/R?
> 
> http://www.ssw.uni-linz.ac.at/Research/Projects/Coco/
> 
> Compiler generators read a definition of the input language and generate
> the equivalent output which, in the case of the Java version of Coco/R, 
> would be a Java program that can parse and process files written in the 
> input language. 
> 
> What the generated program does with its input is entirely up to its 
> author: in your case you'd probably want it to be a preprocessor that can 
> read your language and output the equivalent logic as compilable Java 
> source code, but it could equally interpret the input as a script and 
> execute it as its being read, or even generate bytecode directly.
> 
> Personal use cases:
> 
> I've used lex and yacc, C equivalents of Coco/R, to parse a complex data 
> definition language and use SQL statements to create database tables from 
> it which formed an active data dictionary that managed the input of data 
> into a data warehouse.
> 
> I used Coco/R to generate Java code to parsed and interpret C 
> preprocessor directives as part of a C documentation generator that does 
> a similar job to Javadocs.


Nice tool. I'm using AntLR for generating my parser / lexer, but in my case I need a lazy / dynamic binding during runtime. So I need the name of the method during the AntLR parser runs through the code but during execution of my data structure I know the correct bindto object and the full signature of the method. So during parsing time I know only the class and method name.

[toc] | [prev] | [next] | [standalone]


#33512

FromJan Burse <janburse@fastmail.fm>
Date2016-01-22 16:49 +0100
Message-ID<n7tj2u$f0m$1@news.albasani.net>
In reply to#33503
Hi,


If you know the signature, in the sense of knowing the
classes of the formal parameter, you can call
    getDeclaredMethod() [1]
Instead of
    getDeclaredMethods()

So in case of your foo example below, to find the argument
less bar() method, call
     getDeclaredMethod("bar", new Class[]{})
to find the bar() method with primitive integer type, call
     getDeclaredMethod("bar", new Class[]{Integer.TYPE})

Yes caching is a good idea in my opinion.

Bye

[1] 
https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getDeclaredMethod%28java.lang.String,%20java.lang.Class...%29

Philipp Kraus schrieb:
> The problem is a class like
> class foo
> {
>       public void bar()
>       {
>            System.out.println("test");
>       }
>
>       public void bar( int x )
>       {
>            System.out.println("test " + x);
>       }
> }
>
> So my question is, how can I create a dynamic binding
 > on method handles depend on the signature?

> And can I cache the method handles / is it a good choice
 > of caching all handles?

> In my case I know during runtime the concrete signature
 > of the method, "foo/bar" is executed and on this
 > time I know all arguments of the method, so I can
 > create at this point the binding. How can I do this
 > in a fast way?

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web