小编for*_*two的帖子

为什么这种类型不能替代类型参数?

我正在尝试使用泛型来支持委托对象(装饰器,包装器)的可配置结构.我想构建一个实现目标接口和通用委托接口的委托链.

我有这个大纲:

class Test {
    static interface Delegator<T> {}

    static class DelegatorChain<T extends Delegator<T>> {}

    static interface Foo {}

    static class FooDelegator implements Delegator<Foo>, Foo {}

    public static void main(String[] args) {
        DelegatorChain<FooDelegator> chain = new DelegatorChain<FooDelegator>();
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,在尝试实例化chain变量时,编译器抱怨:

绑定不匹配:类型Test.FooDelegator不是该类型的有界参数<T extends Test.Delegator<T>>的有效替代Test.DelegatorChain<T>

我承认泛型对我来说就像魔术一样,但我可以以某种方式承认FooDelegator不是扩展 Delegator <Foo>的Foo,它只是实现了两个接口.

鉴于我很清楚我想要完成什么,有什么我可以用gertics来修复它,或者我只是更好地忘记了它?

java generics

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

如何在OSGi Bundle中使用Java扩展执行XSLT转换

我们正在将现有代码转换为OSGi环境.在我们的一个(尚未)OSGi包中,我们有代码执行XSLT转换.一段XSLT包含一个java扩展函数,用于创建唯一的数值.Java类也驻留在bundle中.这是样式表的一个片段:

<xsl:template match="m:property">
   <xsl:variable name="uniqueDataStreamName" select="concat(../@id,'/',@name)" />
   <xsl:variable name="uniqueDataStreamId"
       select="java:com.xyz.TransformationUtils.makeDataStreamIdFromUniqueName($uniqueDataStreamName)" />
   <data id="{number($uniqueDataStreamId)}">
   <tag>
      <xsl:value-of select="$uniqueDataStreamName" />
   </tag>
   <current_value>
     <xsl:value-of select="@value" />
  </current_value>
</data>
Run Code Online (Sandbox Code Playgroud)

作为参考,这是如何设置和调用转换:

protected Templates createTemplates(Source xsltSource) {
    try {
        TransformerFactory tf = TransformerFactory.newInstance();
        Templates templates = tf.newTemplates(xsltSource);
        return templates;
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
}


protected byte[] transform(byte[] input) throws TransformerException {
    ByteArrayOutputStream out = new ByteArrayOutputStream(2048);
    templates.newTransformer().transform(
            new StreamSource(new ByteArrayInputStream(input)),
            new StreamResult(out));
    return out.toByteArray();
}
Run Code Online (Sandbox Code Playgroud)

在非OSGi environemnt中运行时,它可以工作.在OSGi框架中运行时,它会失败,因为无法编译样式表,因为找不到类TransformationUtils.我有点明白 - 加载jaxp转换器实现的类加载器在我们的bundle中没有看到扩展类.但是,我很难找到解决方案.我尝试过使用OSGi:使用Xalan和Xerces捆绑无济于事.

我的问题是:如何解决这个问题?它可以?

xslt osgi classloader

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

为什么 Netty 不执行我的计划任务?

使用 Java 8 和 Netty 4.1.1.Final,我原以为下面的测试用例会成功,但它超时了。我不理解 wrt nettys 事件循环和任务调度是怎么回事?

public class SchedulerTest {


CountDownLatch latch;

TimerHandler handler;

static class TimerHandler extends ChannelInboundHandlerAdapter {

    ChannelHandlerContext ctx;

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
        this.ctx = ctx;
    }

    private void timeout(final long ms) {
        ctx.executor().schedule(() -> {
            ctx.fireUserEventTriggered(ms);
        }, ms, TimeUnit.MILLISECONDS);
    }

}

static class TimeoutReactor extends ChannelInboundHandlerAdapter {
    CountDownLatch latch;

    public TimeoutReactor(CountDownLatch latch) {
        super();
        this.latch = latch;
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { …
Run Code Online (Sandbox Code Playgroud)

java scheduler netty

0
推荐指数
1
解决办法
466
查看次数

标签 统计

java ×2

classloader ×1

generics ×1

netty ×1

osgi ×1

scheduler ×1

xslt ×1