generics

Find type parameter of method return type in Java 6 annotation processor

萝らか妹 提交于 2020-01-20 04:07:51
问题 I'm writing a tool that uses the annotation processor to generate source code depending on the return type of methods of an annotated class. The return type is always some subtype (interface or class) of an interface A that defines a type variable T . interface A<T>{T m();}; I would like to find the type parameter for the method m() return value type variable T . The return type is represented by the annotation processor as a javax.lang.model.type.TypeMirror instance. The simplest case is to

Generics and the question mark

扶醉桌前 提交于 2020-01-20 03:07:27
问题 I'd like to use a generic list, but the initialization method only returns a List . The following code works well: List tmpColumnList = aMethodToInitializeTheColumnList(); tmpColumnList.add("ANICELITTLECOLUMN"); Java accuses that I'm using a raw type and I should paramerize the list. So I added the question mark parameterize this list. List<?> tmpColumnList = aMethodToInitializeTheColumnList(); tmpColumnList.add("ANICELITTLECOLUMN"); Problem is: Now the add(..) method doesn't work anymore. I

How to create List of open generic type of class<T>?

梦想的初衷 提交于 2020-01-19 06:24:18
问题 i am working on someone else code i need to add few things i have a class public abstract class Data<T> { } public class StringData : Data<string> { } public class DecimalData : Data<decimal> { } in my program i want to maintain list of different type of data List<Data> dataCollection=new List<Data>(); dataCollection.Add(new DecimalData()); dataCollection.Add(new stringData()); List<Data> dataCollection=new List<Data>(); at above line i am getting compiler error Using the generic type 'Data'

Generic view controller won't work with delegate and extension

半世苍凉 提交于 2020-01-17 10:34:31
问题 I already posted a question but it was not clear about what I want. As @AlainT suggested, I filed a new one. I have a typealias tuple public typealias MyTuple<T> = (key: T, value: String) A protocol: public protocol VCADelegate: class { associatedtype T func didSelectData(_ selectedData: MyTuple<T>) } A view controller (VCA) with a table view class VCA<T>: UIViewController, UITableViewDelegate, UITableViewDataSource { var dataList = [MyTuple<T>]() weak var delegate: VCADelegate? // Error: can

Generic view controller won't work with delegate and extension

喜欢而已 提交于 2020-01-17 10:33:53
问题 I already posted a question but it was not clear about what I want. As @AlainT suggested, I filed a new one. I have a typealias tuple public typealias MyTuple<T> = (key: T, value: String) A protocol: public protocol VCADelegate: class { associatedtype T func didSelectData(_ selectedData: MyTuple<T>) } A view controller (VCA) with a table view class VCA<T>: UIViewController, UITableViewDelegate, UITableViewDataSource { var dataList = [MyTuple<T>]() weak var delegate: VCADelegate? // Error: can

Generic view controller won't work with delegate and extension

北城余情 提交于 2020-01-17 10:32:32
问题 I already posted a question but it was not clear about what I want. As @AlainT suggested, I filed a new one. I have a typealias tuple public typealias MyTuple<T> = (key: T, value: String) A protocol: public protocol VCADelegate: class { associatedtype T func didSelectData(_ selectedData: MyTuple<T>) } A view controller (VCA) with a table view class VCA<T>: UIViewController, UITableViewDelegate, UITableViewDataSource { var dataList = [MyTuple<T>]() weak var delegate: VCADelegate? // Error: can

What does it mean when a generics function has two return types?

风格不统一 提交于 2020-01-17 07:59:06
问题 I am going through a tutorial and I found this particular code. private <V> V fromJson(HttpRequest request, Class<V> target) throws IOException { Reader reader = request.bufferedReader(); try { return GSON.fromJson(reader, target); } catch (JsonParseException e) { throw new JsonException(e); } finally { try { reader.close(); } catch (IOException ignored) { // Ignored } } } I notice that the fromJson function has two return types? I have the basic idea of generics and how it works. What I can

Create generic abstract class using EF database first approach

会有一股神秘感。 提交于 2020-01-17 07:15:10
问题 I have some similar entities and I want to create a generic abstract class for CRUD operations for them, but I don't know how can access to entities in generic class. For example: public abstract class HeaderRepository<T> { public HeaderRepository() { } public virtual byte[] Insert(T item) { using (MyEntities context = new MyEntities()) { context.***** <-- What I should write to add an object to entity T } } } I hope I've got it. 回答1: You need to set your DB set. It would be better to have a

Compiler error when Calling a generic method with no actual argument but with explicit type parameter

独自空忆成欢 提交于 2020-01-17 05:42:05
问题 From the book "Java Generic and Collections", section 1.4 there is this code sniplet class Lists { public static <T> List<T> toList(T... arr) { List<T> list = new ArrayList<T>(); for (T elt : arr) list.add(elt); return list; } } Then there is this statement: When a type parameter is passed to a generic method invocation, it appears in angle brackets to the left, just as in the method declaration. The Java grammar requires that type parameters may appear only in method invocations that use a

java generics - The method compareTo(capture#1-of ?) in the type Comparable<capture#1-of ?> is not applicable for the arguments

只谈情不闲聊 提交于 2020-01-17 01:22:09
问题 So I want to do this: public interface IFieldObject { public Comparable get(); } public interface IFieldCondition { public boolean apply(IFieldObject field, Comparable compare); } public class EqualTo implements IFieldCondition { public boolean apply(IFieldObject field, Comparable compare) { return (field.get().compareTo(compare) == 0); } } but Eclipse gives me warnings: Type safety: The method compareTo(Object) belongs to the raw type Comparable. References to generic type Comparable should