Unable to import ChannelSftp.LsEntry from Jsch when I use it in Scala code, why? - scala

So in my scala class, I had to use the Jsch(JAVA) library to do SFTP work. But for some reason, it is unable to import:
import com.jcraft.jsch.ChannelSftp.LsEntry
Any idea why this would be? LsEntry is a nested class of ChannelSftp.
http://epaul.github.io/jsch-documentation/simple.javadoc/com/jcraft/jsch/ChannelSftp.html
package services.impl
import java.nio.file.Path
import com.jcraft.jsch.ChannelSftp
import com.jcraft.jsch.ChannelSftp.LsEntry
import services.InputService
class InputServiceImpl extends InputService[List[ChannelSftp.LsEntry]] {
}

Figured it out.
In scala, to reference an nested class, you use the following syntax:
ChannelSftp#LsEntry

Related

Can Dart conditional imports use names?

Consider a conditional import statement. The following comes from the Dart language guide:
import 'src/hw_none.dart'
if (dart.library.io) 'src/hw_io.dart'
if (dart.library.html) 'src/hw_html.dart';
Is there a syntax to add a name to these imports? For example, I'd like to say something like the following:
import 'src/hw_none.dart' as my_prefix
if (dart.library.io) 'src/hw_io.dart' as my_prefix
if (dart.library.html) 'src/hw_html.dart' as my_prefix;
Unfortunately, the above doesn't compile. I haven't been able to find a variation that does compile.
Is there a way to name conditionally imported packages?
I've done something like this a few month ago, to implement mobile and web functionalities. Seems like you cannot name each import separately.
import 'package:flutter_fcm_web_example/notification_helper.dart';
import 'firebase_mobile_messaging.dart'
if (dart.library.html) 'firebase_web_messaging.dart' as notifInstance;
abstract class NotificationEncapsulation {
static NotificationHelper get instance =>
notifInstance.FirebaseMessagingHelper();
}

'Router' is imported from both 'package:fluro/src/router.dart' and 'package:flutter/src/widgets/router.dart'

I was using Fluro package for navigating. Fluro and Flutter are using 'Router'. So their classes merged. How could I fix this?
lib/routers/routers.dart:2:1: Error: 'Router' is imported from both 'package:fluro/src/router.dart' and 'package:flutter/src/widgets/router.dart'. import 'package:flutter/material.dart'
I faced with that error when upgraded Flutter to 1.20.0.
Change
import 'package:flutter/material.dart'
with
import 'package:flutter/material.dart' hide Router;
in your navigating class that using fluro. So you could use fluro's Router now.
You can alias the Fluro library with the as keyword:
import 'package:flutter/material.dart'
import 'package:fluro/src/router.dart' as Fluro // Alias to avoid conflicts
Then use the alias for any class, method or symbol in the imported library:
var x = Fluro.Router(...)

Using any() to stubbing before calling .thenReturn failing

My first question in StackOverflow. Tried to search for answer, but I could not find any related to my problem.
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatra.test.scalatest.ScalatraFunSuite
import org.mockito.Mockito.when
import org.mockito.Matchers.any
#RunWith(classOf[JUnitRunner])
#Category(Array(classOf[DatabaseTest]))
class Test extends ScalatraFunSuite {
test("Test") {
when(pre.findProduct(any[Seq[Sone]]))
.thenReturn(Util.createProduct())
when(ft.findProduct(any()))
.thenReturn(Util.createFT())
when(interval.findSones(any()))
.thenReturn(Util.createInterval())
//The rest is code to utilise the above
}
I have the test code above written in Scala and using Mockito. Here is the error I get when I run it:
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Misplaced argument matcher detected here:
-> at org.Test.$anonfun$new$1(Test.scala:23)
You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
when(mock.get(anyInt())).thenReturn(null);
doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
verify(mock).someMethod(contains("foo"))
Also, this error might show up because you use argument matchers with
methods that cannot be mocked. Following methods cannot be
stubbed/verified: final/private/equals()/hashCode(). Mocking methods
declared on non-public parent classes is not supported.
That is, both ft.findProduct(any()) and interval.findSones(any()) get the same error. A temporarily fixed has been to use a fixed integer, that is:
test("Test") {
when(pre.findProduct(any[Seq[Sone]]))
.thenReturn(Util.createProduct())
when(ft.findProduct(1)) //any() => 1
.thenReturn(Util.createFT())
when(interval.findSones(3)) //any() => 3
.thenReturn(Util.createInterval())
//The rest is code to utilise the above
}
NOTE: Also tried to use anyInt(), which gave the same error.
My question is: Is there anything wrong with my code? Hope I was able to clarify my problem.

Scala: How to always make certain utils available to sub packages?

All my code is under package com.company.project. In almost all of my files, I end up importing some common things like import scala.util.{Failure, Try, Success} and import scala.util.control.NonFatal etc. Is it possible to somehow setup a package object in such a way that all these utils are always available to all sub packages in com.company.project.sub (kind of my own project level Predef)?
Simply create a package object with type aliases:
package com.company.project
import scala.util
package object sub {
type Failure = util.Failure
type Try = util.Try
type Success = util.Success
type NonFatal = util.control.NonFatal
}

scala: can't import object from root scope

I Have the following code:
(src/main/scala/coins/coins.scala)
object Main extends App {
def countChange(money: Int, coins: List[Int]): Int = {
[...]
And I'm trying to reference it from a test like this:
(src/test/scala/coins/CoinsSuite.scala)
package coins
import org.scalatest.FunSuite
class CoinsSuite extends FunSuite {
import Main.countChange
test("only onw way to pay $0") {
[...]
And I get the following error:
not found: value Main
[error] import Main.countChange
But on the other hand, from an sbt console it works fine
If I declare any package in the main file, like
package x
object Main extends App {
Console.println("Hello World!")
Then I can correcly issue
import x.Main.countChange
Is there limitation on root package or on singleton objects visibility that I'm not aware of?
-- added
just to complete the answer, a couple of useful links at SO
https://stackoverflow.com/a/2030159/47633
https://stackoverflow.com/a/9822212/47633
https://stackoverflow.com/a/9822227/47633
Java (and Scala according to the same convention) is grumpy about importing things in the unnamed package, which is not the same thing as the root package. Put Main into a package.
See Why is my object not a member of package <root> if it's in a separate source file?