[Nice-commit] Nice/stdlib/nice/lang/reflect promotion-demotion.nice,NONE,1.1 instances.nice,1.2,1.3
Brought to you by:
bonniot
From: Daniel B. <bo...@us...> - 2004-07-30 19:09:21
|
Update of /cvsroot/nice/Nice/stdlib/nice/lang/reflect In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6432/stdlib/nice/lang/reflect Modified Files: instances.nice Added Files: promotion-demotion.nice Log Message: java.lang.Class is now parameterized by the type it represents. --- NEW FILE: promotion-demotion.nice --- /**************************************************************************/ /* N I C E */ /* A high-level object-oriented research language */ /* (c) Daniel Bonniot 2004 */ /* */ /* This program is free software; you can redistribute it and/or modify */ /* it under the terms of the GNU General Public License as published by */ /* the Free Software Foundation; either version 2 of the License, or */ /* (at your option) any later version. */ /* */ /**************************************************************************/ package nice.lang.reflect; /** @param source an object of type T @param target the Class of type U, a supertype of T @return a new instance of class U, keeping all relevant fields from object <code>source</code> */ public <T, U | T <: U> U demote(T source, Class<U> target) { // Create the new object U res = target.newInstance(); for (field : target.getFields()) println(""field[res]" => "field[source]); // Initialize all fields of the new object by copy from the source for (field : target.getFields()) field[res] = field[source]; return res; } /** @param source an object of non-null type T @param target the Class of type U, a subtype of T @return a new instance of class U, keeping all fields from object <code>source</code> */ public <!T, U | U <: T> U promote(T source, Class<U> target) { Class<T> sourceClass = source.getClass(); U res = target.newInstance(); for (field : target.getFields()) { // Only copy fields that exist in the source class if (field.getDeclaringClass().isAssignableFrom(sourceClass)) field[res] = field[source]; } return res; } // Unit tests class _Dog { String name = ""; } class _Dalmatian extends _Dog { boolean spotted = false; } class _Chihuahua extends _Dog { boolean dancing = false; } void _testDemote() { _Dalmatian dalmatian = new _Dalmatian(name: "Dalmatian", spotted: true); _Dog dog = dalmatian.demote(nice.lang.reflect._Dog); assert dog.getClass() == nice.lang.reflect._Dog; assert dog.name.equals("Dalmatian"); Object o = dog.demote(Object.class); // Error detected at compile time: //String s = dog.demote(java.lang.String); } void _testPromote() { _Dog dog = new _Dog(name: "Simple dog"); _Dalmatian dalmatian = dog.promote(nice.lang.reflect._Dalmatian); assert dalmatian.getClass() == nice.lang.reflect._Dalmatian; assert dalmatian.name.equals("Simple dog"); assert ! dalmatian.spotted; _Chihuahua chihuahua = new _Chihuahua(name: "Chichi", dancing: true); dalmatian = chihuahua.promote(nice.lang.reflect._Dalmatian); assert dalmatian.getClass() == nice.lang.reflect._Dalmatian; assert dalmatian.name.equals("Chichi"); assert ! dalmatian.spotted; } Index: instances.nice =================================================================== RCS file: /cvsroot/nice/Nice/stdlib/nice/lang/reflect/instances.nice,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** instances.nice 12 Dec 2003 12:55:25 -0000 1.2 --- instances.nice 30 Jul 2004 19:08:42 -0000 1.3 *************** *** 52,59 **** ****************************************************************/ - public <T> T newInstance(Class) = native Object Class.newInstance(); - public <T> T newInstance(String className) = ! newInstance(Class.forName(className)); // Testcases --- 52,57 ---- ****************************************************************/ public <T> T newInstance(String className) = ! cast(newInstance(Class.forName(className))); // Testcases |