NoSuchMethodErrorJUnit和Hamcrest组合的另一个例子.违规代码:
assertThat(dirReader.document(0).getFields(), hasItem(
new FeatureMatcher<IndexableField, String>(equalTo("Patisnummer"), "Field key", "Field key") {
@Override
protected String featureValueOf(IndexableField actual) {
return actual.name();
} } ));
Run Code Online (Sandbox Code Playgroud)
IndexerTest.java中的注释行152-157 (commit ac72ce)
导致NoSuchMethodError(请参阅http://db.tt/qkkkTE78以获取完整输出):
java.lang.NoSuchMethodError: org.hamcrest.Matcher.describeMismatch(Ljava/lang/Object;Lorg/hamcrest/Description;)V
at org.hamcrest.FeatureMatcher.matchesSafely(FeatureMatcher.java:43)
at org.hamcrest.TypeSafeDiagnosingMatcher.matches(TypeSafeDiagnosingMatcher.java:55)
at org.hamcrest.core.IsCollectionContaining.matchesSafely(IsCollectionContaining.java:25)
at org.hamcrest.core.IsCollectionContaining.matchesSafely(IsCollectionContaining.java:14)
at org.hamcrest.TypeSafeDiagnosingMatcher.matches(TypeSafeDiagnosingMatcher.java:55)
at org.junit.Assert.assertThat(Assert.java:770)
at org.junit.Assert.assertThat(Assert.java:736)
at indexer.IndexerTest.testIndexContainsField(IndexerTest.java:152)
Run Code Online (Sandbox Code Playgroud)
A NoSuchMethodError是由调用非现有方法的(已编译)类引起的.describeMismatchJUnit + Hamcrest组合的特定情况通常是由JUnit中包含的Hamcrest类与Hamcrest库中的那些类的版本之间的不兼容引起的.
聚甲醛包含Hamcrest库1.3,Hamcrest芯1.3,和JUnit 4.11的显式依赖,(按顺序)所建议加勒特厅在答复以获取:在运行测试时,"的NoSuchMethodError org.hamcrest.Matcher.describeMismatch" IntelliJ …
如何使用cpp-netlib实现HTTPS服务器?
以下问题使我无法合并asio SSL和cpp-netlib:
通过asio和cpp-netlib的SSL都使用asio接受器来监听端口(例如80或443),然后使用单独的会话进行实际连接.
我认为对于HTTPS:
但是如何将cpp-netlib的HTTP连接处理程序与接受器分开使用呢?
或者是否从asio SSL连接传递了io_service?如果是这样,哪一个?或者它们都是一样的?
或者是否有完全不同的路线?
我使用.class-operator向泛型类提供有关包含类型的信息.对于非通用包含类型,例如Integer.class,这没有任何问题.但是包含的类型是通用的,例如List<Integer>.class,List.class它会导致关于类转换的编译时错误.
有办法规避错误,但我很好奇这里发生了什么.有人可以解释发生了什么吗?为什么事情就像它们一样?以及解决问题的最佳方法是什么?
以下行演示了此问题:请注意外部泛型类型需要Class<T>作为参数,因此在这种情况下Class<List<Integer>>.
Class<Integer> tInt = Integer.class; // Works as expected.
Class<List> tList = List.class; // Works with warning, but is not
// what i'm looking for.
Class<List<Integer>> tListInt1 = List.class; // Error
Class<List<Integer>> tListInt2 = (Class<List<Integer>>) List.class; // Error
Class<List<?>> tListGeneric = (Class<List<Integer>>) List.class; // Error
Run Code Online (Sandbox Code Playgroud)
下一行有效:
Class<List<Integer>> tListInt3 =
(Class<List<Integer>>) ((Class<Integer>)List.class);
Run Code Online (Sandbox Code Playgroud)
为什么声明tListInt2和tListGeneric给出和错误?为什么upcast然后downcast而tListInt3不会产生错误?有更好的方式申报tListInt3吗?
亲切的问候,
Kasper van den Berg
PS.如果您希望将代码视为需要此类型信息的外部通用容器,请告诉我们.如果需要,我会发布它.
open delegate是没有目标的实例方法的委托.要调用它,您需要提供目标作为其第一个参数.它们是优化代码的一种聪明方法,否则会使用反射并且性能较差.有关开放代表的介绍,请参阅此内容.你在实践中使用它的方法是使用昂贵的反射代码来构建这些开放的委托,但是你可以像一个简单的委托调用一样非常便宜地调用它们.
我正在尝试编写将任意PropertyInfo转换为其setter的委托的代码.到目前为止,我想出了这个:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace Test
{
class TestClass
{
static Action<T, object> MakeSetterDelegate<T>(PropertyInfo property)
{
MethodInfo setMethod = property.GetSetMethod();
if (setMethod != null && setMethod.GetParameters().Length == 1) //skips over nasty index properties
{
//To be able to bind to the delegate we have to create a delegate
//type like: Action<T,actualType> rather than Action<T,object>.
//We use reflection to do that
Type setterGenericType = typeof(Action<,>);
Type delegateType = setterGenericType.MakeGenericType(new Type[] …Run Code Online (Sandbox Code Playgroud) 我喜欢pattern-matching在nullable intie 上使用int?:
int t = 42;
object tobj = t;
if (tobj is int? i)
{
System.Console.WriteLine($"It is a nullable int of value {i}");
}
Run Code Online (Sandbox Code Playgroud)
但是,这会导致以下语法错误:
'i)'标有红色波浪线.
使用旧运算符时表达式编译is:
int t = 42;
object tobj = t;
if (tobj is int?)
{
System.Console.WriteLine($"It is a nullable int");
}
string t = "fourty two";
object tobj = t;
if (tobj is string s)
{
System.Console.WriteLine($@"It is a …Run Code Online (Sandbox Code Playgroud) 如何实现java.util.Comparator根据偏序关系对其元素进行排序?
例如,给定的部分顺序关系一个 ≺ Ç,b ≺ Ç ; a和b的顺序是不确定的.
由于Comparator需要总排序,因此实现命令部分排序未定义但是一致的元素.
以下工作会怎样?
interface Item {
boolean before(Item other);
}
class ItemPartialOrderComperator implements Comparator<Item> {
@Override
public int compare(Item o1, Item o2) {
if(o1.equals(o2)) { // Comparator returns 0 if and only if o1 and o2 are equal;
return 0;
}
if(o1.before(o2)) {
return -1;
}
if(o2.before(o1)) {
return +1;
}
return o1.hashCode() - o2.hashCode(); // Arbitrary order on hashcode
}
}
Run Code Online (Sandbox Code Playgroud)
我想用@Nullable-annotation(来自Java Checker Framework)注释一个完全限定的类名,例如:
class Demo {
private transient @Nullable org.apache.lucene.search.Query cached_results;
// ...
}
Run Code Online (Sandbox Code Playgroud)
但是这会导致错误:
scoping construct cannot be annotated with type-use annotation: @checkers.nullness.quals.Nullable
Run Code Online (Sandbox Code Playgroud)
如何注释完全限定的类名?
我的代码调用了当前未运行的WCF服务。所以我们应该期待EndPointNotFoundException。using语句尝试对Close()导致a的异常连接进行故障处理CommunicationObjectFaultedException。在use块周围的try catch块中捕获了此异常:
class Program
{
static void Main()
{
try
{
using (ChannelFactory<IDummyService> unexistingSvc = new ChannelFactory<IDummyService>(new NetNamedPipeBinding(), "net.pipe://localhost/UnexistingService-" + Guid.NewGuid().ToString()))
{
using (IClientChannel chan = (unexistingSvc.CreateChannel() as IClientChannel))
{
(chan as IDummyService)?.Echo("Hello");
}
}
}
catch (EndpointNotFoundException ex)
{
Console.WriteLine("Expected");
}
catch (CommunicationObjectFaultedException ex)
{
Console.WriteLine("Expected: caused by closing channel that has thrown EndPointNotFoundException");
}
}
}
Run Code Online (Sandbox Code Playgroud)
注意,服务EndPoint使用新的Guid,因此它将永远不会监听服务。
IDummyService 是:
[ServiceContract]
interface IDummyService
{
[OperationContract]
string Echo(string e);
}
Run Code Online (Sandbox Code Playgroud)
这会导致Visual Studio调试器(Visual Studio Professional …
我有一个传输二进制数据的设备.要解释我定义struct的数据与数据格式匹配.该struct有一个StuctLayoutAttribute带LayoutKind.Sequential.这按预期工作,例如:
[StructLayout(LayoutKind.Sequential)]
struct DemoPlain
{
public int x;
public int y;
}
Marshal.OffsetOf<DemoPlain>("x"); // yields 0, as expected
Marshal.OffsetOf<DemoPlain>("y"); // yields 4, as expected
Marshal.SizeOf<DemoPlain>(); // yields 8, as expected
Run Code Online (Sandbox Code Playgroud)
现在我希望处理一个类似于其他结构的结构,所以我尝试了实现接口的结构:
interface IDemo
{
int Product();
}
[StructLayout(LayoutKind.Sequential)]
struct DemoWithInterface: IDemo
{
public int x;
public int y;
public int Product() => x * y;
}
Marshal.OffsetOf<DemoWithInterface>("x").Dump(); // yields 0
Marshal.OffsetOf<DemoWithInterface>("y").Dump(); // yields 4
Marshal.SizeOf<DemoWithInterface>().Dump(); // yields 8
Run Code Online (Sandbox Code Playgroud)
令我惊讶的是,偏移和大小DemoWithInterface保持相同DemoPlain …
c# ×5
java ×3
reflection ×2
.net ×1
algorithm ×1
annotations ×1
boost-asio ×1
c#-7.0 ×1
c++ ×1
casting ×1
cpp-netlib ×1
debugging ×1
delegates ×1
generics ×1
hamcrest ×1
https ×1
inheritance ×1
java-8 ×1
junit ×1
junit4 ×1
maven ×1
nullable ×1
owl ×1
rdf ×1
semantic-web ×1
sorting ×1
vtable ×1