Re: Proxying how to?!? (was: changing dynamically the name of classes in a source code)

2016-03-29 Thread OC
Jason,

thanks for a quick response!

On 29. 3. 2016, at 19:09, "Winnebeck, Jason"  
wrote:

> You still have to follow the rules of Java bytecode

right, that's why I wrote it's a Java fault (inherited by Groovy), not a Groovy 
fault.

> that is your class DumbProxy is not related by class hierarchy or interface 
> to AnyClassOfMine, so you can't cast to it.

That is precisely the problem: without casting to it, it can't be used.

> You have to use def/Object type and Groovy's duck typing, or you need to make 
> DumbProxy extend AnyClassOfMine

Far as I understand the Groovy dispatch, this would force me not to go through 
a relatively clean missingXXX APIs; instead, I would probably have to exploit 
metaclass functionality directly, or something like that.

Well OK, that I can live with (unless it gets really slow, which it probably 
would not).

But what if the target class is final? Would never happen with AnyClassOfMine 
of course, but would happen with 3rd party classes whose instances I might need 
to proxy.

> or make an interface for them both to implement and use that.

Same problem there: whilst I can turn AnyClassOfMine to implement anything, I 
can't turn 3rd party classes to do that; and proxying 3rd party classes is 
essential (after all, with my own classes I can easily use other tricks than 
proxying to achieve the same result).

> Other options to consider include:
> http://docs.groovy-lang.org/latest/html/gapi/groovy/util/Proxy.html

Correct me please if I am overlooking anything, but it seems to me this is just 
slightly better implementation of DumbProxy. To redirect methods it uses 
invokeMethod instead of methodMissing (perhaps it is faster? Dunno. One 
advantage might be it would technically allow to forward even methods 
implemented in Proxy itself, but that is prevented by the implementation); but 
it _does_ share the very same typecasting problem we have just established with 
my DumbProxy.

Or does it not? If not, how comes?

> http://docs.groovy-lang.org/latest/html/gapi/groovy/util/ProxyGenerator.html

Well this seems really to create an instance of a dynamically made subclass 
(whoa! But I can see there's nothing else we can do with the bloody Java 
inherited behaviour), but I must be missing something. That instance does not 
work as a proxy, but seems to be half-proxy half-real instance of the target 
class.

Far as I has been able to test, it seems that

pg.instantiateDelegateWithBaseClass([:],[],delegate,class)

(a) creates a new instance of (a private subclas of) given “class” (OK)
(b) which instance gets all the methods (including property getters and 
setters) “class” contains, directly, without notifying the “delegate” anyhow 
(COMPLETELY WRONG)
(c) and only properties a methods the “class” does _not_ contain are directed 
to the delegate (OK)

The functionality needed for proxying differs from that:

(a) the proxied object (a server) exists before a proxy is created;
(b) the delegate (far as I understand what is its purpose?!?) should get _all_ 
the methods called “of the proxy”...
(c) ... so that it can (pre- or post-process them if need be and) forward them 
to the real proxied object.

Here's my testing code; perhaps you can point out what's wrong? The 
documentation is seriously lacking :)

===
class AnyClassOfMine {
  def xxname // renamed from 'name' to be triple sure not to clash with proxy 
name or something
  def foo(bar) { println "foo of $this called with $bar"; 
"${bar.toUpperCase()}, I am $xxname" }
}
class Delegate {
  def server
  def propertyMissing(String name) {
println "- delegate asked for a property '$name' for server $server"
if (name!='xxname') return 'nonexistent'
server."$name"
  }
  def propertyMissing(String name,value) {
println "- delegate asked to set a property '$name' to $value for server 
$server"
if (name!='xxname') return 'nonexistent'
server."$name"
  }
  def methodMissing(String name, args) {
println "- delegate asked for a method '$name'$args for server $server"
if (name!='foo') return 'nonexistent'
server."$name"(*args)
  }
}

def proxied=new AnyClassOfMine(xxname:"Proxied"),delegate=new 
Delegate(server:proxied)
assert delegate.xxname=='Proxied'

def pg=groovy.util.ProxyGenerator.INSTANCE // is this the proper way to do it?
def 
proxy=pg.instantiateDelegateWithBaseClass([:],[],delegate,delegate.server.class)
assert proxy instanceof AnyClassOfMine

println "=== Delegate gets unknown method allright:"
println "-> ${proxy.unknown('hello')}"
println "=== Known method implemented by $proxy, instead of being sent through 
delegate to $proxied!"
println "-> ${proxy.foo('hello')}"
println "=== It should look like this:"
println "-> ${delegate.foo('hello')}"

println "=== Exactly same problem with properties: unknown one correctly 
forwarded..."
println "-> $proxy.unknownProperty"
println "=== Known property though processed directly by proxy, not forwarded 
through 

Re: changing dynamically the name of classes in a source code

2016-03-29 Thread frenchy48
answer found
Groovy is fantastic!
Since I use the groovyConsole for my teaching scripts
I created a subclass of groovy console
then modified the CompilerConfiguration to accept an importCustomizer that
aliases import!
fun!



-
member of Grumpy Old Programmers
--
View this message in context: 
http://groovy.329449.n5.nabble.com/changing-dynamically-the-name-of-classes-in-a-source-code-tp5732080p5732103.html
Sent from the Groovy Users mailing list archive at Nabble.com.


Re: Check if List is of specific size and elements of specific type

2016-03-29 Thread Søren Berg Glasius (GR8Conf EU)
@dinko yeah, that's why I choose to do my example with listVariable*.getClass() 

Best regards,
Søren Berg Glasius
GR8Conf Europe organizing team

GR8Conf ApS
Mobile: +45 40 44 91 88, Web: www.gr8conf.eu, Skype: sbglasius 
Company Address: Buchwaldsgade 50, 5000 Odense C, Denmark
Personal Address: Hedevej 1, Gl. Rye, 8680 Ry, Denmark
--- GR8Conf - Dedicated to the Groovy Ecosystem

From: Dinko Srkoč 
Reply: users@groovy.apache.org 
Date: March 29, 2016 at 14:04:50
To: users@groovy.apache.org 
Subject:  Re: Check if List is of specific size and elements of specific type  

On 29 March 2016 at 12:29, Marcos Carceles  wrote:  
> Would this work?  
>  
> listVariable*.class == [Integer, Integer]  

It would in this particular example, but this may be dangerous for  
some other cases. Try e.g. this:  

[1, [:], [class: 1]]*.class  

Cheers,  
Dinko  

>  
> On 29 March 2016 at 12:25, Maarten Boekhold  wrote:  
>>  
>> Hi,  
>>  
>> Is there a quick and easy way to do something like:  
>>  
>> assert listVariable == [int, int]  
>>  
>> eg, the list is of size 2 and each element is an int?  
>>  
>> Maarten  
>  
>  


Re: Check if List is of specific size and elements of specific type

2016-03-29 Thread Dinko Srkoč
On 29 March 2016 at 12:29, Marcos Carceles  wrote:
> Would this work?
>
> listVariable*.class == [Integer, Integer]

It would in this particular example, but this may be dangerous for
some other cases. Try e.g. this:

  [1, [:], [class: 1]]*.class

Cheers,
Dinko

>
> On 29 March 2016 at 12:25, Maarten Boekhold  wrote:
>>
>> Hi,
>>
>> Is there a quick and easy way to do something like:
>>
>> assert listVariable == [int, int]
>>
>> eg, the list is of size 2 and each element is an int?
>>
>> Maarten
>
>


Re: Check if List is of specific size and elements of specific type

2016-03-29 Thread Marcos Carceles
Would this work?

listVariable*.class == [Integer, Integer]

On 29 March 2016 at 12:25, Maarten Boekhold  wrote:

> Hi,
>
> Is there a quick and easy way to do something like:
>
> assert listVariable == [int, int]
>
> eg, the list is of size 2 and each element is an int?
>
> Maarten
>


Re: Check if List is of specific size and elements of specific type

2016-03-29 Thread Søren Berg Glasius (GR8Conf EU)
Hi Maarten,

You could be close with this.

def listVariable = [1,2]
assert listVariable*.getClass() == [int, int]

but your assert will have to be

assert listVariable*.getClass() == [Integer, Integer]

since ints are actually the object type Integer

Best regards,
Søren Berg Glasius
GR8Conf Europe organizing team

GR8Conf ApS
Mobile: +45 40 44 91 88, Web: www.gr8conf.eu, Skype: sbglasius 
Company Address: Buchwaldsgade 50, 5000 Odense C, Denmark
Personal Address: Hedevej 1, Gl. Rye, 8680 Ry, Denmark
--- GR8Conf - Dedicated to the Groovy Ecosystem

From: Maarten Boekhold 
Reply: users@groovy.apache.org 
Date: March 29, 2016 at 12:25:42
To: us...@groovy.incubator.apache.org 
Subject:  Check if List is of specific size and elements of specific type  

Hi,  

Is there a quick and easy way to do something like:  

assert listVariable == [int, int]  

eg, the list is of size 2 and each element is an int?  

Maarten