注释或一些自动代码生成用于设置默认值以避免Android / Java中的NullPointerExceptions?(annotation or some automated code generation for setting default values to avoid NullPointerExceptions in Android/Java?)
我想知道在Android中是否有任何设计模式,第三部分库或任何处理null的Annotation处理器?
例如,假设您使用
Gson
解析JSON Response
并将响应字符串直接映射到带有Gson的Object named "Person"
的Object named "Person"
。Now assume Person has 3 fields 1) Name 2) Age 3) Gender
并且您将要在UI上显示它们,我的问题是如何
NULL pointer Exception without checking for Nulls for each field when displaying this data.
避免使用NULL pointer Exception without checking for Nulls for each field when displaying this data.
是否有注释处理器或允许您提供空替换值的任何库? 像这样的东西
@defVal("N/A") private String name; @defVal(0) private int age; @defVal("N/A") private String gender;
所以我们可以避免所有这些字段的空检查语句,并且在调用getter时它提供值,如果存在则为null,然后提供默认值。 有什么建议么?
编辑:我知道处理Null Pointer崩溃的两种方法
1) to use try catch 2) to use Getters and inside every getter i should check the null value and sets a default value if there is null
但我的问题是有没有办法减少这些代码行并使用一些自动生成的东西? 我相信某种注释
I want to know if in Android there is any design pattern,third part library or any Annotation processors that handles null?
For example, let's say you parse a
JSON Response
usingGson
and maps the response string directly to anObject named "Person"
with Gson.Now assume Person has 3 fields 1) Name 2) Age 3) Gender
and you are about to display them on UI, my Question is how I can avoid a
NULL pointer Exception without checking for Nulls for each field when displaying this data.
Is there an Annotation Processor or any library that allows you to provide null replacement values? Something like this
@defVal("N/A") private String name; @defVal(0) private int age; @defVal("N/A") private String gender;
so we can avoid null check statements for all these fields and when calling there getters it provides the value if exists if there is null then provides default values. Any Suggestions?
EDIT: I know the two ways for handling Null Pointer crash
1) to use try catch 2) to use Getters and inside every getter i should check the null value and sets a default value if there is null
But My Question is is there a way to reduce these lines of code and use some auto generation thing? Some kind of Annotations I believe
原文:https://stackoverflow.com/questions/36334365
最满意答案
托管服务时,您无法使用开放式泛型。 您必须指定具体类型来托管具体服务。 如果要为更多通用参数托管服务,则必须为每个参数类型创建一个新主机,并为每个参数公开一个具有唯一地址的端点。 托管服务后,它必须能够说出它接受的类型,并在服务描述(WSDL源代码)中描述它们。
WCF在您托管服务的基础上工作,该服务必须能够描述它接受的消息 - 因为它生成服务描述。 服务能够根据描述序列化消息。 任何平台上的任何客户端都可以使用该描述并向服务发送正确的消息 - 客户端将从描述中了解消息的允许内容,并且因为该服务主机必须使用具体类型 - 而不是T.
您可以通过将基类型指定为泛型参数来克服此问题,但即使在此之后,您的服务必须知道可以使用的所有派生类型而不是基类型(有多种技术可用于此但它们都不提供随机类型)。
编辑:
从技术上讲,您要求的方法是:将类型的名称从客户端发送到主服务。 主服务将检查具有该类型的服务是否已存在。 如果是,它将把服务的URL发送回客户端。 如果不是,它将通过反射创建服务并启动它。 它将存储有关新托管服务的信息,并将URL发送回客户端。
另一种变体是预先启动所有服务并具有单个WCF路由服务,该服务将请求路由到正确的服务。
这样的解决方案看起来像维护噩梦,第一个解决方案将会有更糟糕的性能,因为每个操作都需要两次网络呼叫。 此外,它通常不具有互操作性,因为您的客户必须预先获得通用合同才能调用此类服务。 根本不这样做 - 定义有限的已使用类集,并使用单一服务使用
KnownType
或DataContractResolver
。When you host the service you cannot use open generics. You must specify concrete type to host concrete service. If you want to host services for more generic arguments you must indeed create a new host for each argument type and expose an endpoint with an unique address for each of them. Once the service is hosted it must be able to say what types it accepts and describe them in the service description (source for WSDL).
WCF works on basis where you host the service which must be able to describe messages it accepts - because of that it generates service description. Service is able to serialize messages according to the description. Any client on any platform can use the description and send the correct message to the service - client will know about allowed content of the message from the description and because of that service host must use the concrete type - not T.
You can overcome this by specifying base type as generic argument but even after that your service must know about all derived types which can be used instead of the base type (there are multiple techniques for this but none of them offer random type).
Edit:
Technically what you ask for means: send the name of the type from the client to the main service. The main service will check if the service with the type already exists. If yes it will send the url of the service back to the client. If not it will creates the service via reflection and starts it. It will store information about new hosted service and sends the url back to the client.
Another variant is starting all services upfront and having single WCF Routing service which will route request to correct service.
Such solutions look like maintenance nightmare and the first one will have much worse performance because two network calls will be needed for each operation. Also it is not generally interoperable because your clients will have to get generic contract upfront to be able to call such services. Simply don't do it - define finite set of used classes and use
KnownType
orDataContractResolver
with single service.
相关问答
更多-
查询实现通用接口的类的泛型类型定义(Query the generic type definition of a class implementing a generic interface)[2022-05-30]
现在我输入了一些问题并且“yahoogled”了一些事情,我发现我可以使用泛型方法然后只查询typeof(T) void Trigger(IEvent event) where T:IMonitor foreach (var monitor in monitors.Where(m=> m.GetType() == typeof(T))) { monitor.Notify(event); } 简单! 以防有人有类似的问题...... Now that I typed the ... -
在C#中是否有任何方法可以使用通用类型,它始终是接口中的实现类型? 不会。到目前为止给出的答案不能满足这一要求,原因有两个: 您始终可以使用不同的T实现接口 interface IFoo
{ T GetOtherThis(); } public class NotAString : Foo { string GetOtherThis() { ... } } 这可以通过约束在某处修复: interface IFoo where T : IFoo 但仍然没有停 ... -
通用接口的一个例子是IEnumerable
。 它代表你可以枚举的一些集合。 集合中的项目类型与接口无关,因此它允许您使用通用参数来指定它。 你可以像这样创建一个类: class Foo where T : IEnumerable { } 这样,通用参数T只能是类型E的集合。约束接口也是通用的。 你也可以这样做: class Foo where T : IEnumerable { } 在这种情况下,您不允许任何类型的集合,只允许字符串集合。 你可以用这个很疯 ... -
与任何其他参数(方法或通用参数)一样,通用接口类型参数需要参数 。 没有参数的参数根本不是参数。 也许C#vNext可以实现类似默认通用参数值的东西: public class A
where T : default(string) { ... } ......或者谁知道什么。 但是,就目前而言,泛型参数需要一个参数 。 而且,毕竟,这不适合你在你的情况下寻找的东西 - 自动魔法传播的通用参数...... - 。 As any other parameter, either method or ge ... -
问题是,如果你从implementations拿出某些东西,我们知道它是IGenericInterface
类型。 这意味着我们应该可以调用GiveT并在传递TakeItBack时获得WeirdType和WeirdType 。 特别是这意味着以下两个方面必须工作: impl.TakeItBack(new WeirdType1()); impl.TakeItBack(new WeirdType2()); 现在如果impl的类型是NonGenericImplementation1那么第二个 ... -
托管实现通用接口的C#泛型类的WCF服务(WCF service hosting a C# generic class implementing generic Interface)[2022-12-16]
托管服务时,您无法使用开放式泛型。 您必须指定具体类型来托管具体服务。 如果要为更多通用参数托管服务,则必须为每个参数类型创建一个新主机,并为每个参数公开一个具有唯一地址的端点。 托管服务后,它必须能够说出它接受的类型,并在服务描述(WSDL源代码)中描述它们。 WCF在您托管服务的基础上工作,该服务必须能够描述它接受的消息 - 因为它生成服务描述。 服务能够根据描述序列化消息。 任何平台上的任何客户端都可以使用该描述并向服务发送正确的消息 - 客户端将从描述中了解消息的允许内容,并且因为该服务主机必须使用 ... -
具有接口约束的泛型类与类实现接口(Generic class with an interface constraint vs class implementing interface)[2022-09-25]
我认为对泛型论点施加限制是没有问题的。 有一个通用的论点并不意味着“这将适用于任何事情”,这意味着代码有多种意义。 它可能实际上暴露了一个完全通用的概念,如List,但它可能会暴露一个仅在某些情况下才有意义的概念(如Nullable 只对非可空实体有意义) 这些约束就是你用来告诉世界在什么情况下这个类将会有意义的机制,并且将使你能够以合理的方式实际使用那个(约束的)参数,即对实现IDisposable事情调用Dispose 这种情况的极端是上下文受到很大限制时,即如果只有两种可能的实现方式呢? ... -
这称为接口的显式实现。 如果您不(或不能)将成员作为具体类型的实例成员公开,而只是通过接口使它们可用,则可以执行此操作。 编译器抱怨的原因: CS0111类型'FloatInputSocketValues'已经定义了一个名为'GetValue'的成员,它具有相同的参数类型 是你的两个成员只能通过他们的返回类型区分,如果你把方法公之于众,你会有: //If I make this method public it won't compile. Musn't all interfaces be public? ...
-
在T上设置泛型类型约束。 public class BindingProperty
: IComparable where T : IComparable { public T Value {get; set;} public int CompareTo(object obj) { if (obj == null) return 1; var other = obj as BindingProperty ; if (other ... -
如果通过接口访问Items ,则可以为其分配一个不是LinkedList
的集合,这将导致错误。 由于Items也有一个setter,它不能是IEnumerable ; 但是,你可以像这样实现它 private LinkedList _items; public IEnumerable Items { get { return _items; } set { var linkedList = value as LinkedList ; ...