누가 이 메서드를 호출 했는지 찾고 싶을 때


@CallerSensitive
public static void callerTestJDK7() {
Class clazz= Reflection.getCallerClass();
System.out.println("class: "+clazz);
}


java 1.7까지는 이렇게 썼으나

java 1.8부터는 안된다.

@CallerSensitive 붙이라는 에러만 뱉는다.

deprecated됐다는 말도 없다.

해당 native에 대한 권한이 삭제 된 것 같다.


java 1.8부터는 이렇게 하면 된다.


public static void callerTestJDK8() {

        new SecurityManager() {
            {
                Class<?>[] classes = getClassContext();
                for(Class<?> clazz : classes) {

                    String name = clazz.getName();
                    System.out.println("name: "+name);
                }
            }
        };

    }


보안쪽이 다 SecurityManager로 옮겨 갔나 보다.

그런데 어렵다. SecurityManager 객체를 생성하고 getClassContext를 부르면 될 것 같은데 protected다.

클래스 재정의해서 내부 블럭을 이용하면 같은 패키지처럼 이용할 수 있다.

그런데 이런 식으로 부르는게 맞는 지 모르겠다.

jvm이 실제로 class를 어떤 식으로 실행하는 지 학인 할 수 있다.

자바소스를 java bytecode라는 것으로 보면 된다.

javap -c 클래스이름

을 실행하면 java bytecode가 나온다.

어셈블리어에 좀 더 가까운 코드를 볼 수 있다.


실무에서는 이런 식으로 확인하는 건 상당히 귀찮은 일이므로 ide에 플러그인을 깔면 편하다.

이클립스 같은 경우 현재 market place에서 설치하는 건 버전 호환때문에 에러 나는 분들이 많을 것이다.

install new software 를 이용해서 

http://andrei.gmxhome.de/eclipse/

주소를 적으면

여러 플러그인이 나오는데 이클립스 버전에 맞는 Bytecode Outline을 찾아서 설치하면 된다.

show view에서 Bytecode를 찾아서 창을 열면 

바로바로 전환되는 bytecode를 볼 수 있다.


속도 향상에 관심이 있다면 이걸로 코드를 비교해보면 좋다.


'Java' 카테고리의 다른 글

java concurrent와 stream(동시성 문제)  (0) 2018.04.01
Java Applet ReflectPermission  (0) 2018.03.28
Java Applet 이런 젠장  (0) 2018.03.28
BigDecimal을 사용하는 이유  (0) 2018.03.18
autoclose resource : try-with-resource  (0) 2018.03.09

Applet 실행하는데 보안상 위험한 곳에 접근 하려고 한다고 경로를 막 뱉으면서 버벅인다.


그러더니 에러가 발생


jackson으로 json컨버트 하는데서 에러가 발생했다.

access denied("java.lang.reflect.ReflectPermission" )


reflection이 파일로 접근하는 건줄 몰랐다.

reflection으로 함수형 프로그램을 흉내내도 실제 함수형 프로그램과는 멀다는 이야기네...


https://docs.oracle.com/javase/8/docs/technotes/guides/security/permissions.html

'Java' 카테고리의 다른 글

java concurrent와 stream(동시성 문제)  (0) 2018.04.01
java bytecode  (0) 2018.04.01
Java Applet 이런 젠장  (0) 2018.03.28
BigDecimal을 사용하는 이유  (0) 2018.03.18
autoclose resource : try-with-resource  (0) 2018.03.09

Java Applet은 웹표준에서 사용 금지가 됐고

다들 쓰지 말라고 한다.


그런데 뭐 어떻하나

디바이스랑 연결 된 프로그램들이 applet으로 쓰게 되있다


앞으로 쓸일이 별로 없겠지만


역사 공부한다고 생각하고 하는 수 밖에...

'Java' 카테고리의 다른 글

java bytecode  (0) 2018.04.01
Java Applet ReflectPermission  (0) 2018.03.28
BigDecimal을 사용하는 이유  (0) 2018.03.18
autoclose resource : try-with-resource  (0) 2018.03.09
자바 자체 인증키 발급  (0) 2018.01.02

table cell 안에 가로 숫자 바를 넣고 싶다.

div 안의 div의 넓이를 조절해서 바를 표현한다.

여기까지는 아무런 문제가 없다.

그런데 바를 우측 정렬을 하고 싶다면 문제가 발생한다.


<!doctype html>
<html>
<head>
<style>
table {
width : 20rem;
border-collapse: collapse;
}
td {
border : 1px solid black;
height : 1.3rem;
text-align : center;
}

td:nth-of-type(1) {
width : 33.3%;
}
td:nth-of-type(2) {
width : 33.3%;
}
td:nth-of-type(3) {
width : 33.3%;
}

.depthBar {
text-align : left;
padding : 3px 5px;
}
.depthBar > div {
display : inline-block;
line-height: 1rem;
width : 2%;
background-color: blue;
overflow: visible;
}

</style>
</head>
<body>
<table>
<tr>
<td>1</td>
<td>2</td>
<td><div class="depthBar"><div>12345</div></div></td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td><div class="depthBar"><div>1234</div></div></td>
</tr>
</table>
</body>
</html>


우측정렬이라서 글자를 우측에서 우측부터 찍을 줄 알았다.

그러나 글자는 우측 끝을 맞추려고 시도할 뿐

좌측부터 찍힌다.

즉 공간이 충분하지 않으면 좌측으로 튀어나가는 것이 아니라 우측으로 튀어나간다.

어찌보면 당연한 것 같기도 하지만

우측정렬을 했는데 글자가 우측으로 튀어나가길 바라는 사람이 있을까?


바안에 글자를 넣어서 해결하려는 게으름이 애초부터 잘못된 개념이라고 생각할 수도 있다.

하지만 이런 게으른 발상이 코드양을 줄려준다면 좋은 것 아닌가?


물론 이 문제는 다른 방향으로 좀 더 설정을 많이 해주면 해결 된다.




뭔가를 구현하고 객체를 얻어 올 때

templete method와 factory mehtod 두 용어가 같이 나올 때가 많다.

그래서 뭐가 다른지 혼란 스럽다.


잘 설명된 말을 빌려온다.

Templete Mehthod (https://en.wikipedia.org/wiki/Template_method_pattern) 

In software engineering, the template method pattern is a behavioral design pattern that defines the program skeleton of an algorithm in an operation, deferring some steps to subclasses.

처리 단계의 뼈대를 만드는 것


Factory Mehthod (https://en.wikipedia.org/wiki/Factory_method_pattern) 

In class-based programming, the factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created.

최종 결정되지 않은 객체를 만드는 패턴


정확하지 않지만 단순하게 생각하면 팩토리 메서드는 프로퍼티가 변경 가능한 객체를 만드는 메서드라고 생각하면 될 것 같다.


아직 프라미스도 익숙치 않지만

더 좋은 방법이 나왔다


다음은 

6 Reasons Why JavaScript’s Async/Await Blows Promises Away 라는 제목의 글이다.


https://hackernoon.com/6-reasons-why-javascripts-async-await-blows-promises-away-tutorial-c7ec10518dd9



'javascript' 카테고리의 다른 글

nodeJs require  (0) 2018.04.13
함수형 프로그래밍 : reduce  (0) 2018.04.07
let의 필요성  (0) 2018.03.20
async function 과 await  (0) 2018.03.10
pseudo code 이벤트 발생 시키기  (0) 2018.01.10

jpa 겟 스타트에 보면

메모리 DB를 쓰고 있고

datasource 프로퍼티 설정은 안나온다.


applicatoin.properites에서 설정하면 된다.


spring.datasource.url = jdbc:mysql://localhost:3306/dbname?useSSL=false

spring.datasource.username = username

spring.datasource.password = password

var는 전역 let은 지역 대충 구분은 이러지만

서로 바꿔쓴다고 뭔가 달라진걸 느끼기 힘들다.


let이 왜 있나 했더니 지역 변수를 못잡아 주는 스코프에서 let을 쓰면 로컬화 할 수 있다.

대표적인 예가 for문이다.

for(var i = 0; i < 5; i++ ) {

  $(div).onClick = () => { alert( i ); }

}


for(let i = 0; i < 5; i++ ) {

  $(div).onClick = () => { alert( i ); }

}



'javascript' 카테고리의 다른 글

함수형 프로그래밍 : reduce  (0) 2018.04.07
promise 대신 async, await를 사용하자  (0) 2018.03.22
async function 과 await  (0) 2018.03.10
pseudo code 이벤트 발생 시키기  (0) 2018.01.10
ajax의 자동 encoding  (0) 2017.12.28

프로그램을 시작한지 얼마 안됐을 때 누구나 DB에서 resultSet을 받아서 int로 받으려는데 에러가 나서 골아픈 적이 있었을 것이다.

DB 타입은 number인데 왜 BigDecimal로 나오는 거지? 왜 이따구로 만들었을 까 정말 불편하다

라고 생각을 했다.


컴퓨터가 부동 소수점 연산을 잘 못한 다는 걸 알고 짜증난 적도 많다.

한국돈은 소수점이 나올 일이 없는데 달러는 소수점이 나온다.

가상화폐 거래소 만들면서 많이 느꼈다.

소수를 계속 정수로 만들어서 계산하고 나중에 다시 소수로 만들어주는 짜증나는 작업을 계속 했는데


BigDecimal이 소수 점 계산을 정확히 해준다.

그런데 정작 써봐도 똑같았다.

낚였다고 정말 욕을 많이 했는데

생성할 때 float나 double로 생성하면 안된다고 한다.

그냥 String으로 생성하거나 초기값을 스태틱 상수 Bigdecimal.ZERO로 쓰면 된다.


이런 걸 대부분 우연히 알아내게 된다는게 참 슬프다.

'Java' 카테고리의 다른 글

Java Applet ReflectPermission  (0) 2018.03.28
Java Applet 이런 젠장  (0) 2018.03.28
autoclose resource : try-with-resource  (0) 2018.03.09
자바 자체 인증키 발급  (0) 2018.01.02
final 변수 설정을 하는 이유  (0) 2017.12.21

+ Recent posts