English 中文(简体)
Java and Injecting Defenive Cplica
原标题:Java and Injecting Defensive Copies

因此,我开始真正像以下概念一样:defensive texts,以便使其代码更加“安全”,但不幸的是,这些概念似乎与《禁止杀伤人员地雷公约》所处理的关切的混淆存在内在冲突。

因此,我指的是,在我被介绍参加《全面和平协定》之前,我要把论点与我的所有方法一道人工确认:

public void doSomething(final int p_iAge, final Widget p_oWidget)
        throws IllegalArgumentException
{
    if(p_oWidget == null)
        throw new IllegalArgumentException();

    // ...
}

现在,我利用阿帕奇·巴尼·瓦莱德来验证我的方法,而通过阿普特组织推翻我的任何例外。 这使我的代码变得清醒,把我的商业逻辑(我真心想!)与磨擦分开,流产验证检查,破例扔 throw等等。

但是,现在我开始喜欢使用防御拷贝的概念,我开始采取正确方法,大家再次看到:

@NotNull(message="Widget cannot be null.")
public void doSomething(final int p_iAge, final Widget p_oWidget)
{
    Widget oWidget = new Widget(p_oWidget);

    // ...
}

因此,我的问题是:

www.un.org/Depts/DGACM/index_spanish.htm 使用AOP/IoC(任何框架! ——我绝望!)撰写建议/点,将人工写作防御拷贝的任务分开,并把它们带回当地(方法)物体? 这样,我就可以获得防御性副本的安全利益,并具备AOP/IoC的所有清洁设施。

我认为,我必须撰写建议书,以拦截所有方法,并在某种程度上使用依赖注射来造成这种方法参数的集合/管理情况;这些情况将是防御性的副本。 然后,在我的目标群体中,我可以利用IoC获得这些防御拷贝(豆类)。

根据这一(一般)范式,我想象,这部法典会照此办理:

@NotNull(message="Widget cannot be null.")
public void doSomething(final int p_iAge, final Widget p_oWidget)
{
    // By the time we get here, bean validation has already made sure
    // that p_oWidget isn t null, and the AOP method interceptor has already
    // executed and created the "defensive-widget" bean.
    Widget oWidget = springOrWhateverInjector.getBean("defensive-widget");

    // ...
}

Now, oWidget is a defensive copy of p_oWidget, and I don t have to manually write it. Huge time savings, and my OCD with AOP is satisfied!

但是,我甚至不敢肯定,阿普协议框架,如阿斯特里特J/阿佩普联盟和春天或古冰等国际奥委会框架,甚至会支持这种概念。

I also have an enormous amount of respect for the SO community and would like some general input here - comment/recommendations/etc. Thanks in advance!

最佳回答

AspectJ s around advice lets you supply your own parameters to the intercepted method when using ProceedingJoinPoint#proceed(Object[]).

In the advice, you could check if a given parameter is copyable – in your case, using reflection to find a declared constructor that takes a single parameter of the type of the object, or however else you choose to mark objects that you want to make defensive copies of – and then proceed with the method using the copies instead of the original parameters.

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签