Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7492603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:28:50+00:00 2026-05-29T16:28:50+00:00

In a Scala Compiler Plugin, I’m trying to create a new class that implement

  • 0

In a Scala Compiler Plugin, I’m trying to create a new class that implement a pre-existing trait. So far my code looks like this:

def trait2Impl(original: ClassDef, newName: String): ClassDef = {
    val impl = original.impl
    // Seems OK to have same self, but does not make sense to me ...
    val self = impl.self
    // TODO: implement methods ...
    val body = impl.body
    // We implement original
    val parents = original :: impl.parents
    val newImpl = treeCopy.Template(impl, parents, self, body)
    val name = newTypeName(newName)
    // We are a syntheic class, not a user-defined trait
    val mods = (original.mods | SYNTHETIC) &~ TRAIT
    val tp = original.tparams
    val result = treeCopy.ClassDef(original, mods, name, tp, newImpl)
    // Same Package?
    val owner = original.symbol.owner
    // New symbol. What's a Position good for?
    val symbol = new TypeSymbol(owner, NoPosition, name)
    result.setSymbol(symbol)
    symbol.setFlag(SYNTHETIC)
    symbol.setFlag(ABSTRACT)
    symbol.resetFlag(INTERFACE)
    symbol.resetFlag(TRAIT)
    owner.info.decls.enter(symbol)
    result
}

But it doesn’t seem to get added to the package. I suspect that is because actually the package got “traversed” before the trait that causes the generation, and/or because the “override def transform(tree: Tree): Tree” method of the TypingTransformer can only return one Tree, for every Tree that it receives, so it cannot actually produce a new Tree, but only modify one.

So, how do you add a new Class to an existing package? Maybe it would work if I transformed the package when “transform(Tree)” gets it, but I that point I don’t know the content of the package yet, so I cannot generate the new Class this early (or could I?). Or maybe it’s related to the “Position” parameter of the Symbol?

So far I found several examples where Trees are modified, but none where a completely new Class is created in a Compiler Plugin.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T16:28:53+00:00Added an answer on May 29, 2026 at 4:28 pm

    The full source code is here: https://gist.github.com/1794246

    The trick is to store the newly created ClassDefs and use them when creating a new PackageDef. Note that you need to deal with both Symbols and trees: a package symbol is just a handle. In order to generate code, you need to generate an AST (just like for a class, where the symbol holds the class name and type, but the code is in the ClassDef trees).

    As you noted, package definitions are higher up the tree than classes, so you’d need to recurse first (assuming you’ll generate the new class from an existing class). Then, once the subtrees are traversed, you can prepare a new PackageDef (every compilation unit has a package definition, which by default is the empty package) with the new classes.

    In the example, assuming the source code is

    class Foo {
      def foo {
        "spring"
      }
    }
    

    the compiler wraps it into

    package <empty> {
      class Foo {
        def foo {
          "spring"
        }
      }
    }
    

    and the plugin transforms it into

    package <empty> {
      class Foo {
        def foo {
          "spring"
        }
      }
      package mypackage {
        class MyClass extends AnyRef
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my quest to generate new code in a Scala compiler plugin, I have
Why doesn't scalac (the Scala compiler) optimize tail recursion? Code and compiler invocations that
I'm new to Scala and don't know Java. I want to create a jar
using the CPS compiler-plugin of Scala 2.8, there are the two magic controls reset
I have a scala project that I use Maven and the maven-scala-plugin to compile.
So I'm trying to compile the scala compiler, I've overcome many problems trying to
I am a very new bie for scala. So I download the source code
The Scala compiler compiles direct to Java byte code (or .NET CIL). Some of
I'm trying to understand what Scala does with Case Classes that makes them somehow
sbt downloads new version of scala compiler and the scala library for every sbt

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.