PageRenderTime 41ms CodeModel.GetById 33ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/jboss-as-7.1.1.Final/pojo/src/main/java/org/jboss/as/pojo/descriptor/AbstractConfigVisitorNode.java

#
Java | 145 lines | 64 code | 15 blank | 66 comment | 6 complexity | a148af423a0ea9416bf82ab5d4daf320 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1/*
  2 * JBoss, Home of Professional Open Source.
  3 * Copyright 2011, Red Hat, Inc., and individual contributors
  4 * as indicated by the @author tags. See the copyright.txt file in the
  5 * distribution for a full listing of individual contributors.
  6 *
  7 * This is free software; you can redistribute it and/or modify it
  8 * under the terms of the GNU Lesser General Public License as
  9 * published by the Free Software Foundation; either version 2.1 of
 10 * the License, or (at your option) any later version.
 11 *
 12 * This software is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 15 * Lesser General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU Lesser General Public
 18 * License along with this software; if not, write to the Free
 19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 21 */
 22
 23package org.jboss.as.pojo.descriptor;
 24
 25import org.jboss.as.pojo.service.BeanInfo;
 26import org.jboss.as.pojo.service.DefaultBeanInfo;
 27import org.jboss.as.server.deployment.reflect.DeploymentReflectionIndex;
 28
 29import java.lang.reflect.ParameterizedType;
 30import java.lang.reflect.Type;
 31import java.util.ArrayList;
 32import java.util.Deque;
 33import java.util.List;
 34
 35/**
 36 * Abstract config visitor node.
 37 *
 38 * @author <a href="mailto:ales.justin@jboss.org">Ales Justin</a>
 39 */
 40public abstract class AbstractConfigVisitorNode implements ConfigVisitorNode, TypeProvider {
 41    public void visit(ConfigVisitor visitor) {
 42        visitor.visit(this);
 43    }
 44
 45    /**
 46     * Add children as needed.
 47     *
 48     * @param visitor the current visitor
 49     * @param nodes   the nodes list to add to
 50     */
 51    protected void addChildren(ConfigVisitor visitor, List<ConfigVisitorNode> nodes) {
 52    }
 53
 54    public Iterable<ConfigVisitorNode> getChildren(ConfigVisitor visitor) {
 55        List<ConfigVisitorNode> nodes = new ArrayList<ConfigVisitorNode>();
 56        addChildren(visitor, nodes);
 57        return nodes;
 58    }
 59
 60    /**
 61     * Get temp bean info.
 62     *
 63     * @param visitor   the visitor
 64     * @param className the class name
 65     * @return bean info
 66     */
 67    protected static BeanInfo getTempBeanInfo(ConfigVisitor visitor, String className) {
 68        return getTempBeanInfo(visitor, getType(visitor, className));
 69    }
 70
 71    /**
 72     * Get temp bean info.
 73     *
 74     * @param visitor the visitor
 75     * @param clazz   the class
 76     * @return bean info
 77     */
 78    @SuppressWarnings({"unchecked"})
 79    protected static BeanInfo getTempBeanInfo(ConfigVisitor visitor, Class<?> clazz) {
 80        return new DefaultBeanInfo(visitor.getReflectionIndex(), clazz);
 81    }
 82
 83    /**
 84     * Get temp bean info.
 85     *
 86     * @param clazz the class
 87     * @return bean info
 88     */
 89    @SuppressWarnings({"unchecked"})
 90    protected static BeanInfo getTempBeanInfo(Class<?> clazz) {
 91        return new DefaultBeanInfo(DeploymentReflectionIndex.create(), clazz);
 92    }
 93
 94    /**
 95     * Load class.
 96     *
 97     * @param visitor   the visitor
 98     * @param className the class name
 99     * @return class or null if null class name
100     */
101    protected static Class<?> getType(ConfigVisitor visitor, String className) {
102        if (className != null) {
103            try {
104                return visitor.getModule().getClassLoader().loadClass(className);
105            } catch (Exception e) {
106                throw new IllegalArgumentException(e);
107            }
108        }
109        return null;
110    }
111
112    /**
113     * Get component type.
114     *
115     * @param type the type
116     * @param index the component index
117     * @return component's class or null if cannot be determined
118     */
119    static Type getComponentType(ParameterizedType type, int index) {
120        Type[] tp = type.getActualTypeArguments();
121        if (index + 1 > tp.length)
122            return null;
123
124        return tp[index];
125    }
126
127    @Override
128    public Class<?> getType(ConfigVisitor visitor, ConfigVisitorNode previous) {
129        Deque<ConfigVisitorNode> nodes = visitor.getCurrentNodes();
130        if (nodes.isEmpty())
131            throw new IllegalArgumentException("Cannot determine type - insufficient info on configuration!");
132
133        ConfigVisitorNode current = nodes.pop();
134        try {
135            if (current instanceof TypeProvider) {
136                return TypeProvider.class.cast(current).getType(visitor, current);
137            } else {
138                return getType(visitor, current);
139            }
140        } finally {
141            nodes.push(current);
142        }
143
144    }
145}