티스토리 뷰

대분류 - Portability [PORT]

규칙명 - Do not use 'System.getenv()' [PORT.ENV-1]

- System.getenv() 를 이용해서 환경변수를 읽어오려 하지 말라는 것이다.
- 이유는 아래에도 나오겠지만, 모든 플랫폼에서 환경변수를 지원하지는 않기 때문으로, 실제로 jdk 1.4 에서 deprecated 된 메소드이다.

DESCRIPTION

This rule identifies the use of 'System.getenv ()'. An error is reported for each occurrence.

본 규칙은 'System.getenv()' 메소드를 사용하는 경우를 찾아낸다.

BENEFITS

'System.getenv ()' has been "deprecated" because it is not portable.  This is because not all platforms have environment variables.

모든 플랫폼이 환경변수(Environment variables)를 지원하지는 않기 때문(적어도 HP-UX JVM 에서는 지원하지 않음을 확인했다.)으로 System.getenv() 는 deprecated 된 메소드이다.

DRAWBACKS

Your users may be unfamiliar with how to change Java properties.

단점이라고 해야하나. 어찌됐건 System.getenv() 대신에 System.getProperty()를 사용해야 하는데 이에 따라 Property를 설정해서 사용해야 한다는게 번거로울 수도 있겠다.

EXAMPLE

package examples.rules.port;

public class ENV {
    void method (String name) {
        System.getenv (name);  // VIOLATION
    }
}

System.getenv() 는 deprecated 된, 아마 Windows 에서만 통할만한 메소드.

REPAIR

Use 'System.getProperty ()' and the corresponding 'getTypeName ()' methods of
the "boolean", "integer", and "long" primitive types.  This will retrieve Java
system properties instead of environment variables.

package examples.rules.port;

public class ENVFixed {
    void method (String name) {
        System.getProperty (name);  // FIXED
    }
}

System.getProperty() 로 수정.

REFERENCES

David Flanagan: "Java in a Nutshell".
O'Reilly November, 1999: Third Edition, pp.190-192

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함