Eclipse: Scala cannot access Java Class members - just after some cleanup - java

Im trying to use Scala and Java in one project. Im working with the Scla IDE for Eclipse. I have two packages in my Scala Project: one for my scala code and one for my java code.
Now lets say I create new JavaClass with one static member.
package javastuff;
public class MyJavaClass {
public static String MESSAGE = "Im Java";
}
After that Im trying to get access to this variale and somehow I cannot. Funny thing, because scala is able to see the Java class "MyJavaClass" just not able to see MESSAGE.
import javastuff.MyJavaClass
object Main {
def main(args: Array[String]) {
println(MyJavaClass.MESSAGE)
}
}
Value MESSAGE is not a member of object javastuff.MyJavaClass
If I use Project/Clean... 1-2x times eclipse is maybe starting realizing that the member MESSAGE is really there and everything is fine. Is this normal? Maybe Im doing something wrong, I know eclipse is really a bad IDE and I should maybe try IntelliJ, but somehow I like eclipse and I would like to use later some of my favorite plugins, thats why I would not change the IDE just because of this problem. Any ideas how to handle this problem better?

Scala doesn't have any static fields. Here is a blogpost about it
btw. public static without final is pretty bad design (no encapsulation => possible memory leaks)

Related

Eclipse gives error for 'undefined' Java function it can easily find

I've been having trouble with Eclipse. I'm working on a java project that's configured with git. After submitting my code through git, Eclipse suddenly generates all these errors, saying many functions are undefined. However all the functions are still there, Eclipse will even take me to them when Ctrl+click the function.
For instance say I have this class:
public class myClass {
public myClass () {}
public void myFunction () {
//do some stuff
}
}
And now I have another class that uses it:
public class secondClass {
public void callFunction () {
myClass a = new myClass();
a.myFunction();
}
}
The myFunction call in secondClass causes an 'undefined' error in Eclipse. I've tried refreshing the project but it doesn't make a difference. The only way I've found to get Eclipse to behave correctly is to comment out the function it can't find, uncomment it, and then save the file. Is there a better way to do this? Or prevent Eclipse from having this problem?
Sounds like something is going wrong with your eclipse.
When this happens I typically do the following. (Continue to the next bullet if previous did not help):
refresh the project. First try F5. If it does not help do it using right click on project and choosing the appropriate option in context menu. It is strange, but sometimes F5 does not work, but menu does.
rebuild the project.
Try to close project and open it again
try to delete project (without removing content) through Eclipse.
try to create project again and copy *.java files there.
try to create new workspace.
I hope that #1 (or probably #2) will help.
Check if you have properly imported myClass in secondClass.
Secondly, your class naming is wrong. Class names generally starts with a capital letter.
From your 'solution' it seems to me the imports have gone missing. Commenting and then uncommenting the method and saving the file caused eclipse to fix your imports.
You have to take a look at the way you commit/push your code through git (and who did the last change), there is probably something wrong in the workflow that causes them to commit a file with missing import statements.

How to create a simple android test case for a utiltiy class

I've got a utility class that I've created:
package com.g2.quizification.utils;
import com.g2.quizification.domain.Question;
public class ParsingUtils {
public static Question parse(String raw) {
Question question = new Question();
//TODO: parse some stuff
return question;
}
}
...that lives here:
I've also followed the tutorials and created a testing app, that looks like this:
And here's my test code, just waiting for some good 'ole TDD:
package com.g2.quizification.utils.test;
import com.g2.quizification.domain.Question;
import com.g2.quizification.utils.ParsingUtils;
public class ParsingUtilsTest {
public void testParse() {
String raw = "Q:Question? A:Answer.";
Question question = ParsingUtils.parse(raw);
//assertEquals("Question?", question.getQuestion());
//assertEquals("Answer.", question.getAnswer());
}
}
The test class is obviously missing the extension, but all the examples seem to only show extending something like ActivityUnitTestCase. I'm not testing an activity; I just want to test a static method in a utility class. Is that possible?
It seems like creating a utility test class should be simple, but I'm not sure what the next step is and/or what I'm missing.
The best approach for test project is to add the test project so that its root directory tests/ is at the same level as the src/ directory of the main application's project. If you are using junit4 and eclipse, you can just right-click on the util class you want to test and choose New -> JUnit Test Case.
Basically I would expect a new test class named ParsingUtilTest under the source folder tests/ and within the package com.g2.quizification.utils.test. The test class should extend TestCase and each method you want to test in that util class should have a new method in the test class with the name preceded with "test". I mean to say, suppose you have a method name in ParsingUtils called parseXml. The test method name in ParsingUtilsTest (which Extend 'TestCase') should be named testParseXml
The test class is obviously missing the extension, but all the examples seem to only show extending something like ActivityUnitTestCase. I'm not testing an activity; I just want to test a static method in a utility class. Is that possible?
Yes, as long as the class your are testing has nothing to do with android apis. And if you do need to test code with android api dependencies, for example, testing a view or an activity, you might want to have a try with robolectric. It's faster than the ones that extend ActivityUnitTestCase.
I have been playing with robolectric a lot (to do TDD on android), and so far, I prefer version 1.1 or 1.2 to 2.x, more stable and run fast.
Besides the tools mentioned above, there are many practices for writing good test cases, naming conventions, code refactoring and such.
It seems like creating a utility test class should be simple, but I'm not sure what the next step is and/or what I'm missing.
Its good to begin with small steps, xUnit Test Patterns: Refactoring Test Code and Extreme Programming Explained are some good books for your reference.

akka with play framework

still learning to master akka java with play framework. I have a code snippet below. It was working fine but has decided to give some headaches.
public class Application extends Controller {
static ActorRef masterActor;
RubineActor rubineactor;
public static Result index() {
return ok(index.render(null));
........ somecode
}
it was working fine but now my eclipse juno complains that it cannot resolve the index object in the return line . I am new to both akka and play framework . Can someone please explain what is happening to me. cos have to submit the project as my final year project. thanks
Your problem is not related to Akka, it's a template concern.
The variable index is provided by a template import, certainly import views.html.*;
Eclipse sometimes cannot resolve this object because it is generated automatically by Play after the first request.
Templates are compiled as standard Scala functions, following a simple naming convention. If you create a views/Application/index.scala.html template file, it will generate a views.html.Application.index class that has a render() method.
See the hello word sample for a concrete exemple.

Netbeans debugger unable debug any project with a particular class name

Some weird behavior in Netbeans 7.0. Ostensibly something went wrong when I created a class, because now no matter what project I am in, if I create a class named "RainbowBall" in a package called "gamesandbox.agents" (even if I just created the package fresh), it compiles fine, but the debugger gives me "Thread main stopped" when I call the RainbowBall constructor.
Stripped down example from a freshly created project:
//RainbowTest.java
package rainbowtest;
import gamesandbox.agents.RainbowBall;
public class RainbowTest
{
public static void main(String[] args)
{
RainbowBall r = new RainbowBall();
System.out.println(r.toString());
}
}
/*---------------*/
//RainbowBall.java
package gamesandbox.agents;
public class RainbowBall
{
public RainbowBall() {};
}
Again, this compiles fine, but the debugger acts like RainbowBall is an unresolvable symbol ("Thread Main Stopped at RainbowTest.java:10").
If I use any other class name (ex. "RainbowBall2") or any other package name I do not get this error. It happens in freshly created projects as well as old ones, and even when no outside libraries/jars/packages are being used in any way.
I'll probably just change the name or try updating to the latest NetBeans, but it would be good to understand what's going on. The IDE has clearly stored the name of the class somewhere permanent and project-agnostic, and is refusing to work with RainbowBalls like some kind of homophobe.
The output message you gave sounds like NetBeans thinks there is a breakpoint in the class. I'm not sure why it would be global to every project, though.

Scala trouble accessing Java methods

So, I have something written in Java, and I want to extend it in Scala... The issue I'm running into is that Scala isn't seeing methods I need.
Here is how it's set up:
Player extends Mob, and Mob extends Entity.
I need to access a method in Player that isn't defined in Mob or Entity, but Scala doesn't think it exists even though Java does.
It can see methods defined by Mob and Entity just fine. Also, all the methods I'm talking about are non-static.
So, am I doing something wrong, or is this a limitation imposed by Scala?
Edit --
Here is the relevant code:
package test
import rsca.gs.model.Player
object Test {
def handle(p:Player): Unit = {
p.getActionSender().sendTeleBubble(0, 0, false);
}
}
Player class:
package rsca.gs.model;
// imports
public final class Player extends Mob {
// Implemented methods (not going to post them, as there are quite a few)
// Relevant code
private MiscPacketBuilder actionSender;
public MiscPacketBuilder getActionSender() {
return actionSender;
}
}
Error:
value getActionSender is not a member of rsca.gs.model.Player
I never encountered such problems, and you probably checked your configuration and everything else twice, so I would guess this is some Eclipse related build issue. You should try to build from the command line in order to see whether Scala or Eclipse is the problem.
Is it possible for you to run a test against the class just to see if you got the right one?
p.getClass.getMethods
... and if possible (may run into NPE) in order to find the source:
p.getClass.getProtectionDomain.getCodeSource.getLocation.getPath
When compiling the Scala class, do something like this:
scalac *.scala *.java
This way, Scala will look a the Java code to see what is available. If, however, the Java code is already compiled and provided as a jar file, just add it to the classpath used when compiling the Scala code.

Categories

Resources