@John,
Great work. I am thinking about adding Traits to my pet project
https://pec.dev.java.net/. Your concept of using a factory,
Trait4J.createInstance, might make this easier to do.
On Nov 9, 11:39 am, "John Nilsson" <[EMAIL PROTECTED]> wrote:
> I created a little proof of concept using Javassist
> The class inheriting the trait isn't much more advanced than this. The trick
> is to make the class abstract and have the library handle instantiation and
> implementation.
>
> public abstract class HelloWorld implements Swedish
> {
> public void sayHello()
> {
> System.out.println(hello());
>
> }
> }
>
> For a library API I guess this is ok.
>
> public class HelloWorldTest {
>
> public static void main(String[] args) {
> final HelloWorld hw = Trait4J.createInstance(HelloWorld.class);
> hw.sayHello();
>
> }
> }
>
> The actual trait leaves something to be desired. But I guess it's the best
> we can do.
>
> @Implementation(Swedish.Impl.class)
> public interface Swedish
> {
> public static abstract class Impl implements Swedish
> {
> public String hello()
> {
> return "Hej";}
> }
>
> public String hello();
>
> }
>
> The (vary naive) library just creates a subclass and then spawns instances
> from this.
>
> public static <T> Class<? extends T> implementTraits(Class<T> clazz)
> {
> try
> {
> final ClassPool cp = ClassPool.getDefault();
> final CtClass target = cp.get(clazz.getName());
> final CtClass impl = cp.makeClass(target.getName() + "Impl");
> for(Class<?> iface : clazz.getInterfaces())
> {
> final Implementation implSrc = iface.getAnnotation(Implementation.class);
> if(implSrc != null)
> {
> for(CtMethod sm : cp.get(implSrc.value().getName()).getDeclaredMethods())
> {
> impl.addMethod(CtNewMethod.copy(sm, impl, null));}
> }
> }
>
> impl.setSuperclass(target);
> return impl.toClass();}
>
> catch (Exception e) {
> e.printStackTrace();
>
> }
> return null;
> }
>
> BR,
> John
>
> On Thu, Nov 6, 2008 at 10:35 PM, Jess Holle <[EMAIL PROTECTED]> wrote:
> > There are big limitations imposed by dynamic proxies here as I see it.
> > Dynamic proxies are beautiful interceptors for one or more interfaces, but
> > they themselves are of a synthetic class.
>
> > If you have a class A and you want to mix-in/apply an interface to it,
> > dynamic proxies could help wire a default implementation around an
> > interface, but you'd still have to manually delegate to the proxy from your
> > class. Overall it still leaves a gaping "can't get there from here" hole in
> > the middle of things.
>
> > John Nilsson wrote:
>
> > I haven't done any benchmarking. But I can't imagine the overhead being
> > "terrible", mesurable: yes, but I hear the reflection stuff in Java is
> > rather quick.
> > If performance is a problem you could probably use some runtime class
> > generation instead. But I imagine it would take a little more effort to
> > assemble such a system.
>
> > BR,
> > John
>
> > On Thu, Nov 6, 2008 at 8:20 PM, Casper Bang <[EMAIL PROTECTED]> wrote:
>
> >> But aren't dynproxies terribly slow? I thought that's why people
> >> dabble on invokedynamic.
>
> >> /Casper
>
> >> On Nov 6, 8:16 pm, "John Nilsson" <[EMAIL PROTECTED]> wrote:
> >> > It shouldn't be to hard to implement traits as a library in current Java
> >> > using a dynamic proxy.
> >> > Should make for an interesting project.
>
> >> > BR,
> >> > Jphn
>
> >> > On Wed, Nov 5, 2008 at 3:00 AM, Mark Derricutt <[EMAIL PROTECTED]>
> >> wrote:
> >> > > Please please please bring on traits! I'm somewhat on the fence of
> >> rather
> >> > > seeing traits than closures in java sooner than the other.
>
> >> > > I'm finding LOTS of places in my code where traits would just make
> >> things
> >> > > cleaner.
>
> >> > > More and more I think I just want scala :)
>
> >> > > On Wed, Nov 5, 2008 at 12:15 PM, hlovatt <[EMAIL PROTECTED]>
> >> wrote:
>
> >> > >> I thinks that Traits are a great idea for Java and judging by #215
> >> the
> >> > >> posse, particularly Dick, like them. I wrote about them for Java 7
> >> in:
>
> >> > >>http://www.artima.com/weblogs/viewpost.jsp?thread=220916
>
> >> > >> What do you think?
>
> >> > > --
> >> > > "It is easier to optimize correct code than to correct optimized
> >> code." --
> >> > > Bill Harlan
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "The
Java Posse" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/javaposse?hl=en
-~----------~----~----~----~------~----~------~--~---