Calling .filter() on a stream looses generic type - java-stream

I have the following code. "Protectable" is an interface. My compiler gives the following error: "Incompatible types: Object cannot be converted to Collection"
When I remove the .filter line, everything works. Why does the compiler loose my type here?
Thanks,
Hannes
Collection<Protectable> requiredItems prefs.getConnectedProtectables(fuzDoc)
.stream()
.filter(protectable -> !protectable.itemVisibleForCurrentUser(fuzDoc))
.collect(Collectors.toList());
The variable prefs implements HasConnectedRights which is implemented as follows:
public interface HasConnectedRights {
public Collection<Protectable> getConnectedProtectables(FuzDocument doc);
}
The interface Protectable declares the method itemVisibleForCurrentUser like this:
default public boolean itemVisibleForCurrentUser(Docker<FuzDocument> doc) {
User user = UserCtrl.getCurrentUser(doc.getDoc());
return user == null || itemVisibleFor(user);
}

Related

How do I check whether a generic type is nullable in Dart NNBD?

Let's say I had some function that takes a generic type as an argument. How do I check within that function whether the generic type argument is nullable or not? I want do something like this:
void func<T>() {
print(T is nullable);
}
void main(){
func<int>(); //prints false
func<int?>(); //prints true
}
All I can think of to do is to check if T.toString() ends with a ? which is very hacky.
Try:
bool isNullable<T>() => null is T;
The accepted answer really just checks if the type can be null. It doesn't care about the type that you are operating the null operator on.
If you want to check if a type is a specific nullable type, a.k.a if you want to check if a type is specifically one of type DateTime? and not String?, you can't do this in dart via T == DateTime? as this conflicts with ternary operator syntax.
However, since dart allows passing nullable types into generic arguments, it's possible to it like so:
bool isType<T, Y>() => T == Y;
isType<T, DateTime?>() works.
I have come across this a lot. And #Irn method works, except for when T is type Type (when using generics), it will always return saying that T is not null.
I needed to test the actual type of Type not Type its self.
This is what I have that is working really well for me.
bool get isNullable {
try {
// throws an exception if T is not nullable
final value = null as T;
return true;
} catch (_) {
return false;
}
}
It creates a new List instance to verify if it's type is nullable or not by using the is operator which supports inheritance:
bool isNullable<T>() => <T?>[] is List<T>;

Java compiler error: raw type with method returning Optional

I'm trying to understand the Java compiler's thinking (I know, bad idea)...
Consider this program:
import java.util.Optional;
public class xx {
public static class Foo<T> {
public interface Bar<T> {
int getX();
}
public Optional<Bar<T>> getBar() {
return Optional.empty();
}
}
public static void main(String[] args) throws Exception {
Foo foo = new Foo(); // note raw type
foo.getBar().get().getX();
}
}
The java 1.8.0_112 compiler gives:
xx.java:15: error: cannot find symbol
foo.getBar().get().getX();
^
symbol: method getX()
location: class Object
1 error
The question is: why doesn't the compiler, given the raw type Foo for foo, realize that the return type of foo.getBar() is Optional<? extends Bar> instead of what it apparently thinks, which is Optional<?> ?
Note: I know how to change this program to make it compile, that's not the question.
Once you use raw types in conjunction with type inference, the following from JLS 18.5.2 will apply
If unchecked conversion was necessary for the method to be applicable during constraint set reduction in ยง18.5.1, then [...] the return type and thrown types of the invocation type of m are given by the erasure of the return type and thrown types of m's type.
From this follows, that the return type of foo.getBar() is indeed just Optional with all type arguments erased.
Solution: avoid raw types, always.

Analyzer warns that an argument type can't be itself

The error: The argument type 'AdvformBaseComponent(advform/base.dart)' can't be assigned to the parameter type 'AdvformBaseComponent(advform/base.dart)'.
The analyzer is warning that the argument type can't be AdvformBaseComponent, but the function is expecting that exact class as argument:
void addControl(AdvformBaseComponent baseComponent, String fieldName);
the below snippet is within a ngOnInit of AdvformBaseComponent.
objector.addControl(this, name);
It only hides the warning if I do a "addControl(this as dynamic", but that is so hacky and non performant...
sdk 1.22.1
EDIT:
The AdvformBaseComponent is an abstract class that is implemented by other components. It is essentially a base class for form components.
The AdvformObjectComponent is a panel group of AdvformBaseComponent based components. It is mandatory that any AdvformBaseComponent have a parent of AdvformObjectComponent.
They are in the same project and same directory.
I can share these files if you find it useful, they are browser components anyway.
abstract class AdvformBaseComponent implements OnInit, AfterViewInit,
OnDestroy {
AdvformObjectComponent objector;
AdvformBaseComponent(this.objector, #Optional() this._submitter, this.translator, #Optional() this._group) {
if (objector == null)
throw new Exception(
'Advform inputs must have a advform-object as parent.');
}
}
class AdvformObjectComponent implements OnInit {
void addControl(AdvformBaseComponent baseComponent, String fieldName) {
...
}
}
the error stopped after the 1.23.0

Syntax error when trying to determine if a variable is of a certain type

Pretty much as the title says: If you have a Type stored in a variable, there's no way to compare your actual object to this type variable, as far as I can tell. I can probably accomplish what I'm trying to do with mirrors, but I'd prefer not to if at all possible.
void example() {
Type myType = String;
String myExample = "Example";
//Syntax error here: The name 'myType' is not a type and cannot be used in an 'is' expression
if (myExample is myType) {
}
}
You can't generally test if a value is of a type using the Type object.
Type objects are reflected types, not real types. They represent the real type, but you can't use them in the code where you need a type: as type assertions, as generic type parameters or with the is/as operators. You must use the name of a type in those places, and not the name of a normal variable that happens to hold a Type object.
Clever stuff using mirrors might get there, but it's likely overkill for most cases (and I understand that you don't want it).
What you might be able to do instead, is to not pass around raw Type objects. You could instead make your own type abstraction, something like:
class MyType<T> {
const MyType();
Type get type => T;
bool isA(Object object) => object is T;
}
Then you can use that to represent types, not a Type object, and do something like:
void main(List<String> args) {
MyType myType = const MyType<String>();
String myExample = "Example";
if(myType.isA(myExample)) {
print('is');
} else {
print('is not');
}
}
That does require that your entire program uses your type objects to pass around types, but it also gives you a lot of control over those objects, so you can implement the functionality that you need.
I tried
library x;
void main(List<String> args) {
Type myType = String;
String myExample = "Example";
if(myExample.runtimeType == myType) {
print('is');
} else {
print('is not');
}
}
and it worked.
I have not much experience with such code in Dart though. Maybe that is not a fail-safe approach.
import 'package:reflection/reflection.dart';
void main() {
var childType = typeInfo(Child);
var baseType = typeInfo(Base);
if(childType.isA(baseType)) {
print("Child is Base");
}
if(baseType.isAssignableFrom(childType)) {
print("Base is assignable from Child");
}
}
class Base {
}
class Child extends Base {
}
Child is Base
Base is assignable for Child
P.S.
The "reflection" package incompatible with dart2js. It work only when used in Dart language.

Type cast from Java.Lang.Object to native CLR type in MonoDroid

How to cast Java.Lang.Object to some native type?
Example:
ListView adapter contains instances of native type Message. When i am trying to get SelectedItem from ListView it returns instance of Message type casted to Java.Lang.Object, but I can't find solution to cast Java.Lang.Object back to Message.
var message = (Message)list.SelectedItem;
// throws Error 5 Cannot convert type 'Java.Lang.Object' to 'Message'
Please Help.
After long time debuging, have found the solution:
public static class ObjectTypeHelper
{
public static T Cast<T>(this Java.Lang.Object obj) where T : class
{
var propertyInfo = obj.GetType().GetProperty("Instance");
return propertyInfo == null ? null : propertyInfo.GetValue(obj, null) as T;
}
}
Usage example:
var message = list.GetItemAtPosition(e.Position).Cast<Message>();
bundle.PutInt("Message", message.ID);
After careful sdk study have found MonoDroid integrated extension for this purpose:
public static TResult JavaCast<TResult>(this Android.Runtime.IJavaObject instance)
where TResult : class, Android.Runtime.IJavaObject
Member of Android.Runtime.Extensions
The least magical way of getting a native type from the Spinner is to call
message = ((ArrayAdapter<Message>)list.Adapter).GetItem(list.SelectedItemPosition);
I used this code from above answer and it works fine to me
public static class ObjectTypeHelper
{
public static T Cast<T>(this Java.Lang.Object obj) where T : class
{
var propertyInfo = obj.GetType().GetProperty("Instance");
return propertyInfo == null ? null : propertyInfo.GetValue(obj, null) as T;
}
}
and this is how I used
var selectedLocation = locationSpinner.SelectedItem.Cast<Location>();
I am able to get my location object fine from spinner
For generic collections, the right answer would be to use JavaList, which is a Java.Lang.Object and also implements IList. But it involves more work that's for sure. This is actually just an adapter for Java's ArrayList implementation.
You could always try the JavaCast<> method (most of the views implement this)(not tested):
var message = list.SelectedItem.JavaCast< Message >();
If for some reason GetChildAtPosition is not possible, serialise the object to json string and then deserialise the string back to native class.
All of the above answers are correct but I found the simplest way for my case was to make the object a subclass of Java.Lang.Object.
For example I'm writing a Android app in Monotouch, mimicking the concept of a UITableView in iOS using the ExpandableListAdapter, which requires the equivalent of UITableViewCells, so I subclassed cell objects from Java.Lang.Object allowing me to implement a subclass of ExpandableListAdapter such as
public override Java.Lang.Object GetChild(int position, int childPosition)
Etc.
it's work for me:
public class HolderHelper<T> : Java.Lang.Object {
public readonly T Value;
public HolderHelper (T value)
{
this.Value = value;
}
}
test:
chkFileName.Tag = new HolderHelper<LinkInfo> (item);
LinkInfo link= (chkFileName.Tag as HolderHelper<LinkInfo>).Value;

Resources