PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms 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. package org.jboss.as.pojo.descriptor;
  23. import org.jboss.as.pojo.service.BeanInfo;
  24. import org.jboss.as.pojo.service.DefaultBeanInfo;
  25. import org.jboss.as.server.deployment.reflect.DeploymentReflectionIndex;
  26. import java.lang.reflect.ParameterizedType;
  27. import java.lang.reflect.Type;
  28. import java.util.ArrayList;
  29. import java.util.Deque;
  30. import java.util.List;
  31. /**
  32. * Abstract config visitor node.
  33. *
  34. * @author <a href="mailto:ales.justin@jboss.org">Ales Justin</a>
  35. */
  36. public abstract class AbstractConfigVisitorNode implements ConfigVisitorNode, TypeProvider {
  37. public void visit(ConfigVisitor visitor) {
  38. visitor.visit(this);
  39. }
  40. /**
  41. * Add children as needed.
  42. *
  43. * @param visitor the current visitor
  44. * @param nodes the nodes list to add to
  45. */
  46. protected void addChildren(ConfigVisitor visitor, List<ConfigVisitorNode> nodes) {
  47. }
  48. public Iterable<ConfigVisitorNode> getChildren(ConfigVisitor visitor) {
  49. List<ConfigVisitorNode> nodes = new ArrayList<ConfigVisitorNode>();
  50. addChildren(visitor, nodes);
  51. return nodes;
  52. }
  53. /**
  54. * Get temp bean info.
  55. *
  56. * @param visitor the visitor
  57. * @param className the class name
  58. * @return bean info
  59. */
  60. protected static BeanInfo getTempBeanInfo(ConfigVisitor visitor, String className) {
  61. return getTempBeanInfo(visitor, getType(visitor, className));
  62. }
  63. /**
  64. * Get temp bean info.
  65. *
  66. * @param visitor the visitor
  67. * @param clazz the class
  68. * @return bean info
  69. */
  70. @SuppressWarnings({"unchecked"})
  71. protected static BeanInfo getTempBeanInfo(ConfigVisitor visitor, Class<?> clazz) {
  72. return new DefaultBeanInfo(visitor.getReflectionIndex(), clazz);
  73. }
  74. /**
  75. * Get temp bean info.
  76. *
  77. * @param clazz the class
  78. * @return bean info
  79. */
  80. @SuppressWarnings({"unchecked"})
  81. protected static BeanInfo getTempBeanInfo(Class<?> clazz) {
  82. return new DefaultBeanInfo(DeploymentReflectionIndex.create(), clazz);
  83. }
  84. /**
  85. * Load class.
  86. *
  87. * @param visitor the visitor
  88. * @param className the class name
  89. * @return class or null if null class name
  90. */
  91. protected static Class<?> getType(ConfigVisitor visitor, String className) {
  92. if (className != null) {
  93. try {
  94. return visitor.getModule().getClassLoader().loadClass(className);
  95. } catch (Exception e) {
  96. throw new IllegalArgumentException(e);
  97. }
  98. }
  99. return null;
  100. }
  101. /**
  102. * Get component type.
  103. *
  104. * @param type the type
  105. * @param index the component index
  106. * @return component's class or null if cannot be determined
  107. */
  108. static Type getComponentType(ParameterizedType type, int index) {
  109. Type[] tp = type.getActualTypeArguments();
  110. if (index + 1 > tp.length)
  111. return null;
  112. return tp[index];
  113. }
  114. @Override
  115. public Class<?> getType(ConfigVisitor visitor, ConfigVisitorNode previous) {
  116. Deque<ConfigVisitorNode> nodes = visitor.getCurrentNodes();
  117. if (nodes.isEmpty())
  118. throw new IllegalArgumentException("Cannot determine type - insufficient info on configuration!");
  119. ConfigVisitorNode current = nodes.pop();
  120. try {
  121. if (current instanceof TypeProvider) {
  122. return TypeProvider.class.cast(current).getType(visitor, current);
  123. } else {
  124. return getType(visitor, current);
  125. }
  126. } finally {
  127. nodes.push(current);
  128. }
  129. }
  130. }