Using java in rascal - rascal

I have made a java function genImage(List<String lines) which is located in class vis. I am trying to import it into my rascal code, but it won't work. This is the last of my efforts to import it:
#javaClass{visualization.vis}
java void genImage(list[str] lines);
The error I get:
Cannot link method visualization.vis because: visualization.vis.(io.usethesource.vallang.IValueFactory)
Advice: |http://tutor.rascal-mpl.org/Errors/Static/JavaMethodLink/JavaMethodLink.html%7C

The #javaClass tag must point to a fully qualified classname, including the package and the class. It seems it's the class you are missing, right?

Related

Ambiguous type name in Swift unit test

Recently I spent quite some time to figure it out the issue with my unit test. The error that I saw in my unit test is :
Ambiguous type name 'AssetIdentifier' in 'UIImage'
This is the complete code :
import XCTest
import module
#testable import module
class VoucherOptionsViewControllerTests: XCTestCase {
lazy var result = ""
class VoucherOptionsViewControllerMock: VoucherOptionsViewController {
var result = ""
override func showOverlayView(title: String, message: String, assetIdentifier: UIImage.AssetIdentifier) {
self.result = "lalalala"
}
}
}
AssetIdentifier is an enum in UIImage extension
I tried the suggestion listed in:
Ambiguous type name error
However, it returns me with a different error on the controller:
'AssetIdentifier' is inaccessible due to 'internal' protection level
I've tried to clean and rebuild but it returns the same error.
I think your issue lays in the double import of the module module.
import module
#testable import module
By importing it twice you give the compiler two (identical) versions of every type and function defined in module. That's why it's complaining about the ambiguity of AssetIdentifier, there's two of those and it doesn't know which to pick.
The right thing to do would be to remove import module though.
The #testable import statement allows you to import a module in your test target and to gain access to the types and functions defined internal as well as public.
When you add the #testable attribute to an import statement for a module compiled with testing enabled, you activate the elevated access for that module in that scope. Classes and class members marked as internal or public behave as if they were marked open. Other entities marked as internal act as if they were declared public.
Source.
If you do that you'll see that you won't have ambiguity issues and you won't need to move the UIImage extension in the test target. As #JonReid rightfully pointed out in his comment "Production code should remain in your production target".
Hope this helps.
Although that thread does not give the real answer, it gives great clue about the issue. So the fix that I found is by removing :
#testable import module
I keep the UIImage extension in the test module so the compiler does not complain.

Failed to find spock.lang.Specification

I am trying to use Geb+Spock to do web automation testing, but it keeps telling me :
Caught: java.lang.NoClassDefFoundError: spock/lang/Specification
Caused by: java.lang.ClassNotFoundException: spock.lang.Specification
And I think I have already added the things spock might need ...?
#Grapes([
#Grab('org.gebish:geb-core:1.1.1'),
#Grab('org.seleniumhq.selenium:selenium-chrome-driver:2.42.0'),
#Grab('org.seleniumhq.selenium:selenium-support:2.42.0'),
#Grab('org.gebish:geb-spock:1.1.1')
])
import geb.spock.GebSpec
import spock.lang.*
class GoogleSpec extends GebSpec{
def "Google search"() {
given:
to GooglePage
when:
searchBox.value == "Dogs"
and:
searchButton.click()
then:
at ResultPage
}
}
Well, maybe you also want to add dependencies to
Spock itself: org.spockframework:spock-core:1.0-groovy-2.4
Optionally, if you want to mock classes (in addition to interfaces) in Spock: cglib:cglib-nodep:3.2.4
Optionally, if you feel the urge to mock final classes (please don't, it is evil!) and classes without default constructors (that's fine) in Spock: org.objenesis:objenesis:2.2
I don't know anything about Gradle, but those I use in Maven.
P.S.: Maybe next time you want to use your favourite web search engine to look for sample projects or documentation first. You should find plenty.
Yup - you need spock-core not spock-parent or anything else.
org.spockframework:spock-core:1.1-groovy-2.4-rc-3

Dart Editor: "unused import" errors

For a project, I'm importing a library but I don't use any of the classes in it directly. The goal is to fetch a ClassMirror at runtime to create an instance from it. So I have something like:
import 'controllers.dart';
main() {
ClassMirror controller = getClassFromString(libraryName: 'deck_app', className: 'HomeController');
InstanceMirror instance = controller.newInstance(new Symbol(''), []);
instance.reflectee.sayHey();
}
This gives me an "unused import" error. Idk if this is to be considered a bug. So I'm asking you: do you think this is to be considered as a bug? If not, is there a way I could suppress unused import errors?
What's weird is I thought Dart would tree-shake the source and remove the unused import's code, but it does not. The library is properly imported and available.
The unused import is just a conclusion from the static analyzer. You can ignore it or add a dummy statement to silence the analyzer. This has no effect when you run the application.

javac will not compile enum, ( Windows Sun 1.6 --> OpenJDK 1.6)

package com.scheduler.process;
public class Process {
public enum state {
NOT_SUBMITTED, SUBMITTED, BLOCKED, READY, RUNNING, COMPLETED
}
private state currentState;
public state getCurrentState() {
return currentState;
}
public void setCurrentState(state currentState) {
this.currentState = currentState;
}
}
package com.scheduler.machine;
import com.scheduler.process.Process;
import com.scheduler.process.Process.state;
public class Machine {
com.scheduler.process.Process p = new com.scheduler.process.Process();
state s = state.READY; //fails if I don't also explicitly import Process.state
p.setCurrentState(s); //says I need a declarator id after 's'... this is wrong.
p.setCurrentState(state.READY);
}
Modified the example to try and direct to the issue. I cannot change the state on this code. Eclipse suggests importing Process.state like I had on my previous example, but this doesn't work either. This allows state s = state.READY but the call to p.setCurrentState(s); fails as does p.setCurrentState(state.READY);
Problem continued.... Following Oleg's suggestions I tried more permutations:
package com.scheduler.machine;
import com.scheduler.process.Process;
import com.scheduler.process.Process.*;
public class Machine {
com.scheduler.process.Process p = new com.scheduler.process.Process();
public state s = Process.state.READY;
p.setCurrentState(s);
p.setCurrentState(state.READY);
}
Okay. It's clear now that I'm a candidate for lobotomy.
package com.scheduler.machine;
import com.scheduler.process.Process;
import com.scheduler.process.Process.state;
public class Machine {
public void doStuff(){
com.scheduler.process.Process p = new com.scheduler.process.Process();
state s = state.READY; //fails if I don't also explicitly import Process.state
p.setCurrentState(s); //says I need a declarator id after 's'... this is wrong.
p.setCurrentState(state.READY);
}
}
I needed to have a method in the class--but we're still missing something (probably obvious) here. When I go via the command line and run javac on the Machine class AFTER compiling Process, I still get the following error:
mseil#context:/media/MULTIMEDIA/Scratch/Scratch/src/com/scheduler/machine$ javac Machine.java
Machine.java:3: package com.scheduler.process does not exist
import com.scheduler.process.Process;
^
So I guess the question now becomes, what idiot thing am I missing that is preventing me from compiling this by hand that eclipse is doing for me behind the scene?
======
Problem solved here:
Java generics code compiles in eclipse but not in command line
This has just worked for me:
Download latest Eclipse
Create new project
Create two packages com.scheduler.process and com.scheduler.machine
Create class Process in package com.scheduler.process and class Machine in com.scheduler.machine and copy their contents from your post modifying them to conform to Java language syntax, like this:
Everything compiles right away.
------ to answer the previous version of the question ------
To answer the question as it is right now: you need to either
import com.scheduler.process.Process.status or import com.scheduler.process.Process.* and refer to status as just status
or
import com.scheduler.process.* or import com.scheduler.process.Process and refer to status as Process.status
------ to answer the original version of the question ------
You can't import classes that are not inside some package. You just can't. It is a compile time error to import a type from the unnamed package.
You don't need to import anything if your classes are in the same package, or if all of your classes are packageless.
If Process class was inside some package it would be possible to import just its status inner class: import a.b.c.Process.status would work just fine.
All your Windows/Linux migration issues don't have anything to do with Java and exceptions that you see. import Process.state; will produce exception on any OS because you can't import classes that don't belong to any package.
Eclipse doesn't use the Sun JDK by default. I would assume that you are using Eclipse's built in compiler as Sun's JDK and the OpenJDK are almost identical.
Java code compiles and runs exact the same on Windows and Linux most of the time (unless you use a few of the platform specific operations)
I suspect you are not building the code the same way and when you compile Machine, the Process class has not been compiled.
I suggest you use a standard build system like maven or ant and it will build the same everywhere. Failing that run Eclipse on Linux or just the same .class you use on windows as they don't need to be re-compiled in any case.
BTW: You don't need to import Process.state as it not used and its in the same package (so you wouldn't need to if you did)

Scala error compiling OptionBuilder

I am using Apache commons cli (1.2) for command line parsing.
I have the following in my code:
import org.apache.commons.cli.OptionBuilder
OptionBuilder.withLongOpt("db-host").hasArg.
withDescription("Name of the database host").create('h')
I get the error hasArg is not a member of org.apache.commons.cli.OptionBuilder. It makes no difference if I change .hasArg to .hasArg().
Why?
BTW, Java parses this fine.
import org.apache.commons.cli.OptionBuilder
OptionBuilder.withLongOpt("db-host").hasArg.
withDescription("Name of the database host").create('h')
I get the error hasArg is not a member of org.apache.commons.cli.OptionBuilder. It makes no difference if I change .hasArg to .hasArg().
Why?
Because there is no instance method hasArg in OptionBuilder, only a static method. Since hasArg is a static method, you obviously need to call it on the class, not on an instance of the class.
BTW, Java parses this fine.
I don't understand what this has to do with parsing. Scala parses this just fine, too. Plus, what some completely different programming does or doesn't do with this code is utterly irrelevant, since this is Scala code, not some other language.
You need to do something like this:
import org.apache.commons.cli.OptionBuilder
OptionBuilder.withLongOpt("db-host")
OptionBuilder.hasArg
OptionBuilder.withDescription("Name of the database host")
val optionParser = OptionBuilder.create('h')

Resources