[java] Apache Commons Lang 을 사용하여 객체가 인터페이스를 구현하는지 체크하는 방법

자바에서 객체가 특정 인터페이스를 구현하는지 확인하는 방법은 다양한 방법이 있습니다. 그 중 Apache Commons Lang 라이브러리에서 제공하는 ClassUtils 클래스를 사용하여 객체가 인터페이스를 구현하는지 체크할 수 있습니다.

Apache Commons Lang이란?

Apache Commons Lang은 자바에서 자주 사용되는 유틸리티 클래스를 모아놓은 라이브러리입니다. 여기서는 ClassUtils 클래스를 사용하여 객체의 인터페이스를 체크하는 방법을 알아보겠습니다.

객체가 인터페이스를 구현하는지 체크하는 방법

아래는 Apache Commons Lang을 사용하여 객체가 인터페이스를 구현하는지 체크하는 예제 코드입니다.

import org.apache.commons.lang3.ClassUtils;

public class InterfaceCheckExample {

    public static void main(String[] args) {
        // 인터페이스를 구현한 객체 생성
        MyInterface myObject = new MyObject();

        // 인터페이스 체크
        boolean isImplemented = ClassUtils.isAssignable(myObject.getClass(), MyInterface.class);

        if (isImplemented) {
            System.out.println("객체는 인터페이스를 구현합니다.");
        } else {
            System.out.println("객체는 인터페이스를 구현하지 않습니다.");
        }
    }
}

interface MyInterface {
    // 인터페이스 내용 생략
}

class MyObject implements MyInterface {
    // 객체 내용 생략
}

위 예제에서는 MyInterface라는 인터페이스를 구현한 MyObject라는 객체를 생성하고, ClassUtils.isAssignable 메서드를 사용하여 해당 객체가 인터페이스를 구현하는지 체크합니다.

참고 자료