ByteBuddy가 해당 빌더에 대한 인터페이스가있는 단계 빌더를 구현하려고합니다. 나는 2 개의 장소에 붙이게된다.ByteBuddy로 스텝 빌더 생성하기
- 메서드 체인을위한 현재 인스턴스를 반환하는 setter를 만드는 방법은 무엇입니까?
내가 시작 : 난 그렇게 현재 빌더 인스턴스를 반환 싶습니다에만
.method(ElementMatchers.isSetter())
.intercept(FieldAccessor.ofBeanProperty());
우리가 할 수처럼 체인 호출 :이 같은 인터셉터를 생성하므로 대신
final Object obj = ...builder().id(100).name("test").build();
이는 해킹과 같아서 가능하면 반영하지 않으려합니다.
@RuntimeType
public Object intercept(@RuntimeType Object arg, @This Object source, @Origin Method method)
{
try
{
// Field name is same as method name.
final Field field = source.getClass().getDeclaredField(method.getName());
field.setAccessible(true);
field.set(source, arg);
}
catch (Throwable ex)
{
throw new Error(ex);
}
// Return current builder instance.
return source;
}
- 리플렉션없이 정의하는 클래스의 정의 된 필드에 쉽게 액세스 할 수 있습니까?
은 현재 내가 루프에서 빌더 클래스에 필드를 추가하고 빌더 내 빌드 방법은 다음과 같이 차단된다
private static final class InterBuilder
{
private final Collection<String> fields;
private final Constructor<?> constructor;
InterBuilder(final Constructor<?> constructor, final Collection<String> fields)
{
this.constructor = constructor;
this.fields = fields;
}
@RuntimeType
public Object intercept(@This Object source, @Origin Method method)
{
try
{
final Object[] args = Arrays.stream(source.getClass().getDeclaredFields())
.filter(f -> this.fields.contains(f.getName()))
.map(f -> { try {
f.setAccessible(true);
return f.get(source); }
catch (Throwable ex) { throw new Error(ex); } })
.toArray();
// Invoke a constructor passing in the private field values from the builder...
return this.constructor.newInstance(args);
}
catch (Throwable ex)
{
throw new Error(ex);
}
}
}
은 내가
@FieldValue
annoation을 보았다. 나는 그들의 이름을 알지 못하고 모든 분야를 알려줄 무언가가 있다고 생각하지 않습니까?
코드는 현재 개념 증명입니다. 내가 여기서하고있는 일을하는 더 좋은 방법이 있습니까?
감사합니다.
고맙습니다! 나는 이것을 간단한 콩 세터와 함께 사용할 수있게했다. "빌드"를 구현하는 가장 좋은 방법은 무엇입니까? 나는 방금 설정 한 비공개 필드에 액세스해야한다. return new Something (this.a, this.b, this.c); 물론 클래스 작성자이기 때문에 작성자도 모두 생성됩니다. 생성자는입니다. –
akagixxer
MethodCall 구현을 사용하여 생성자를 호출 할 수 있습니다. –
필자가 필요한 "MethodCall.construct (...)"을 발견했습니다. 이것은 훌륭한 도구입니다, 고마워요! – akagixxer