软件工程实践者

统计

留言簿

友情链结

阅读排行榜

评论排行榜

2016年8月30日 #

Java 8 Repeatable注解的使用

Java 8之前,同一注解不能在相同的目标元素上多次使用,例如,如下的注解在Java 8之前是不允许的:
public class SampleClass {
    
    @Quality("Security")
    @Quality("Performance")
    @Quality("Readability")
    public void foo(){
        //
    }
}

Java 8引入了Repeatable注解(@Repeatable)可以解决这一问题,但光有可重复的注解定义还不够,还需要它的容器注解,两者一起来实现可重复注解的使用。实例如下:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
@Repeatable (Qualities.class)
public @interface Quality {
    String value();
}
@Target(ElementType.METHOD)
public @interface Qualities {
    Quality[] value();
}

其中,Quality是可重复注解,由@Repeatable注解标明,它的容器注解是Qualities,用于存放所有可重复的Quality(存贮在Quality[]中);同时还要注意可重复注解和它的容器注解的目标元素必须是一样的(这也不言自明)。如此这般,我们最开始的 SampleClass 在Java 8环境下就可以安全使用了。


posted @ 2016-09-06 16:24 软件工程实践者 阅读(797) | 评论 (0)编辑 收藏

Bill Push的单例模式实现

以下单例实现思想来自《Java Design Patterns: A Programmer's Approach》.
该方法利用了Java缺省的Lazy类实例化机制克服了传统单例模式实现中Lazy实例化方式的不足。

public class Singleton {
   private Singleton(){}
   
   public static Singleton getInstance(){
      return Helper.instance;
   }

   static class Helper {
       private static Singleton instance = new Singleton();
   }
}

posted @ 2016-09-03 09:37 软件工程实践者 阅读(208) | 评论 (0)编辑 收藏

Maven error “Failure to transfer…”

以下转自StackOverflow(http://stackoverflow.com/questions/5074063/maven-error-failure-to-transfer),亲测可用。


This worked for me in Windows as well.

  1. Locate the {user}/.m2/repository (Using Juno /Win7 here)
  2. In the Search field in upper right of window, type ".lastupdated". Windows will look through all subfolders for these files in the directory. (I did not look through cache.)
  3. Remove them by Right-click > Delete (I kept all of the lastupdated.properties).
  4. Then go back into Eclipse, Right-click on the project and select Maven > Update Project. I selected to "Force Update of Snapshots/Releases". Click Ok and the dependencies finally resolved correctly.

posted @ 2016-09-02 15:52 软件工程实践者 阅读(159) | 评论 (0)编辑 收藏

Groovy:Invalid duplicate class definition of class....The type xxx is already defined..

当我们写Groovy脚本代码的时候,有时会发生编译错误,如下:

- Groovy:Invalid duplicate class definition of class XXX : The source XXXX\XXX.groovy contains at least two 
definitions of the class XXX.
- The type XXX is already defined

原因在于Groovy会把.groovy代码文件作为脚本或类定义来处理,例如如下代码:

class Order {
    def security
    def value
    private buy_sell(su, closure) {
        security = su[0]
        quantity = su[1]
        closure()
    }
    def getTo() {
        this
    }
}
def methodMissing(String name, args) {
    order.metaClass.getMetaProperty(name).setProperty(order, args)
}
def getNewOrder() {
    order = new Order()
}

Integer.metaClass.getShares = { -> delegate }

Groovy会把上述代码作为脚本处理,同时缺省用文件名来作为一个外围类类包括整个脚本程序,此时,如果该文件名恰好也是Order的话,那么就会出现重复的类定义错误提示。
解决办法是将脚本文件名取另外一个不同的名字。

posted @ 2016-08-31 09:00 软件工程实践者 阅读(983) | 评论 (0)编辑 收藏

OMG OCUP2考试通知

已经申请OCUP中级考试的学员可以在一年内(截止到17年9月份)免费申请OCUP2中级考试的资格(原有考试仍可以参加)。此外,2014年3月份之后参加了原有OCUP中级认证考试的学员可以免费申请OCUP2中级认证考试。详见OMG网站声明(http://www.omg.org/ocup-2/exam-info.htm)。

posted @ 2016-08-30 14:10 软件工程实践者 阅读(268) | 评论 (0)编辑 收藏