Skip to content
DarkKaiser의 블로그
DarkKaiser의 블로그
  • 개발 관련 자료(노션)
  • Raspberry Pi(노션)
  • WD My Cloud(노션)
  • GitHub
DarkKaiser의 블로그

어노테이션 사용하기

DarkKaiser, 2015년 1월 7일2023년 9월 6일

출처 : http://hiddenviewer.tistory.com/96

Annotation을 실제로 사용하는 예제를 알아보자.

첫번째 예제는 UseCase라는 어노테이션을 정의한다. 이 어노테이션은 id와  description이라는 값을 멤버로 갖으며 각각 기능번호와 기능에 대한설명을 나타낸다.  Password 검사와 관련된 클래스에스는 각 메소드에 UseCase 어노테이션을 사용하여 메서드들이 어떤 유스케이스를 구현하고 있는지를 표시한다.
나중에 모든 유스케이스를 구현하는 모든 메소드들이 잘 구현되었는지 확인하기 위해  UseCaseTracker 를 사용하여 어노테이션 정보를 출력한다. (코드는 Thinking in Java 4E 에 있는 예제코드를 사용하였다. )

1. UseCase 어노테이션 정의

메서드에 사용할 어노테이션이므로 @Target을 ElementType.METHOD를 설정하였고, 런타임 시에 사용되기 때문에 @Rention을 RetentionPolicy.RUNTIME 로 설정하여 class 파일에 어노테이션 정보가 남도록 지정하였다.

package net.atgame.annotation.usecase;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface UseCase {
	public int id();
	public String description() default "no description";
}

2. PasswordUtil 클래스에 정의한  어노테이션을 사용한다.

UseCase 어노테이션은 코드 작성간에는 주석의 역할을 하고,  코드 실행시에는 테스트로서의 역할도 한다.

import java.util.List;

public class PasswordUtils {
	
	@UseCase(id = 47, description = "passwords must contain at least one numeric")
	public boolean validatePassword(String password) {
		return (password.matches("\\w*\\d\\w*"));
	}
	
	@UseCase(id = 48)
	public String encryptPassword(String password) {
		return new StringBuilder(password).reverse().toString();
	}
	
	@UseCase(id = 49, description = "New passwords can't equal perviously used ones")
	public boolean checkForNewPassword(List prevPasswords, String password) {
		return !prevPasswords.contains(password);
	}
}

3.  UseCaseTracker를 사용하여 모든 유스케이스를 구현하는 메소드들이 작성되었는지 확인한다.

아래 코드에서는 리플렉션을 사용하여 유스케이스 아이디 47, 48, 49, 50 을 사용하는 메소드들을 검색하여
그 메서드의 어노테이션 멤버를  출력하고 있다.  이 프로그램 실행을 통해 어떤 유스케이스가 아직 구현되지 않았는지 알 수 있다.

package net.atgame.annotation.usecase;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class UseCaseTracker {
	
	public static void trackUseCases(List useCases, Class cl) {
		for (Method m : cl.getDeclaredMethods()) {
			UseCase uc = m.getAnnotation(UseCase.class);
			if (uc != null) {
				System.out.println("Found Use Case:" + uc.id() + " " + uc.description());
				useCases.remove(new Integer(uc.id()));
			}
		}
		
		for (int i : useCases) {
			System.out.println("Warning: Missing use case~" + i);
		}
	}
	
	public static void main(String[] args) {
		List useCases = new ArrayList();
		Collections.addAll(useCases, 47, 48, 49, 50);
		trackUseCases(useCases, PasswordUtils.class);
	}
}

어노테이션이 어떻게 정의되고 사용되는지 보여주기 위해 위 예제를 사용하였다. 어노테이션(UserCase)을 정의를 했다면, 그 어노테이션을 처리하기 위한 클래스(UseCaseTracker)도 반드시 작성해야함을 눈여겨보자.


이제 좀더 실용적인 두번째 예제를 소개하겠다.  데이터베이스 접속 처리를 하기위해 데이터베이스 종류에 맞는 SQL을 작성하여 DBMS에 전송해야한다.  데이터베이스에 맞는 SQL 작성! 이 상당히 코드를 지저분하게 만들수 있기 때문에 모듈화가 잘 고려되어야 한다. 데이터베이스별 SQL 작성을 안할 수는 없지만  어노테이션을 사용하면, DB 접속에 관한 부분을 클라이언트 프로그래머에게 투명하게 처리할 수 있다. 그 의미에 대해서 알아보자

1. 테이블명을 나타내기 위한 DBTable 어노테이션을 정의한다.

package net.atgame.annotation.database;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface DBTable {
	public String name() default "";
}

2. 테이블 컬럼의 타입을 나타내기 위한 SQLInteger 와 SQLString 어노테이션을 정의한다.

package net.atgame.annotation.database;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SQLInteger {
	String name() default "";
	Constraints constraints() default @Constraints;
}

3. 제약조건을 나타내기 위한 Constraint 어노테이션을 정의한다.

package net.atgame.annotation.database;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SQLString {
	int value() default 0;
	String name() default "";
	Constraints constraints() default @Constraints;
}

4. Memeber 모델 클래스에 정의한 어노테이션들을 사용한다.

package net.atgame.annotation.database;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Constraints {
	boolean primaryKey() default false;
	boolean allowNull() default true;
	boolean unique() default false;
}

5. TableCreator 클래스에서 어노테이션 정보를 사용하여, 데이베이스 생성 SQL를 만들어낸다.

package net.atgame.annotation.database;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class TableCreator {
	
	public static void main(String[] args) throws Exception {
		if (args.length < 1) {
			System.out.println("arguments: annotated classes");
			System.exit(0);
		}
		// 모든 엔티티 클래스를 읽음 
		for (String className : args) {
			Class cl = Class.forName(className);
			// 클래스 어노테이션 가져오기 
			DBTable dbTable = cl.getAnnotation(DBTable.class);
			if (dbTable == null) {
				System.out.println("No DBTable annotations in class " + className);
				continue;
			}
			String tableName = dbTable.name();
			
			if (tableName.length() < 1) {
				tableName = cl.getName().toUpperCase();
			}
			List columnDefs = new ArrayList();
			
			/// 필드목록 조회 
			for (Field field : cl.getDeclaredFields()) {
				String columnName = null;
				Annotation[] anns = field.getDeclaredAnnotations();
				if (anns.length < 1) {
					continue;
				}
				if (anns[0] instanceof SQLInteger)  {
					SQLInteger sInt = (SQLInteger)anns[0];
					if (sInt.name().length() < 1) {
						columnName = field.getName().toUpperCase();
					} else {
						columnName = sInt.name();
					}
					columnDefs.add(columnName + " INT" + getConstraints(sInt.constraints()));
				}
				if (anns[0] instanceof SQLString) {
					SQLString sString = (SQLString)anns[0];
					if (sString.name().length() < 1) {
						columnName = field.getName().toUpperCase();
					} else {
						columnName = sString.name();
					}
					columnDefs.add(columnName + " VARCHAR(" +sString.value() + ")" + getConstraints(sString.constraints()));
				}
				
			}
			StringBuilder createCommand = new StringBuilder("CREATE TABLE " + tableName + " (");
			for (String columnDef : columnDefs) {
				createCommand.append("\n " + columnDef + ",");
			}
			// 마지막 쉼표제거 
			String tableCreate = createCommand.substring(0, createCommand.length()-1) + ");";
			System.out.println("Table Creation SQL for " + className + " is :\n" + tableCreate);
		}
		
	}
	
	private static String getConstraints(Constraints con) {
		String constraints = "";
		if (!con.allowNull()) {
			constraints += " NOT NULL";
		}
		if (con.primaryKey()) {
			constraints += " PRIMARY KEY";
		}
		if (con.unique()) {
			constraints += " UNIQUE";
		}
		return constraints;
	}
}

어노테이션을 읽어 데이터베이스 SQL을 생성하는 부분에 주목하자.
Model 클래스 어노테이션 정보를 읽어서 DB에 해당하는 SQL를 생성하고, 테이블을 생성하게 할 수 있다.
더 나아가 getter 와 setter 메소드를 오버라이드하여 인스턴스에 대한 조작만으로 데이터베이스 CRUD(등록,조회,수정,삭제) 연산을 처리하게 할 수도 있을 것이다. 이렇게 하면 데이터베이스 종류에 따른 번거로운 처리를 클라이언트 프로그래머에게 좀더 투명하게 사용할 수 있다.

이상 어노테이션을 사용하는 예제를 간략히 알아보았다. 어노테이션을 잘 활용하면 간결하고, 투명한 코드를 작성할 수 있다.
하지만 언제나 그렇듯 “잘 활용하면” 이라는 조건이 붙는다.  어떻게 잘 활용할지에 대한 생각은 이제 개인의 몫이다.

Java

글 내비게이션

Previous post
Next post

답글 남기기 응답 취소

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다

최신 글

  • AssertJ 소개testCompile ‘org.assertj:assertj-core:3.6.2’ 2017년 9월 14일
  • 자주 사용되는 Lombok 어노테이션 2017년 9월 14일
  • 유니코드 #3 2017년 9월 14일
  • 유니코드 #2 2017년 9월 14일
  • 유니코드 #1 2017년 9월 14일

최신 댓글

    카테고리

    • 개인 자료 (1)
      • 일기 (1)
    • 주절주절 (7)
    • 프로그래밍 갤러리 (16)
    • 프로그래밍 언어 (186)
      • Java (29)
      • C/C++/VC++ (114)
      • C# (11)
      • Visual Basic (6)
      • 안드로이드 (9)
      • Objective-C (5)
      • JavaScript (4)
      • JSP/Servlet (2)
      • Python (4)
      • 어셈블러 (1)
    • 개발++ (44)
      • Book (11)
        • Joel On Software (10)
      • 프로젝트 관리 (6)
      • Maven (1)
      • 디버깅 (1)
      • DirectX (1)
      • Silverlight (1)
      • RESTful (1)
      • Hacking (1)
      • WDM (4)
      • VoIP (5)
      • 기타 (1)
    • 개발 도구 (15)
      • eclipse (14)
      • Sublime Text (1)
    • 네트워크 (7)
    • 설치 및 배포 (7)
      • InstallShield (2)
      • NSIS (4)
    • 버전 관리 (9)
      • Git (2)
      • CVS (2)
      • Subversion (5)
    • 데이터베이스 (7)
      • Oracle (3)
      • Sybase (2)
      • MS-SQL (2)
    • 단위테스트 (3)
      • JUnit (1)
      • NUnit (2)
    • 버그추적시스템 (2)
      • mantis (2)
    • 운영체제 (7)
      • Windows (5)
      • 리눅스 (2)
    • WAS (3)
      • WebLogic (3)
    • 디자인패턴 (1)
    • 디지털 이미지 프로세싱 (16)

    태그

    Abstract ActiveX AfxParseURL Automation boost devenv.exe event EventLogTraceListener Hover interface IO iTextSharp JAD jar JavaScript Joel Leave MFC Monitor msdev.com MSDN mutable PDF Properties RAW Saturation SHGetFolderPath SHGetKnownFolderPath SQLite STLTask String TextWriterTraceListener URL VI 권한 데이터소스 디컴파일러 문자열 스레드 동기화 스레드 생성 실용주의 프로그래머 자동화 테스팅 파일포맷 프리컴파일

    메타

    • 로그인
    • 엔트리 피드
    • 댓글 피드
    • WordPress.org
    ©2025 DarkKaiser의 블로그 | WordPress Theme by SuperbThemes