| /** |
| * Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License 2.0 |
| * which accompanies this distribution, and is available at |
| * https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * Florian Pirchner - Initial implementation |
| */ |
| package org.eclipse.osbp.runtime.common.annotations; |
| |
| import java.beans.BeanInfo; |
| import java.beans.Introspector; |
| import java.beans.PropertyDescriptor; |
| import java.lang.reflect.Field; |
| import java.lang.reflect.Method; |
| import java.util.Collections; |
| import java.util.HashMap; |
| import java.util.Map; |
| |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| /** |
| * The Class FindIdHelper. Can be used for DTOs and Entities. |
| */ |
| public class FindIdHelper { |
| |
| /** The Constant LOGGER. */ |
| private static final Logger LOGGER = LoggerFactory.getLogger(FindIdHelper.class); |
| |
| /** The infos. */ |
| private static Map<Class<?>, Info> INFOS = Collections.synchronizedMap(new HashMap<Class<?>, FindIdHelper.Info>()); |
| |
| /** |
| * Returns the id field. Field annotated with {@link Id}. |
| * |
| * @param clazz |
| * the clazz |
| * @return the id field |
| */ |
| public static Field getIdField(Class<?> clazz) { |
| Info info = getInfo(clazz); |
| return info.getIdField(); |
| } |
| |
| /** |
| * Returns the info for the given class. |
| * |
| * @param clazz |
| * the clazz |
| * @return the info |
| */ |
| protected static Info getInfo(Class<?> clazz) { |
| Info info = INFOS.get(clazz); |
| if (info == null) { |
| info = createInfo(clazz); |
| } |
| return info; |
| } |
| |
| /** |
| * Returns the id field. Field annotated with {@link Id}. |
| * |
| * @param dto |
| * the dto |
| * @return the id field |
| */ |
| public static Object getIdValue(Object dto) { |
| Info info = getInfo(dto.getClass()); |
| Field f = info.getIdField(); |
| if(f == null) { |
| return null; |
| } |
| f.setAccessible(true); |
| try { |
| return f.get(dto); |
| } catch (IllegalArgumentException | IllegalAccessException e) { |
| throw new IllegalStateException(e); |
| } |
| } |
| |
| /** |
| * Returns the field value. |
| * |
| * @param instance |
| * the instance |
| * @param idProperty |
| * the id property |
| * @return the value |
| */ |
| public static Object getValue(Object instance, String idProperty) { |
| try { |
| BeanInfo beanInfo = Introspector.getBeanInfo(instance.getClass()); |
| for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) { |
| if (pd.getName().equals(idProperty)) { |
| Method idMethod = pd.getReadMethod(); |
| return idMethod.invoke(instance); |
| } |
| } |
| } catch (Exception e) { |
| LOGGER.error("{}", e); |
| } |
| return null; |
| } |
| |
| /** |
| * Returns true, if the given field is a id field. |
| * |
| * @param clazz |
| * the clazz |
| * @param fieldName |
| * the field name |
| * @return true, if is id field |
| */ |
| public static boolean isIdField(Class<?> clazz, String fieldName) { |
| Info info = getInfo(clazz); |
| return info.getIdField() != null ? info.getIdField().getName().equals(fieldName) : false; |
| } |
| |
| /** |
| * Creates a new info. |
| * |
| * @param clazz |
| * the clazz |
| * @return info |
| */ |
| private static Info createInfo(Class<?> clazz) { |
| Info info = new Info(); |
| applyFieldInfo(clazz, info); |
| |
| INFOS.put(clazz, info); |
| |
| return info; |
| } |
| |
| /** |
| * Applies all required field infos to the info object. |
| * |
| * @param clazz |
| * the clazz |
| * @param info |
| * the info |
| */ |
| private static void applyFieldInfo(Class<?> clazz, Info info) { |
| try { |
| for (Field field : clazz.getDeclaredFields()) { |
| if (field.getAnnotation(org.eclipse.osbp.runtime.common.annotations.Id.class) != null |
| || field.getAnnotation(javax.persistence.Id.class) != null) { |
| info.idField = field; |
| break; |
| } |
| } |
| |
| if (info.idField == null) { |
| Class<?> superClass = clazz.getSuperclass(); |
| if (superClass != null) { |
| applyFieldInfo(superClass, info); |
| } |
| } |
| } catch (SecurityException e) { |
| LOGGER.error("{}", e); |
| } |
| } |
| |
| /** |
| * The Class Info. |
| */ |
| private static class Info { |
| |
| /** The id field. */ |
| private Field idField; |
| |
| public Field getIdField() { |
| return idField; |
| } |
| |
| } |
| } |