This page is no longer maintained — Please continue to the home page at www.scala-lang.org

A Simple Scala Compiler Plugin, failed to create a new Class

2 replies
iron9light
Joined: 2009-07-04,
User offline. Last seen 3 years 15 weeks ago.

This's a very simple plugin.

The original class is here:

@autoinject
trait X extends (() => Int) {
def name: String
val someValue: Int
def p = classOf[X]
def apply() = 5
}

And I wanna generate a class below:

private class XX @javax.inject.Inject()(val name: String, val
someValue: Int) extends X

Here's the code: https://github.com/iron9light/autoguice

It's nearly finished, except the generated constructor:

TREE: private class XImpl extends java.lang.Object with simple.X with
ScalaObject {
private[this] val name: String = _;
private[this] val someValue: Int = _;
private[this] def (name: String, someValue:
Int) = {
super.();
()
};
def name: String =
XImpl.this.name;
def someValue: Int =
XImpl.this.someValue
}

Can anyone help, PLZ!

Best regards,

IL

iron9light
Joined: 2009-07-04,
User offline. Last seen 3 years 15 weeks ago.
Re: A Simple Scala Compiler Plugin, failed to create a new Class
Better now. no exception when run compiler.But get the errors:
error: class XImpl needs to be abstract, since:method someValue in trait X of type => Int is not definedmethod name in trait X of type => String is not defined

Here's the tree:
ClassDef( // sym=class XImpl, sym.owner=package simple, sym.tpe=XImpl, tpe=<notype>, tpe.sym=<none>  vate <synthetic> <implclass/presuper>, // flags=private <synthetic> <implclass/presuper>, annots=List()  "XImpl",  List(), // no type parameter  Template( // sym=value <local XImpl>, sym.owner=class XImpl, sym.tpe=<notype>, tpe=XImpl, tpe.sym=class XImpl, tpe.sym.owner=package simple, tpe.decls=List(value name: String, , value name: String, , value someValue: Int, , value someValue: Int, , constructor XImpl: (name: String, someValue: Int)XImpl, )    List(class Object, trait X, trait ScalaObject), // parents    ValDef( // sym=<none>, sym.tpe=<notype>, tpe=<notype>, tpe.sym=<none>      0, // flags=, annots=List()      "_",      TypeTree(), // sym=<none>, tpe=<notype>, tpe.sym=<none>,      EmptyTree    ),    List( // body      ValDef( // sym=value name, sym.owner=class XImpl, sym.tpe=String, tpe=<notype>, tpe.sym=<none>        vate <local> <paramaccessor>, // flags=private <local> <paramaccessor>, annots=List()        "name",        TypeTree(), // sym=class String, tpe=String, tpe.sym=class String, tpe.sym.owner=package lang,        EmptyTree      ),      ValDef( // sym=value someValue, sym.owner=class XImpl, sym.tpe=Int, tpe=<notype>, tpe.sym=<none>        vate <local> <paramaccessor>, // flags=private <local> <paramaccessor>, annots=List()        "someValue",        TypeTree(), // sym=class Int, tpe=Int, tpe.sym=class Int, tpe.sym.owner=package scala,        EmptyTree      ),      DefDef( // sym=constructor XImpl, isConstructor, sym.owner=class XImpl, sym.tpe=(name: String, someValue: Int)XImpl, tpe=<notype>, tpe.sym=<none>        thod>, // flags=<method>, annots=List()        "<init>",        List(), // no type parameter        List(          List( // 2 parameter(s)            ValDef( // sym=value name, sym.owner=constructor XImpl, sym.tpe=String, tpe=<notype>, tpe.sym=<none>              ram>, // flags=<param>, annots=List()              "name",              TypeTree(), // sym=class String, tpe=String, tpe.sym=class String, tpe.sym.owner=package lang,              EmptyTree            ),            ValDef( // sym=value someValue, sym.owner=constructor XImpl, sym.tpe=Int, tpe=<notype>, tpe.sym=<none>              ram>, // flags=<param>, annots=List()              "someValue",              TypeTree(), // sym=class Int, tpe=Int, tpe.sym=class Int, tpe.sym.owner=package scala,              EmptyTree            )          )        ),        java.lang.Object with simple.X with ScalaObject,        Block( // sym=null, tpe=Unit, tpe.sym=class Unit, tpe.sym.owner=package scala          List( // 1 statement(s)            Apply( // sym=constructor Object, tpe=java.lang.Object, tpe.sym=class Object, tpe.sym.owner=package lang              Select( // sym=constructor Object, isPrimaryConstructor, sym.owner=class Object, sym.tpe=()java.lang.Object, tpe=()java.lang.Object, tpe.sym=<none>                Super("") // sym=class XImpl, tpe=XImpl.super.type, tpe.sym=class XImpl, tpe.sym.owner=package simple, tpe.decls=List(constructor Object: ()java.lang.Object, , method getClass: ()java.lang.Class[_], , method hashCode: ()Int, , method equals: (x$1: Any)Boolean, , method clone: ()java.lang.Object, , method toString: ()java.lang.String, , method notify: ()Unit, , method notifyAll: ()Unit, , method wait: (x$1: Long)Unit, , method wait: (x$1: Long, x$2: Int)Unit, , method wait: ()Unit, , method finalize: ()Unit, , method ##: ()Int, , method ==: (x$1: AnyRef)Boolean, , method !=: (x$1: AnyRef)Boolean, , method eq: (x$1: AnyRef)Boolean, , method ne: (x$1: AnyRef)Boolean, , method synchronized: [T0](x$1: T0)T0, , method $isInstanceOf: [T0]()Boolean, , method $asInstanceOf: [T0]()T0, )                  This(""), // sym=class XImpl, sym.owner=package simple, sym.tpe=XImpl, tpe=XImpl, tpe.sym=class XImpl, tpe.sym.owner=package simple, tpe.decls=List(value name: String, , value name: String, , value someValue: Int, , value someValue: Int, , constructor XImpl: (name: String, someValue: Int)XImpl, )                "<init>"),              Nil // no argument            )          ),          Literal(Constant(()))        )      ),      DefDef( // sym=method name, sym.owner=class XImpl, sym.tpe==> String, tpe=<notype>, tpe.sym=<none>        thod> <synthetic> <accessor> <paramaccessor>, // flags=<method> <synthetic> <accessor> <paramaccessor>, annots=List()        "name",        List(), // no type parameter        List(        ),        String,        Select( // sym=method name, sym.owner=trait X, sym.tpe==> String, tpe=String, tpe.sym=class String, tpe.sym.owner=package lang          This("XImpl"), // sym=class XImpl, sym.owner=package simple, sym.tpe=XImpl, tpe=XImpl.this.type, tpe.sym=class XImpl, tpe.sym.owner=package simple, tpe.decls=List(value name: String, , value name: String, , value someValue: Int, , value someValue: Int, , constructor XImpl: (name: String, someValue: Int)XImpl, )          "name")      ),      DefDef( // sym=method someValue, sym.owner=class XImpl, sym.tpe==> Int, tpe=<notype>, tpe.sym=<none>        thod> <synthetic> <accessor> <paramaccessor>, // flags=<method> <synthetic> <accessor> <paramaccessor>, annots=List()        "someValue",        List(), // no type parameter        List(        ),        Int,        Select( // sym=method someValue, sym.owner=trait X, sym.tpe==> Int, tpe=Int, tpe.sym=class Int, tpe.sym.owner=package scala          This("XImpl"), // sym=class XImpl, sym.owner=package simple, sym.tpe=XImpl, tpe=XImpl.this.type, tpe.sym=class XImpl, tpe.sym.owner=package simple, tpe.decls=List(value name: String, , value name: String, , value someValue: Int, , value someValue: Int, , constructor XImpl: (name: String, someValue: Int)XImpl, )          "someValue")      )    )  ))
iron9light
Joined: 2009-07-04,
User offline. Last seen 3 years 15 weeks ago.
Re: A Simple Scala Compiler Plugin, failed to create a new Class
Give up generating any tree after typer phase.Now generate tree after parser, and add annotations after typer.
It work, baby.
Have a try.
https://github.com/iron9light/autoguice


IL

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland