org.jooq.impl.JPAConverter Maven / Gradle / Ivy
/*
* 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.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.impl;
import java.lang.reflect.Method;
import jakarta.persistence.AttributeConverter;
import org.jooq.exception.MappingException;
import org.jooq.tools.JooqLogger;
import org.jooq.tools.reflect.Reflect;
/**
* A converter that delegates data type conversions to a JPA
* {@link AttributeConverter}.
*
* This is particularly useful when generating code from a
* JPADatabase
, which reverse engineers JPA annotated entities, in
* case of which, by default, the {@link AttributeConverter} annotations are
* discovered automatically and the user-defined type is applied also in the
* jOOQ meta model.
*
* @author Lukas Eder
*/
public final class JPAConverter extends AbstractConverter {
private static final JooqLogger log = JooqLogger.getLogger(JPAConverter.class);
private final AttributeConverter delegate;
public JPAConverter(Class extends AttributeConverter> klass) {
super(fromType(klass), toType(klass));
try {
this.delegate = Reflect.on(klass).create().get();
}
catch (Exception e) {
throw new MappingException("Cannot instanciate AttributeConverter", e);
}
}
@SuppressWarnings("unchecked")
private static final Class fromType(Class extends AttributeConverter, T>> klass) {
Method candidate = null;
// [#10951] Try finding the bridge method, skip the inherited method if applicable
for (Method method : klass.getDeclaredMethods())
if ("convertToDatabaseColumn".equals(method.getName()))
if (method.getReturnType() == Object.class)
candidate = method;
else
return (Class) method.getReturnType();
if (candidate != null) {
log.warn("Couldn't find bridge method to detect generic type bound for " + klass.getName() + "::convertToDatabaseColumn");
return (Class) candidate.getReturnType();
}
throw new IllegalArgumentException();
}
@SuppressWarnings("unchecked")
private static final Class toType(Class extends AttributeConverter> klass) {
Method candidate = null;
// [#10951] Try finding the bridge method, skip the inherited method if applicable
for (Method method : klass.getDeclaredMethods())
if ("convertToEntityAttribute".equals(method.getName()))
if (method.getReturnType() == Object.class)
candidate = method;
else
return (Class) method.getReturnType();
if (candidate != null) {
log.warn("Couldn't find bridge method to detect generic type bound for " + klass.getName() + "::convertToEntityAttribute");
return (Class) candidate.getReturnType();
}
throw new IllegalArgumentException();
}
@Override
public final U from(T t) {
return delegate.convertToEntityAttribute(t);
}
@Override
public final T to(U u) {
return delegate.convertToDatabaseColumn(u);
}
}