This question already has an answer here:
Calling a protected static Java method from Scala
(1 answer)
Closed 8 years ago.
I'm trying to compile a Scala class file that extends a Java class. Here is the class definition, it's as basic as it gets. On load, in the host application, the object should write "Hello from Scala" to the host's output window, using the superclass's method 'post'.
import com.cycling74.max.MaxObject;
public class FirstClass extends MaxObject{
public FirstClass{
post("Hello From Java");
}
}
This compiles and runs fine in the application.
Here is my translation to Scala (to be honest I'm not 100% sure I completely understand constructors in Scala):
import com.cycling74.max._
class FirstClass() extends MaxObject {
super.post("Hello from Scala")
}
But, when I try to compile with scalac, I receive the following error:
dm$ scalac -cp max.jar FirstClass.scala
FirstClass.scala:3: error: value post is not a member of com.cycling74.max.MaxObject with ScalaObject
super.post("Hello from Scala")
^
one error found
I'm not sure why the compiler is telling me that 'post' is not a member function, I'm certain that I've botched the Scala syntax, but cannot determine what is wrong.
Thanks!
EDIT
here's the output from the Max window, in addition to changing the code as prescribed below, i just added the Scala libs to Max's dynamic loadpath. This is exciting
MXJ System CLASSPATH:
/Applications/Max 6.1/Cycling '74/java/lib/jitter.jar
/Applications/Max 6.1/Cycling '74/java/lib/jode-1.1.2-pre-embedded.jar
/Applications/Max 6.1/Cycling '74/java/lib/max.jar
MXJClassloader CLASSPATH:
/Applications/Max 6.1/Cycling '74/java/classes/
/Users/dm/maxmsp/classes
/Users/dm/maxmsp/jars/jline.jar
/Users/dm/maxmsp/jars/scala-compiler.jar
/Users/dm/maxmsp/jars/scala-dbc.jar
/Users/dm/maxmsp/jars/scala-library.jar
/Users/dm/maxmsp/jars/scala-partest.jar
/Users/dm/maxmsp/jars/scala-swing.jar
/Users/dm/maxmsp/jars/scalacheck.jar
/Users/dm/maxmsp/jars/scalap.jar
Jitter initialized
Jitter Java support installed
Hello from Scala
Assuming the definitioin of post method in MaxObject as..
public class MaxObject {
public static void post(java.lang.String message){
System.out.println("printing from MaxObject.post :: " + message);
}
}
you can directly call the post method in scala as -
class FirstClass extends MaxObject {
MaxObject.post("Hello from Scala")
}
Infact, if you are not compelled to extend the MaxObject.. you can also use it as..
class FirstClass{
MaxObject.post("Hello from Scala")
}
Consuming it as :
val fc = new FirstClass //> printing from MaxObject.post :: Hello from Scala
Related
After the parser phase of the Scalac process, the following case class
case class ExampleCaseClass(var s:String, var i:Int) extends ContextuallyMutable
takes the intermediate form:
Clazz(case class ExampleCaseClass extends ContextuallyMutable with scala.Product with scala.Serializable {
<caseaccessor> <paramaccessor> var s: String = _;
<caseaccessor> <paramaccessor> var i: Int = _;
def <init>(s: String, i: Int) = {
super.<init>();
()
}
})
However, a run time reflection call:
ExampleCaseClass("Can a Scala compiler plugin transform the autogenerated accessor methods of scala case classes?", 42).getClass.getMethods.foreach(println(_))
reveals many more public methods:
public boolean ExampleCaseClass.equals(java.lang.Object)
public java.lang.String ExampleCaseClass.toString()
public int ExampleCaseClass.hashCode()
public static ExampleCaseClass ExampleCaseClass.apply(java.lang.String,int)
public int ExampleCaseClass.i()
public java.lang.String ExampleCaseClass.s()
public ExampleCaseClass ExampleCaseClass.copy(java.lang.String,int)
public void ExampleCaseClass.i_$eq(int)
public scala.collection.Iterator ExampleCaseClass.productElementNames()
public java.lang.String ExampleCaseClass.productElementName(int)
public void ExampleCaseClass.s_$eq(java.lang.String)
public int ExampleCaseClass.copy$default$2()
public boolean ExampleCaseClass.canEqual(java.lang.Object)
public java.lang.String ExampleCaseClass.productPrefix()
public int ExampleCaseClass.productArity()
public java.lang.Object ExampleCaseClass.productElement(int)
public scala.collection.Iterator ExampleCaseClass.productIterator()
public java.lang.String ExampleCaseClass.copy$default$1()
public static scala.Function1 ExampleCaseClass.tupled()
public static scala.Option ExampleCaseClass.unapply(ExampleCaseClass)
public static scala.Function1 ExampleCaseClass.curried()
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
Clearly some subsequent compiler phase creates the property accessor methods:
public int ExampleCaseClass.i()
public java.lang.String ExampleCaseClass.s()
public void ExampleCaseClass.i_$eq(int)
public void ExampleCaseClass.s_$eq(java.lang.String)
Which compilation phase generates these accessor methods and what manner of compiler plugin (or other means) might prevent or transform them?
The enquirer has already run numerous experiments removing or reshaping the:
<caseaccessor> <paramaccessor> var s: String = _;
<caseaccessor> <paramaccessor> var i: Int = _;
portions of the case class, and also with injecting the desired accessor methods in advance but no combination has met the desired outcome. They either fail to compile because of naming conflicts that arise in subsequent compilation phases, or they alter the parameter names in constructor, apply, and accessor methods.
Can a scala compiler plugin transform synthetic accessors at all? Does the Java Compiler introduce these methods? If so, should the enquirer look to Javac plugins and what analogues might serve the Scala.js and Scala native compilation targets?
Thank you for any consideration.
case class expansion happens in more than one place, see another question.
Instead of writing a new plugin just to disallow using var it would be much better to add a new rule to Wartremover or ScalaFix. As a matter of the fact, these rules already exist:
disallow var with Wartremover - combine it with fatal warnings to fail compilation on var
disallow var with ScalaFix
If you want to add more elaborate rule... it would still be easier just to write your own Wartremover/ScalaFix rule (the latter might be preferred as it is already supported in Scala 3).
And if you really need a custom compiler plugin to mess with code generated by compiler... take a look at better-toString plugin. It adds its own phase after "parser" phase. But I wouldn't hope for removing the autogenerated implementations. At best you can override them manually where specs allows you to.
The enquirer found a Scala 3 solution with persistence and help from examples:
better-tostring a plugin that demonstrates conditional method insertion by Polyvariant.
Compiler Plugin Development in Scala 3 Example and Tutorial by Scala Center dev: Fengyun Liu. The video provided insights into compiler phases and the example shed light on method body generation syntax. In particular, available documentation doesn't readily clarify how to call println from a method body generated as a Tree by a compiler plugin, but Liu's example plugin demonstrated requireModule and requiredMethod.
Scala 3 Compiler Plugin Documentation offers a very nice template for how to start writing a plugin. The final solution looked very similar.
As of 26 Nov 2021, no solution exists for Scala 2.13, but maybe that just means it is time to upgrade.
final class BlockMutatorPlugin extends StandardPlugin {
override val name: String = "BlockMutatorPlugin"
override val description: String = "Scala Compiler Plugin for blocking ContextuallyMutable setter methods."
override def init(options: List[String]): List[PluginPhase] = List(new BlockContextuallyMutableSetters)
}
class BlockContextuallyMutableSetters extends PluginPhase {
val phaseName = "blockGetter"
/* Running this plugin after phases before ElimErasedValueType
resulted in the replacement of the generated setter methods by the synthetic
default versions. By the time that this ElimErasedValueType phase ends, the
defaults already existed, so this plugin could augment them safely. */
override val runsAfter = Set(ElimErasedValueType.name)
private var printBlocked: Tree = _
override def prepareForTemplate(tree: tpd.Template)(using ctx: Context): Context = {
val cnsl = requiredModule("scala.Predef")
val prntln: PreName = "println".toTermName
val say = cnsl.requiredMethod(prntln, List[Types.Type](ctx.definitions.ObjectType))
printBlocked = ref(say).appliedTo(Literal(Constant("Blocked!")))
ctx
}
override def transformTemplate(tree: Template)(using ctx: Context): Tree = {
if (tree.parents.filter(_.symbol.name.toString.equals("ContextuallyMutable")).nonEmpty) {
cpy.Template(tree)(
body = tree.body.collect {
case dd: DefDef if dd.name.isSetterName => DefDef(
dd.symbol.asInstanceOf[Symbols.TermSymbol],
printBlocked
)
case x => x
}
).asInstanceOf[Tree]
} else tree
}
}
The most important part of this effort involved discovering which compiler phase this plugin should follow. Similar efforts in Scala 2.13.6 have failed so far; the only remaining impediment to the Scala 2 solution sought by this original Stack Overflow question. As such, the enquirer will not mark his own answer as the accepted solution unless future edits avail Scala 2. Until that time, your response may claim that designation.
For any inclined to try compile this example, the code above requires the following import statements:
import dotty.tools.dotc.ast.tpd
import tpd.*
import dotty.tools.dotc.core.*
import Names.PreName
import Symbols.{ClassSymbol, requiredMethod, requiredModule}
import Decorators.*
import NameOps.*
import Contexts.Context
import Constants.Constant
import dotty.tools.dotc.plugins.*
import dotty.tools.dotc.transform.ElimErasedValueType
I have a sealed class, written in Kotlin:
sealed class Schema {
class RecordSchema(val fields: List<Field>): Schema()
class ArraySchema(val elementSchema: Schema): Schema()
...
}
And another class that takes RecordSchema as a parameter:
class Enrichment(config: Config, val schema: RecordSchema) { ... }
In Scala I have a class that, among other things, get an instance of the RecordSchema and then create an instance of the Enrichment.
object Job {
def main(args: Array[String]): Unit = {
/// some initializing of resources... and then...
val recordSchema = schemas.getSchema(id) // type is Schema.RecordSchema
val enrichment = Enrichment(config, recordSchema) // this is where scalac errors out
}
}
The scala compiler ends up printing this error message:
Error:(52, 62) type mismatch;
found : com.companyname.enricher.schemas.com.companyname.enricher.schemas.com.companyname.enricher.schemas.
required: com.companyname.enricher.schemas.(some other)com.companyname.enricher.schemas.com.companyname.enricher.schemas.
val enrichment = new Enrichment(config, recordSchema)
If I cast recordSchema using asInstanceOf I get another, not so useful, error:
Error:(52, 62) type mismatch;
found : com.companyname.enricher.schemas.Schema.RecordSchema
required: com.companyname.enricher.schemas.com.companyname.enricher.schemas.
val enrichment = new Enrichment(config, recordSchema.asInstanceOf[RecordSchema])
Overall, I have no idea why this is happening. If I make the constructor take the parent class as a parameter rather than nested class, it compiles just fine (which is my workaround for now). If I do the same type of thing, except having written the sourcecode in Java rather than Kotlin, this error does not happen. I am using Java 1.8, Kotlin 1.0.4, Scala 2.11.8.
UPDATE:
demonstrated bug here:
https://github.com/mjburghoffer/scala-kotlin-innerclass-bug/tree/master
Looks like Kotlin generates different number of classes for this case. I compiled Java and Kotlin sources and found that Java has additional file ParentSealedJava$1.class. Bytecode of Parent and Sealed classes in Java and Kotlin pretty the same (i inspect bytecode using javap -c), so i think Scala compiler relies somehow on this missing file in Kotlin.
I am very new to Scala. I am using IntelliJ IDE to run a very simple program in Scala(2.11.7).
My program goes like
class Rational(n:Int,d:Int) {
val oneHalf = new Rational(1,2)
}
I am trying to run it as a Class rather than a object. How could I Run this class in IntelliJ?
Thanks
As #Clashsoft says you can not initialize the class like this. You can do some thing simple like this for testing:
class Rational(n: Int, d: Int) {
def oneHalf: Int =
n * d
}
object MyProgram {
def main(args: Array[String]) {
val rational = new Rational(1, 2)
println(rational.oneHalf)
}
}
It is also possible to use App trait (extends App) then you do not need to have main method:
object MyProgram extends App {
val rational = new Rational(1, 2)
println(rational.oneHalf)
}
All depends on how you want to implement your solution at the end. Regarding different between main and App trait please read more.
Thx to #tzachzohar for following addition:
In Scala, just like in Java, only a static main method (with appropriate argument and return types) can serve as a program's entry point. For convenience, IntelliJ IDEA provides a Scala Worksheet as a way to easily test your code, but that's no magic either - it's a just nice wrapper - behind the scenes, a worksheet has its own main method calling your code.
I was trying to go through the code for nsc(new scala compiler). I am a little confused about Main.scala. It is implemented as follows:
/* NSC -- new Scala compiler
* Copyright 2005-2013 LAMP/EPFL
* #author Martin Odersky
*/
package scala.tools
package nsc
import scala.language.postfixOps
/** The main class for NSC, a compiler for the programming
* language Scala.
*/
class MainClass extends Driver with EvalLoop {
def resident(compiler: Global): Unit = loop { line =>
val command = new CompilerCommand(line split "\\s+" toList, new Settings(scalacError))
compiler.reporter.reset()
new compiler.Run() compile command.files
}
override def newCompiler(): Global = Global(settings, reporter)
override def doCompile(compiler: Global) {
if (settings.resident) resident(compiler)
else super.doCompile(compiler)
}
}
object Main extends MainClass { }
My first question is, how is Main being called by the compiler process? When I call something along the lines of:
scalac [ <options> ] <source files>
Somewhere along the lines the newCompiler and doCompile is being called, can somebody help me with tracing through how this is being called and how the compiler is being initialized?
Any pointers will be much appreciated.
Thanks
MainClass extends Driver which has the main method:
def main(args: Array[String]) {
process(args)
sys.exit(if (reporter.hasErrors) 1 else 0)
}
At the same time, object Main extends MainClass which means that there is a Main.class file that contains a public static void main(String[] args) forwarder method that actually invokes aforementioned non-static method on object Main. See for example this question for more details on how object is compiled in Scala.
This means that scala.tools.nsc.Main can be used as a main class when running the scala compiler (this is hardcoded somewhere in the scalac script).
newCompiler and doCompile are called by process.
I tried to run one example from Programming in Scala but compiler gives me error:
Description Resource Path Location Type
illegal start of statement (no modifiers allowed here) ChecksumAcc.sc /HelloWorld/src line 3 Scala Problem
basically complains about private
import scala.collection.mutable.Map
object ChecksumAcc {
private val cache = Map[String, Int]()
}
I'm using Eclipse for Scala worksheet. Same after updating. I believe it uses 2.9.3 scala compiler. Why doesn't it compile?
Not sure what your actual question is, but the Scala worksheet has some special rules (as indicated by the very clear error message...). One thing you can do if you have to use the worksheet, is to put all your code inside a Worksheet object like this:
object Worksheet {
import scala.collection.mutable.Map
object ChecksumAcc {
private val cache = Map[String, Int]()
}
}
Or alternatively, use Eclipse's "New Scala object..." and use that instead of the worksheet.
To avoid the error message you are seeing, when you are working in a Eclipse Scala work sheet
wrap the Class definition and Companion class (Singleton object) in the same object
object worksheet {
class CheckSumAccumulator {
...
}
object CheckSumAccumulator {
...
}
CheckSumAccumulator.calculate("foobar")
}