You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EventProducerCollector.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.tools;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.util.Collections;
  22. import java.util.List;
  23. import java.util.Map;
  24. import org.apache.fop.events.EventProducer;
  25. import org.apache.fop.events.model.EventMethodModel;
  26. import org.apache.fop.events.model.EventModel;
  27. import org.apache.fop.events.model.EventProducerModel;
  28. import org.apache.fop.events.model.EventSeverity;
  29. import com.thoughtworks.qdox.JavaDocBuilder;
  30. import com.thoughtworks.qdox.model.DefaultDocletTagFactory;
  31. import com.thoughtworks.qdox.model.DocletTag;
  32. import com.thoughtworks.qdox.model.DocletTagFactory;
  33. import com.thoughtworks.qdox.model.JavaClass;
  34. import com.thoughtworks.qdox.model.JavaMethod;
  35. import com.thoughtworks.qdox.model.JavaParameter;
  36. import com.thoughtworks.qdox.model.Type;
  37. /**
  38. * Finds EventProducer interfaces and builds the event model for them.
  39. */
  40. class EventProducerCollector {
  41. private static final String CLASSNAME_EVENT_PRODUCER = EventProducer.class.getName();
  42. private static final Map<String, Class<?>> PRIMITIVE_MAP;
  43. static {
  44. Map<String, Class<?>> m = new java.util.HashMap<String, Class<?>>();
  45. m.put("boolean", Boolean.class);
  46. m.put("byte", Byte.class);
  47. m.put("char", Character.class);
  48. m.put("short", Short.class);
  49. m.put("int", Integer.class);
  50. m.put("long", Long.class);
  51. m.put("float", Float.class);
  52. m.put("double", Double.class);
  53. PRIMITIVE_MAP = Collections.unmodifiableMap(m);
  54. }
  55. private DocletTagFactory tagFactory;
  56. private List<EventModel> models = new java.util.ArrayList<EventModel>();
  57. /**
  58. * Creates a new EventProducerCollector.
  59. */
  60. EventProducerCollector() {
  61. this.tagFactory = createDocletTagFactory();
  62. }
  63. /**
  64. * Creates the {@link DocletTagFactory} to be used by the collector.
  65. * @return the doclet tag factory
  66. */
  67. protected DocletTagFactory createDocletTagFactory() {
  68. return new DefaultDocletTagFactory();
  69. }
  70. /**
  71. * Scans a file and processes it if it extends the {@link EventProducer} interface.
  72. * @param src the source file (a Java source file)
  73. * @return true if the file contained an EventProducer interface
  74. * @throws IOException if an I/O error occurs
  75. * @throws EventConventionException if the EventProducer conventions are violated
  76. * @throws ClassNotFoundException if a required class cannot be found
  77. */
  78. public boolean scanFile(File src)
  79. throws IOException, EventConventionException, ClassNotFoundException {
  80. JavaDocBuilder builder = new JavaDocBuilder(this.tagFactory);
  81. builder.addSource(src);
  82. JavaClass[] classes = builder.getClasses();
  83. boolean eventProducerFound = false;
  84. for (int i = 0, c = classes.length; i < c; i++) {
  85. JavaClass clazz = classes[i];
  86. if (clazz.isInterface() && implementsInterface(clazz, CLASSNAME_EVENT_PRODUCER)) {
  87. processEventProducerInterface(clazz);
  88. eventProducerFound = true;
  89. }
  90. }
  91. return eventProducerFound;
  92. }
  93. private boolean implementsInterface(JavaClass clazz, String intf) {
  94. JavaClass[] classes = clazz.getImplementedInterfaces();
  95. for (int i = 0, c = classes.length; i < c; i++) {
  96. JavaClass cl = classes[i];
  97. if (cl.getFullyQualifiedName().equals(intf)) {
  98. return true;
  99. }
  100. }
  101. return false;
  102. }
  103. /**
  104. * Processes an EventProducer interface and creates an EventProducerModel from it.
  105. * @param clazz the EventProducer interface
  106. * @throws EventConventionException if the event producer conventions are violated
  107. * @throws ClassNotFoundException if a required class cannot be found
  108. */
  109. protected void processEventProducerInterface(JavaClass clazz)
  110. throws EventConventionException, ClassNotFoundException {
  111. EventProducerModel prodMeta = new EventProducerModel(clazz.getFullyQualifiedName());
  112. JavaMethod[] methods = clazz.getMethods(true);
  113. for (int i = 0, c = methods.length; i < c; i++) {
  114. JavaMethod method = methods[i];
  115. EventMethodModel methodMeta = createMethodModel(method);
  116. prodMeta.addMethod(methodMeta);
  117. }
  118. EventModel model = new EventModel();
  119. model.addProducer(prodMeta);
  120. models.add(model);
  121. }
  122. private EventMethodModel createMethodModel(JavaMethod method)
  123. throws EventConventionException, ClassNotFoundException {
  124. JavaClass clazz = method.getParentClass();
  125. //Check EventProducer conventions
  126. if (!method.getReturnType().isVoid()) {
  127. throw new EventConventionException("All methods of interface "
  128. + clazz.getFullyQualifiedName() + " must have return type 'void'!");
  129. }
  130. String methodSig = clazz.getFullyQualifiedName() + "." + method.getCallSignature();
  131. JavaParameter[] params = method.getParameters();
  132. if (params.length < 1) {
  133. throw new EventConventionException("The method " + methodSig
  134. + " must have at least one parameter: 'Object source'!");
  135. }
  136. Type firstType = params[0].getType();
  137. if (firstType.isPrimitive() || !"source".equals(params[0].getName())) {
  138. throw new EventConventionException("The first parameter of the method " + methodSig
  139. + " must be: 'Object source'!");
  140. }
  141. //build method model
  142. DocletTag tag = method.getTagByName("event.severity");
  143. EventSeverity severity;
  144. if (tag != null) {
  145. severity = EventSeverity.valueOf(tag.getValue());
  146. } else {
  147. severity = EventSeverity.INFO;
  148. }
  149. EventMethodModel methodMeta = new EventMethodModel(
  150. method.getName(), severity);
  151. if (params.length > 1) {
  152. for (int j = 1, cj = params.length; j < cj; j++) {
  153. JavaParameter p = params[j];
  154. Class<?> type;
  155. JavaClass pClass = p.getType().getJavaClass();
  156. if (p.getType().isPrimitive()) {
  157. type = PRIMITIVE_MAP.get(pClass.getName());
  158. if (type == null) {
  159. throw new UnsupportedOperationException(
  160. "Primitive datatype not supported: " + pClass.getName());
  161. }
  162. } else {
  163. String className = pClass.getFullyQualifiedName();
  164. type = Class.forName(className);
  165. }
  166. methodMeta.addParameter(type, p.getName());
  167. }
  168. }
  169. Type[] exceptions = method.getExceptions();
  170. if (exceptions != null && exceptions.length > 0) {
  171. //We only use the first declared exception because that is always thrown
  172. JavaClass cl = exceptions[0].getJavaClass();
  173. methodMeta.setExceptionClass(cl.getFullyQualifiedName());
  174. methodMeta.setSeverity(EventSeverity.FATAL); //In case it's not set in the comments
  175. }
  176. return methodMeta;
  177. }
  178. /**
  179. * Returns the event model that has been accumulated.
  180. * @return the event model.
  181. */
  182. public List<EventModel> getModels() {
  183. return this.models;
  184. }
  185. }