Return-Path: X-Original-To: apmail-groovy-users-archive@minotaur.apache.org Delivered-To: apmail-groovy-users-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id C011C1013F for ; Tue, 26 May 2015 21:03:06 +0000 (UTC) Received: (qmail 72877 invoked by uid 500); 26 May 2015 21:03:01 -0000 Delivered-To: apmail-groovy-users-archive@groovy.apache.org Received: (qmail 72845 invoked by uid 500); 26 May 2015 21:03:01 -0000 Mailing-List: contact users-help@groovy.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@groovy.incubator.apache.org Delivered-To: mailing list users@groovy.incubator.apache.org Received: (qmail 72835 invoked by uid 99); 26 May 2015 21:03:01 -0000 Received: from Unknown (HELO spamd2-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 26 May 2015 21:03:01 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd2-us-west.apache.org (ASF Mail Server at spamd2-us-west.apache.org) with ESMTP id 16D381A3503 for ; Tue, 26 May 2015 21:03:01 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd2-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -0.121 X-Spam-Level: X-Spam-Status: No, score=-0.121 tagged_above=-999 required=6.31 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_PASS=-0.001] autolearn=disabled Authentication-Results: spamd2-us-west.apache.org (amavisd-new); dkim=pass (2048-bit key) header.d=gmail.com Received: from mx1-eu-west.apache.org ([10.40.0.8]) by localhost (spamd2-us-west.apache.org [10.40.0.9]) (amavisd-new, port 10024) with ESMTP id 5cn-vN6mqSOp for ; Tue, 26 May 2015 21:03:00 +0000 (UTC) Received: from mail-wi0-f180.google.com (mail-wi0-f180.google.com [209.85.212.180]) by mx1-eu-west.apache.org (ASF Mail Server at mx1-eu-west.apache.org) with ESMTPS id BF60120FF0 for ; Tue, 26 May 2015 21:02:59 +0000 (UTC) Received: by wizo1 with SMTP id o1so4378633wiz.1 for ; Tue, 26 May 2015 14:02:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=UJJotDyhziNGltpnHaLWw1sJCVQYezDUhB0JF/CXCOU=; b=tWUb7ZTrLGM7CZon8koM4g2mD5X9Ns0E4o7aWEghQ+QCKglAs4eF54sCL1kC3gpjaU UrrCw2xrHihA1q1CBD2L6zzT9WLyGPb+R/VXWsl0tu8QKbNdB7LL+GnbmmbpHq9uLEZr zgK1Ywh2tvGW5Jj3R3Nt7X1fMNSoad4lqxbhQL7Q/m0l7BMz9vHz8KHjZXckdNgLwvKp tgumOeOg6Be1eUcpwtO27Jpi1aW7sWnmOpf/Q/dQhI4YRy+gZvrCd2Pqa7J1iN7g1a4X yzr8uODoC0G/fC1Ctz+AEGPlAWG8lwFRpCnZal8cXvtQEN8odM3crWnxpCI2NGDLvu4D Zn2Q== X-Received: by 10.181.13.5 with SMTP id eu5mr42911125wid.37.1432674179534; Tue, 26 May 2015 14:02:59 -0700 (PDT) Received: from [192.168.43.214] (188.29.165.13.threembb.co.uk. [188.29.165.13]) by mx.google.com with ESMTPSA id ew10sm287456wic.22.2015.05.26.14.02.57 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 26 May 2015 14:02:58 -0700 (PDT) Message-ID: <5564DF80.5090707@gmail.com> Date: Tue, 26 May 2015 22:02:56 +0100 From: =?UTF-8?B?U2NoYWxrIENyb25qw6k=?= User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 MIME-Version: 1.0 To: users@groovy.incubator.apache.org Subject: Re: Dynamic adding properties to class References: <55648F50.50806@gmail.com> In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit I did at one stage, and the result is the same. The only way I could get it to work is something like class A { private Expando props = new Expando() void addSomeProp( String name, String key, def value) { def p = props.getProperty(name) if (null = p) { props.setProperty( name,[:] ) } props."${name}"[key]=value.toString() } def missingProperty( String name ) { def p = props.getProperty(name) if( null = p ) { throw new MissingPropertyException( name,this.class ) } return p } } Which is strange, as it is what I think ExpandoMetaClass would do anyway. On 26/05/2015 19:33, KARR, DAVID wrote: >> -----Original Message----- >> From: Schalk Cronjé [mailto:ysb33r@gmail.com] >> Sent: Tuesday, May 26, 2015 8:21 AM >> To: users@groovy.incubator.apache.org >> Subject: Dynamic adding properties to class >> >> This should be straight-forward, but it is failing for me (at least >> on >> 2.3x and 2.4.x). What am I doing wrong? >> >> Here is some code sufficiently distilled. >> >> class A { >> void addSomeProp( String name,String key, def value ) { >> if(metaClass.getMetaProperty(name)==null) { >> metaClass."${name}" = [:] >> } >> metaClass."${name}"[key]=value.toString() >> } >> } > Perhaps try "metaClass.hasProperty(name)" instead? > >> A a = new A() >> a.addSomeProp( 'info','version',3) // Fails with: No such >> property: >> info for class: groovy.lang.MetaClassImpl >> >> The idea is to add a map (of name) if it does not exist, and then >> add >> items into the map. >> >> -- >> Schalk W. Cronjé >> Twitter / Ello / Toeter : @ysb33r -- Schalk W. Cronjé Twitter / Ello / Toeter : @ysb33r