티스토리 뷰
728x90
package com.tistory.seunggabi.util
import java.lang.reflect.Constructor
object ReflectionUtils {
fun <T> new(clazz: Class<T>, vararg args: Any?): T {
val list = if (args.isEmpty()) {
emptyList()
} else {
args.asList()
}
@Suppress("UNCHECKED_CAST")
val parameters = parameters(list as List<Any>)
@Suppress("SpreadOperator")
val constructor = clazz.getConstructor(*parameters)
return new(constructor, list)
}
private fun parameters(args: List<Any>): Array<Class<*>> {
return args
.map {
it::class.java
}
.toTypedArray()
}
private fun <T> new(constructor: Constructor<T>, args: List<Any?>): T {
constructor.isAccessible = true
@Suppress("SpreadOperator")
return constructor.newInstance(*args.toTypedArray())
}
}
package com.tistory.seunggabi.util
import jdk.jshell.spi.ExecutionControl.NotImplementedException
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatThrownBy
import org.junit.jupiter.api.Test
internal class ReflectionUtilsTest {
class MockClass(private val args: String) {
fun f() {
throw NotImplementedException(args)
}
}
companion object {
private val CLAZZ = MockClass::class.java
}
@Test
fun new() {
// given
val should = "Custom Exception"
// when
val actual = ReflectionUtils.new(CLAZZ, should)
// then
assertThat(actual).isExactlyInstanceOf(CLAZZ)
assertThatThrownBy { actual.f() }
.isInstanceOf(NotImplementedException::class.java)
.hasMessage(should)
}
}
https://stackoverflow.com/questions/46674787/instanceclass-java-vs-instance-javaclass
728x90
'공부' 카테고리의 다른 글
[JPA] manual SQL (nativeQuery=True) (0) | 2022.07.10 |
---|---|
[js] npm(직렬) vs yarn(병렬) (0) | 2022.07.10 |
[js] axios (0) | 2022.07.09 |
[js] package.json "lint": "eslint '**/*.{js,jsx}' --quiet" (0) | 2022.07.09 |
[kotlin] list to map (0) | 2022.07.09 |
댓글
250x250
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- Bot
- 테슬라 레퍼럴 적용 확인
- 팔로워 수 세기
- 모델 Y 레퍼럴
- 책그림
- 개리마커스
- 테슬라 레퍼럴 코드 확인
- follower
- COUNT
- 테슬라 리퍼럴 코드 생성
- 테슬라 크레딧 사용
- 메디파크 내과 전문의 의학박사 김영수
- 어떻게 능력을 보여줄 것인가?
- 인스타그램
- 테슬라 레퍼럴
- Kluge
- 테슬라 리퍼럴 코드
- 김달
- wlw
- 레퍼럴
- 테슬라
- 유투브
- 연애학개론
- 모델y
- 클루지
- 테슬라 리퍼럴 코드 혜택
- 할인
- 테슬라 추천
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
31 |
글 보관함