使用Unsafe.getUnsafe()抛出异常throwException(Throwing an Exception using Unsafe.getUnsafe().throwException)
我在java.lang.Class#newInstance0中遇到了一些有趣的代码:
// Run constructor try { return tmpConstructor.newInstance((Object[])null); } catch (InvocationTargetException e) { Unsafe.getUnsafe().throwException(e.getTargetException()); // Not reached return null; }
查看
Unsafe.getUnsafe().throwException
语句。 它看起来像一个检查的异常是从一个没有声明它被抛出的方法抛出的!他们为什么这么做?
如果Sun开发人员可以使用这种技术,那么我们也可以这么做吗?I came across some interesting code in java.lang.Class#newInstance0:
// Run constructor try { return tmpConstructor.newInstance((Object[])null); } catch (InvocationTargetException e) { Unsafe.getUnsafe().throwException(e.getTargetException()); // Not reached return null; }
Check out the
Unsafe.getUnsafe().throwException
statement. It looks like a checked exception is being thrown from a method which doesn't declare it to be thrown!Why did they do this?
If the Sun developers can use this technique, is it ok for us to do so as well?
原文:https://stackoverflow.com/questions/4375828
最满意答案
连接你的VieWModel给你在测试中嘲笑它们的好处,连接你的Views看起来确实有点矫枉过正。 你不会交换你的观点和UI测试可以在你的ViewModel的模拟上完成,所以你不会真的需要接口他们我认为。
Interfacing your VieWModels gives you the benefit of mocking them in a test, interfacing your Views looks like some overkill indeed. You won't interchange your views and UI testing can be done on mocks of your ViewModel so you won't really need to interface them I think.
相关问答
更多-
关于查看模型和接口(About view models and interfaces)[2023-11-13]
连接你的VieWModel给你在测试中嘲笑它们的好处,连接你的Views看起来确实有点矫枉过正。 你不会交换你的观点和UI测试可以在你的ViewModel的模拟上完成,所以你不会真的需要接口他们我认为。 Interfacing your VieWModels gives you the benefit of mocking them in a test, interfacing your Views looks like some overkill indeed. You won't interchange ... -
我认为这更像是一个Python问题。 你当然可以看看Django抽象基类,但我建议你做更多的pythonic事情,不要担心尝试强制执行这些类接口。 要么你实现了这个方法,要么你没有实现,让调用代码处理它。 I think this is more of a Python question. You can certainly look at Django abstract base classes but I recommend you do the more pythonic thing and not ...
-
这不是一件容易的事。 据我所知,XJC内部没有真正的“指南”。 我不得不边做边学。 您可能希望查看一些现有的XJC插件,以了解它们如何完成任务。 我也不太清楚你为什么要“删除现有的字段并将它们与其他类型一起添加”。 对我来说,这似乎与你的任务不一致。 我将在三个任务中分解此任务: 在指定的包中生成不可变的接口。 自动实现指定包的接口。 生成不可变类。 在构建中,我将定义3个执行: 生成不可变接口(使用1)。 生成可变类(正常XJC + 2)。 生成不可变类(3 + 2)。 immutable-xjc可能已经 ...
-
它应该是 @model [Full Path of Your Model] 所以在你的情况下它可能是: @model [SolutionName].[ModelDirectoryIfAny].[Model] 编辑 @{ ViewBag.Title = "Home Page"; } --------
Top Manufacturers
-
富域模型的接口(Interfaces for Rich Domain Models)[2023-05-31]
通常情况下,领域模型是你应该与其他事物保持隔离的部分。 域模型可以使用接口,以便与外部系统隔离等。 但是,在最常见的情况下,域模型就是您想要防止外部系统,UI逻辑等恶化的影响 - 而不是相反。 因此,将领域模型上的接口没有什么理由。 Usually, the Domain Model is the part that you should keep isolated from everything else. The Domain Model may use interfaces so that it's ... -
在Java中,使用复杂模型的接口会有性能提升吗?(In Java, is there a performance gain in using interfaces for complex models?)[2023-07-26]
使用接口与性能无关(除了开发团队的性能,即开发速度)。 更重要的是保持依赖性得到控制,并将程序的无关部分分开。 如果您直接依赖于代码中的具体类C,则该代码更难以进行单元测试等等。 如果你依赖于一个接口,那么在单元测试中创建一个模拟实现是很简单的。 当然,你可能不需要将你的类的所有方法都放到父接口中。 事实上,你可能不需要一个单一的父接口 。 分析这个类的用法(尤其是像你这样的大类)的机会是,你发现两个或更多不同的方法组,被不同的客户端使用(例如,一组客户端只查询对象状态,而另一组客户端则更新它)。 这使得创 ... -
MVVM是一种将UI与代码分离的模式。 它不是仅用这三种类(视图,视图模型,模型)构建整个应用程序。 您仍然可以拥有工厂,引导程序,存储库等。 根据框架,第一个视图的创建方式不同。 例如在Caliburn.Micro第一个视图是在类Bootstrapper使用DisplayViewFor
通常有一个内置类负责查找具有特定名称(Views)的文件夹,该文件夹提取Views并将其与ViewModels匹配。 视图和模型按名称连接。 您必须遵循某种命名约定: Class1View连接 ... -
模型/查看不同的文件(Models/View in Different Files)[2022-03-22]
您正准备在命名空间准备好之前使用它。 两种选择。 首先,首先使用'app.js',然后取出初始化代码并将其放入最后加载的'bootstrap.js'中。 第二个选项,以及我通常会使用的选项,如果它们尚未存在,则在文件中定义您需要的命名空间。 例如 var app = app || {}; app.collection = app.collection || {}; 基本上,代码是逐个加载的。 当你说namespace.subspace时,代码期望命名空间已被定义为某种东西 - 通常是我见过的大多数情况下的 ... -
接口问题(Trouble with interfaces)[2023-11-26]
在您的位置类中,您需要使用ICoordinates类型的ICoordinates ,而不是Coordinates : class Location : ILocation { public ICoordinates Coordinate1 { get; set; } public ICoordinates Coordinate2 { get; set; } public Location(ICoordinates c1, ICoordinates c2) { ... -
Aurelia鼓励使用MVVM(Model-View-ViewModel)设计模式。 products.html是您的视图 products.ts是您的ViewModel product.d.ts是您的模型,可以包含/使用多个ViewModel 我更喜欢call product-model.ts,但这个名字和你在一起。 通常需要创建一个服务类(例如:product-service.js)来向服务器发出请求,有时还需要一些业务逻辑,在这种情况下,您可以在此服务类中包含模型。 (在TypeScript中,它常见 ...