小编Kas*_*erg的帖子

Hamcrest 1.3和JUnit 4.11的NoSuchMethodError

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)

设置:

  • JUnit 4.11
  • Hamcrest 1.3
  • 使用Maven的surefire插件(版本2.14),它使用其JUnitCoreProvider
  • Java 7(OpenJDK)
  • pom(commit ac72ce)

背景:

A NoSuchMethodError是由调用非现有方法的(已编译)类引起的.describeMismatchJUnit + Hamcrest组合的特定情况通常是由JUnit中包含的Hamcrest类与Hamcrest库中的那些类的版本之间的不兼容引起的.

尝试解决NoSuchMethodError:

junit hamcrest junit4 maven maven-surefire-plugin

42
推荐指数
1
解决办法
3万
查看次数

使用cpp-netlib Https服务器

如何使用cpp-netlib实现HTTPS服务器?

以下问题使我无法合并asio SSL和cpp-netlib:

  • 通过asio和cpp-netlib的SSL都使用asio接受器来监听端口(例如80或443),然后使用单独的会话进行实际连接.

    我认为对于HTTPS:

    1. 您使用asio ssl接受器
    2. 哪个实例化连接
    3. 然后通过此连接执行SSL握手,最后
    4. 让cpp-netlib通过此连接提供HTTP

    但是如何将cpp-netlib的HTTP连接处理程序与接受器分开使用呢?

  • 或者是否从asio SSL连接传递了io_service?如果是这样,哪一个?或者它们都是一样的?

  • 或者是否有完全不同的路线?

c++ https boost-asio cpp-netlib

21
推荐指数
2
解决办法
5329
查看次数

Java将".class"-operator用于泛型类型,例如List,"Class <List <?>>"和"Class <List <Integer >>"

我使用.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)

为什么声明tListInt2tListGeneric给出和错误?为什么upcast然后downcast而tListInt3不会产生错误?有更好的方式申报tListInt3吗?

亲切的问候,
Kasper van den Berg

PS.如果您希望将代码视为需要此类型信息的外部通用容器,请告诉我们.如果需要,我会发布它.

java generics reflection casting

17
推荐指数
1
解决办法
1万
查看次数

为属性setter或getter创建一个高性能的开放委托

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)

c# reflection delegates

15
推荐指数
1
解决办法
4387
查看次数

为什么可以为空的模式匹配会导致语法错误?

我喜欢pattern-matchingnullable 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)

c# nullable pattern-matching c#-7.0

15
推荐指数
2
解决办法
2546
查看次数

什么是.net的优秀RDF库?

我正在寻找一个可以处理RDF和OWL数据的库.

到目前为止,我发现:

  • semweb(我所知道的没有猫头鹰支持)
  • rowlex(更多'浏览器'应用程序)

你的建议:

.net c# rdf semantic-web owl

12
推荐指数
3
解决办法
1万
查看次数

部分有序的比较器

如何实现java.util.Comparator根据偏序关系对其元素进行排序?

例如,给定的部分顺序关系一个Ç,bÇ ; ab的顺序是不确定的.

由于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)
  • 这个比较器的订购是否具有传递性? …

java sorting algorithm

12
推荐指数
2
解决办法
3116
查看次数

Java作用域结构不能使用类型使用进行注释

我想用@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)

如何注释完全限定的类名?

java annotations java-8 checker-framework

9
推荐指数
1
解决办法
585
查看次数

Visual Studio打破了使用未处理的异常对话框处理的异常

我的代码调用了当前未运行的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 …

c# debugging unhandled-exception visual-studio

5
推荐指数
1
解决办法
393
查看次数

使用[StructLayout(LayoutKind.Sequential)]进行解组时,C#在哪里存储结构的vtable?

我有一个传输二进制数据的设备.要解释我定义struct的数据与数据格式匹配.该struct有一个StuctLayoutAttributeLayoutKind.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# inheritance vtable unmarshalling

5
推荐指数
1
解决办法
276
查看次数