Unable to create parboiled parser - java

I have created a parser class for the parboiled framework according to this simple example:
package my.package;
import org.parboiled.BaseParser;
import org.parboiled.annotations.BuildParseTree;
#BuildParseTree
public class QueryParser extends BaseParser<Object> {
//some rules
}
If I try to create parser as shown in the example
QueryParser parser = Parboiled.createParser(QueryParser.class);
I get an exception at that line:
java.lang.ClassCastException: my.package.QueryParser$$parboiled cannot be cast to org.parboiled.BaseParser
at org.parboiled.Parboiled.createParser(Parboiled.java:56)
...
I'm really not doing anything special that is not done in the example. The only difference is that the parser and and the class calling it are in different projects but I can't imagine why this should matter. The dependencies between the projects (which are Eclipse plugin projects) should be alright.
Can anyone tell what I'm doing wrong or where the mistake could be?

It actually seems to have something to do with the run configurations. I moved all the parboiled relevant code to one project and it works. I think I'll keep it this way because it is better encapsulation anyway.

Related

In cannot be resolved to a type

I am following Prinston's course Algorithms, Part I. As I came from .NET and just started to use Java, I have some issue with a piece of Java code and I can not find any related information. They provide with a code which I can use for reading a text file:
public static void main(String[] args) {
In in = new In(args[0]);
int n = in.readInt();
...
}
Exception thrown:
In cannot be resolved to a type
What is this In? Should I import some package or what should I do?
The whole code and description can be also seen here: http://coursera.cs.princeton.edu/algs4/assignments/collinear.html
You need algs4, provided by Princeton as well. When it's in the classpath, add
import edu.princeton.cs.algs4.In;
If that's the only class, you could use the source of In.java.
But I doubt that's allowed on Coursera: when you submit code, it will probably be compiled on the server with algs4.jar in the classpath, so you should really use that one and not your own code.
In.java seems to be a class that someone at princeton made, probably for that same course: http://algs4.cs.princeton.edu/12oop/In.java.
So, yes, you need to import the correct package and you need a jar with that class in your classpath. Search your course documentation for more hints.

Query about packages

I know that import example.* will import all the classes and interfaces in the package example. So is it necessary to use the * only when there are 2 or more classes in the example package?
Because in my program there was only 1 class, xyz, in the package example, and when I tried to use it in other program by import example.*;, the xyz class was not accessible.
Instead if I used it like - import example.xyz, then only it was accessible in other programs. But the usual import java.util.* and other commands work just fine. So is it because they have multiple classes and interfaces in those packages and only then should the * be used?
edit:package code
package myPackage;
public class abcd
{
public void show()
{
System.out.println("this is from package");
}
}
program
import myPackage.*;
class ghg
{
public static void main(String args[])
{
abcd x=new abcd();
x.show();
}
}
error
ghg.java:7: cannot access abcd
bad class file: .\abcd.class
class file contains wrong class: myPackage.abcd
Please remove or make sure it appears in the correct subdirectory of the classpath
EDIT: So i was keeping ghg.java in bin folder of jdk..I moved them out both package and ghg.java and put them in different directory and it worked.
Those import statements are just an indication for the compiler to know where to lookup classes you are using within your source code.
In that sense, this is only about style. There are no good technical reasons to use one or the other style.
But many people consider using "import x.y.*" to be bad practice; they recommend to have "import x.y.Class1", "import x.y.Class2", ...
Reasoning behind that: you, as the human reader are very much interested in understanding the dependencies into other packages. When the wild-card import is used; you have no mean to understand how many classes are really used. As in: if your class needs 50, 100 imports ... that alone would tell you that something is wrong (because your class has way too many dependencies when it is importing 50, 100 other classes). If wildcards are used, you wouldn't know.
As a consequence, tools such as eclipse will by default "roll out" wildcard imports and turn them into specific class imports instead.
Lets be precise: using "import *" is never required. It just seems convenient at times! And when "import x.y.*" does not work; but "import x.y.z" works; then well, you must have done something wrong!

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.

when i use in hadoop 1.2.1,I want to build a new MyPatitioner from father class HashPatitioner

when I use in hadoop 1.2.1 , I want to build a new class MyPatitioner from father class hashpatitioner,and it (mypatitioner) is ok . but , when I try to use it in main, eclipse find some exception.why?
job.setPartitionerClass(MyPartitioner.class);
Tips:The method setPartitionerClass(Class) in the type Job is not applicable for the arguments (Class).
public class MyPartitioner extends HashPartitioner<Text,IntWritable>{……}
Your question is a bit hard to interpret, but I suspect you are not importing packages consistently. In the hadoop API there are two MapReduce API packages org.apache.hadoop.mapreduce and org.apache.hadoop.mapred.
These two do not mix, and if you for instance have imported the job class as org.apache.hadoop.mapreduce.Job and the HashPartitioner class as org.apache.hadoop.mapred.lib.HashPartitioner you will an issue like it sounds like you are describing.
Make sure that the import statements in your MyPartitioner class use org.apache.hadoop.mapreduce if that is what you use for your Job setup, or vice versa. Don't mix these two packages for the same job.

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

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)

Categories

Resources