001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.devtools.jpa.eclipselink.conv.parser.helper.resolver;
017
018import japa.parser.ast.CompilationUnit;
019import japa.parser.ast.body.FieldDeclaration;
020import japa.parser.ast.body.ModifierSet;
021import japa.parser.ast.body.TypeDeclaration;
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024
025import java.lang.reflect.Field;
026
027public final class ResolverUtil {
028
029    private static final Log LOG = LogFactory.getLog(ResolverUtil.class);
030
031    private ResolverUtil() {
032        throw new UnsupportedOperationException("do not call");
033    }
034
035    public static boolean canFieldBeAnnotated(FieldDeclaration node) {
036        final TypeDeclaration dclr = (TypeDeclaration) node.getParentNode();
037        if (!ModifierSet.isStatic(node.getModifiers()) && dclr.getParentNode() instanceof CompilationUnit) {
038            //handling nested classes
039            return true;
040        }
041        return false;
042    }
043
044    public static String logMsgForField(String enclosingClass, String fieldName, String mappedClass) {
045        return enclosingClass + "." + fieldName + " for the mapped class " + mappedClass;
046    }
047
048    public static String logMsgForClass(String enclosingClass, String mappedClass) {
049        return enclosingClass + " for the mapped class " + mappedClass;
050    }
051
052    public static Class<?> getType(String clazz, String fieldName) {
053        try {
054            Class<?> c = Class.forName(clazz);
055            Field field = null;
056            while (field == null && c != Object.class) {
057                Field[] fields = c.getDeclaredFields();
058                field = getField(fields, fieldName);
059                c = c.getSuperclass();
060            }
061            if (field != null) {
062                return field.getType();
063            }
064        } catch (Exception e) {
065            LOG.error("Cannot get type from " + clazz + "." + fieldName, e);
066        }
067        return null;
068    }
069
070    private static Field getField(Field[] fields, String fieldName) {
071        if (fields != null) {
072            for (Field field : fields) {
073                if (fieldName.equals(field.getName())) {
074                    return field;
075                }
076            }
077        }
078        return null;
079    }
080}