Get IntelliJ or SBT to give two names to a package - scala

Please tell me a more describing title :P
I have an SBT project in IntelliJ and its src directory constructed like:
src/main/scala/package1/AAA.scala
src/main/scala/package1/BBB.scala
src/main/scala/package2/CCC.scala
src/main/scala/...
So now these classes are imported like:
import package1.AAA._
But actually I want to import these classes like:
import org.myname.package1.AAA._
At the moment I have two ways; one is to put org.myname to all the class files' package attributes like:
package org.myname.package1
class AAA {...
which works, but doing this manually is time consuming, and makes things complicated.
Another one is to reformat the directories like:
src/main/scala/org/myname/package1/AAA.scala
src/main/scala/org/myname/package1/BBB.scala
src/main/scala/org/myname/package2/CCC.scala
src/main/scala/org/myname/...
which works too, but I'd not like to make the looking of the structure like this.
Is there an automatic way to include all classes into org.myname without changing the structure, with the SBT or IntelliJ features?

Create a new package named org.myname.
Move the package1 inside org.myname
Move package2 inside org.myname
IntellIJ IDEA will fix everything for you.

Related

Package name lookup rules in scala

I came out to scala programmin from Java so it's not clear to me how we should use relative imports in Scala and what is the exact name lookup rules? Suppose I have the following:
pack.age
|
|----MyClass.scala
com.age
|
|---AnotherClass.scala
I need to import MyClass.scala into AnotherClass.scala. Since Scala supports only relative imports I wrote the following:
import _root_.pack.age.MyClass
and it worked fine. But when I tried to delete _root_ from there, there was no compile time error either.
import pack.age.MyClass
works fine as well.
So, what is the package name lookup rules in Scala?
I believe there is an order of operations here. If you had package.age.MyClass within com.age (ie. com.age.package.age.MyClass), as well as package.age.MyClass, the former would be picked up. If you wanted the latter, you would need to use the root syntax.
As there is only one place this class can be picked up from (in root), that is the package picked up.
All imports are relative, so sometimes collisions can appear. For example, if you have package com.org.project.scala then next import scala._ looks up for system package too. _root_ is implicit top package that can be utilised for simulation of absolute path.

Rename references at the same time in Eclipse

I have the following situation:
Consider that I have a class nested in a folder structure like for example /fol/der/exam/ple.java . Of course, the class ple.java contains package or import instuctions referencing to some sources. Now, when I choose Use as a source folder for /fol/der/exam/ple.java then, of course, the references used in package, import or function parameters are not the same anymore. I need to adjust them. Is there an elegant how to do that automatically in eclipse?
So, let´s assume that I have the following line in ple.java:
package fol.der.exam;
and that I have this line also in other files like a.java, b.java, etc. and I want to change the line, let´s say to package der.exam;
How can I do that automatically for all java files in eclipse?
best regards,

Best way to solve imports of file in same package but in different directory with Gatling

I want to clean my code structure and put class/object files in another directories in my gatling project.
If i put all simulation class and utils class in the same directory and same package i do not need an import statement and everything works fine.
Let's say my structure is as follow :
/user-files
----/simulations
--------MySimulation.scala
----/utils
--------Router.scala
I have tried several import or naming configuration to be able to use Router in my Simulation.
Follow package naming as directories structure
Put simulations and utils class in the same package
I have also tried different style of import
//using package
import packagename.Router
//another try
import packagename.Router._
//without package name
import Router._
My attempt to search a solution on scala docs or stack overflow didn't helped me.
This is the error given after executing gatling.bat
not found: value Router
You can't do that this way: there's one single source folder, which is by default /user-files/simulations.
If you want to use folders/packages (which is a good thing), you can have a structure such as:
/user-files
----/simulations
--------MySimulation.scala
--------/utils
------------Router.scala
Then, in Scala, packages and folder hierarchy are not related, BUT it's a good practice to use the same convention as in Java.
So, you would have:
package utils
object Router
then in MySimulation:
import utils.Router

Newbie IDE (IntelliJ IDEA) issue: .class files not all usable

I'm working on a school project right now, and every time I have in the past, I always make a new Project in IntelliJ IDEA. However, this time she gave us some .class files that have methods in them that we can't see (she described what they do so we know how to use them) but need to use, so they have to be in the same folder, obviously.
SIDENOTE: I'm also new to using Macs, which is what I'm on at the moment.
Anyways, I put the .class files in my src folder that I found in the Project8 folder. I just made an array of the Book objects, which was one of the .class files I moved, and now I need to use a method from the other .class file, named BookInventory.class. I put that file in the src folder just like the other, but it won't let me use the only method in that class, which is LoadBooks.
Here is the LoadBooks signature:
public static void LoadBooks(Book[] b)
And here's the description of it that she gave to us:
"For each element in the array, accepts user input for the book, creates the Book object, and stores the object into the array."
So, when I made the array of Book objects, IDEA made an import statement up top all by itself, I didn't type it:
import java.awt.print.Book;
So why does IDEA recognize the Book.class file and allow me to use it in this .java file for my project, but it doesn't seem to notice the BookInventory.class file?
Any help appreciated, thanks ahead of time.
What is happening is when you first typed the line with LoadBooks(Book[] b), IntelliJ could not "see" your class files (you have subsequently loaded them in "class files" and added that as a project library, I presume).
IntelliJ however searched for and found a Book class in the internal java libraries, java.awt.print.Book. Note that this is a different class to the one your teacher gave you, which might have been e.g. edu.myschool.homework.Book.
Firstly, try to delete the line including the import statement, or manually change it to the correct package (your teacher can inform you what it is).
If the same import comes back automatically, you can go into Settings -> Editor -> General -> Auto Import and untick Add unambiguous imports on the fly - this will cause intellij to prompt you before adding imports.
Also, I would ask your teacher to give you the class files in a jar file, since that's the usual approach.
Good luck.

Strange behaviour when importing types in Scala 2.10

Today I cleared my .ivy cache and cleaned my project output targets. Since then I have been getting really strange behaviour when running tests with SBT or editing in the Scala IDE.
Given the following:
package com.abc.rest
import com.abc.utility.IdTLabel
I will get the following error:
object utility is not a member of package com.abc.rest.com.abc
Notice that com.abc is repeated twice, so it appears that the compiler uses the context of the current package when doing the import (maybe it's supposed to do this, but I never noticed it before).
Also, if I try to access classes in package com.abc from anywhere inside com.abc.rest (even using the full path) the compiler will complain that the type can not be found.
It appears that the errors only occur when I try to include files from parent packages. What I do find strange is that my code used to work. It only started happening after I cleaned up my project and my ivy cache, so maybe a later version of the compiler is more strict than the previous one.
I would love some ideas on what I can be doing wrong, or how I can go about troubleshooting this.
Update:
By first importing the parent classes and then defining the current package, the problem goes away:
import com.abc.utility.IdTLabel
import com.abs._
package com.abc.rest {
// Define classes belonging to com.abc.rest here
}
So this works, but I would still love to know why on earth the other way around worked, and then stopped working, and how on earth I can fix it. I had a good look, and could find no packages, objects or traits by the name of com anywhere inside the parent package.
Update relating to Worksheets:
Scala worksheets belonging to the same package share the same scope, which sounds obvious, but wasn't. Worksheets are not sand-boxed - they can see the project, and the project can see them. So all the 'test' object, traits, and classes you create inside the worksheet files, also becomes visible in the rest of the project.
I have so many worksheets that I did not even try to see where the problem came in. I simply moved them all to their own package, and like magic, the problem went away.
So, lesson learned for the day: If you create stuff inside worksheets, it's visible from outside the worksheet.
Anyway, this new found knowledge will come in handy, meaning anything 'interesting' can be build, monitored and tweaked inside the worksheet, while the rest of the project can actually use it. Quite cool actually.
It's still interesting to think how a sbt clean and cleaned up ivy cache managed to highlight the problem that was hidden before, but hey, that's another story....
(At the request of JacobusR, I'm making a proper answer out of my earlier comments).
This can happen if you have defined some class/trait/object inside package com.abc.rest.com. As soon as package com.abc.rest.com exists, and given that you are in package com.abc.rest, com would designate com.abc.rest.com as opposed to _root_.com. Fastest (but non-conclusive) way to check, without even scanning the source files, is to look for any .class files in the "com/abc/rest/com" sub-folder.
In particular you would get this behaviour if any of your files has duplicate package definitions (as in package com.abc.rest; package com.abc.rest; ...). If you have this duplicate package clause somewhere in the same file where you get the error, you wouldn't even see anything fishy with the .class files, as the failure at compiling the file would prevent the generation of .class files for any class definition inside the file.
The final bit of useful information is that as you found out the scala Worksheets are not sandboxed, and what you define in the worksheets affects your project's code (rather than only having the project's code affecting the worksheet). So a duplicate package clause in a worksheet could very well cause the error you got.
If package names conflict, there might be a custom error message for that. See if specifying the full path resolves the issue by starting from __root__. Ex. import __root__.com.foo.bar._