net.openhft.chronicle.wire.TextMethodWriterInvocationHandler Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2016-2020 chronicle.software
*
* https://chronicle.software
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.openhft.chronicle.wire;
import net.openhft.chronicle.core.Jvm;
import net.openhft.chronicle.core.scoped.ScopedResource;
import net.openhft.chronicle.core.util.Mocker;
import net.openhft.chronicle.core.util.ObjectUtils;
import org.jetbrains.annotations.NotNull;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Supplier;
/**
* This class represents an invocation handler specifically for text method writers. It extends the
* AbstractMethodWriterInvocationHandler to implement method call behavior for text-based method writers.
* It mainly converts method calls to textual data using the provided MarshallableOut.
*/
public class TextMethodWriterInvocationHandler extends AbstractMethodWriterInvocationHandler {
@NotNull
private final Supplier marshallableOutSupplier;
private final Map> visitorConverter = new LinkedHashMap<>();
/**
* Constructor initializing the handler with a MarshallableOut instance.
*
* @param tClass The class for which this invocation handler is being used.
* @param marshallableOut The MarshallableOut instance used for data serialization.
*/
TextMethodWriterInvocationHandler(Class> tClass, @NotNull MarshallableOut marshallableOut) {
this(tClass, () -> marshallableOut);
}
/**
* Constructor initializing the handler with a supplier for MarshallableOut.
*
* @param tClass The class for which this invocation handler is being used.
* @param marshallableOutSupplier The supplier providing instances of MarshallableOut for data serialization.
*/
public TextMethodWriterInvocationHandler(Class> tClass, @NotNull Supplier marshallableOutSupplier) {
super(tClass);
this.marshallableOutSupplier = marshallableOutSupplier;
}
@Override
protected Object doInvoke(Object proxy, Method method, Object[] args) {
if (method.getName().equals("writingDocument") && method.getParameterCount() == 0) {
MarshallableOut marshallableOut = this.marshallableOutSupplier.get();
return marshallableOut.writingDocument();
}
return super.doInvoke(proxy, method, args);
}
@Override
protected void handleInvoke(Method method, Object[] args) {
visitorConverter.computeIfAbsent(method, this::buildConverter)
.accept(args);
boolean chained = method.getReturnType().isInterface();
MarshallableOut marshallableOut = this.marshallableOutSupplier.get();
try (WriteDocumentContext dc = (WriteDocumentContext) marshallableOut.acquireWritingDocument(false)) {
try {
dc.chainedElement(chained);
Wire wire = dc.wire();
handleInvoke(method, args, wire);
} catch (Throwable t) {
dc.rollbackOnClose();
throw Jvm.rethrow(t);
}
}
}
static final Consumer
© 2015 - 2024 Weber Informatics LLC | Privacy Policy