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 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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.eventtools;
  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 (JavaClass clazz : classes) {
  85. if (clazz.isInterface() && implementsInterface(clazz, CLASSNAME_EVENT_PRODUCER)) {
  86. processEventProducerInterface(clazz);
  87. eventProducerFound = true;
  88. }
  89. }
  90. return eventProducerFound;
  91. }
  92. private boolean implementsInterface(JavaClass clazz, String intf) {
  93. JavaClass[] classes = clazz.getImplementedInterfaces();
  94. for (JavaClass cl : classes) {
  95. if (cl.getFullyQualifiedName().equals(intf)) {
  96. return true;
  97. }
  98. }
  99. return false;
  100. }
  101. /**
  102. * Processes an EventProducer interface and creates an EventProducerModel from it.
  103. * @param clazz the EventProducer interface
  104. * @throws EventConventionException if the event producer conventions are violated
  105. * @throws ClassNotFoundException if a required class cannot be found
  106. */
  107. protected void processEventProducerInterface(JavaClass clazz)
  108. throws EventConventionException, ClassNotFoundException {
  109. EventProducerModel prodMeta = new EventProducerModel(clazz.getFullyQualifiedName());
  110. JavaMethod[] methods = clazz.getMethods(true);
  111. for (JavaMethod method : methods) {
  112. EventMethodModel methodMeta = createMethodModel(method);
  113. prodMeta.addMethod(methodMeta);
  114. }
  115. EventModel model = new EventModel();
  116. model.addProducer(prodMeta);
  117. models.add(model);
  118. }
  119. private EventMethodModel createMethodModel(JavaMethod method)
  120. throws EventConventionException, ClassNotFoundException {
  121. JavaClass clazz = method.getParentClass();
  122. //Check EventProducer conventions
  123. if (!method.getReturnType().isVoid()) {
  124. throw new EventConventionException("All methods of interface "
  125. + clazz.getFullyQualifiedName() + " must have return type 'void'!");
  126. }
  127. String methodSig = clazz.getFullyQualifiedName() + "." + method.getCallSignature();
  128. JavaParameter[] params = method.getParameters();
  129. if (params.length < 1) {
  130. throw new EventConventionException("The method " + methodSig
  131. + " must have at least one parameter: 'Object source'!");
  132. }
  133. Type firstType = params[0].getType();
  134. if (firstType.isPrimitive() || !"source".equals(params[0].getName())) {
  135. throw new EventConventionException("The first parameter of the method " + methodSig
  136. + " must be: 'Object source'!");
  137. }
  138. //build method model
  139. DocletTag tag = method.getTagByName("event.severity");
  140. EventSeverity severity;
  141. if (tag != null) {
  142. severity = EventSeverity.valueOf(tag.getValue());
  143. } else {
  144. severity = EventSeverity.INFO;
  145. }
  146. EventMethodModel methodMeta = new EventMethodModel(
  147. method.getName(), severity);
  148. if (params.length > 1) {
  149. for (int j = 1, cj = params.length; j < cj; j++) {
  150. JavaParameter p = params[j];
  151. Class<?> type;
  152. JavaClass pClass = p.getType().getJavaClass();
  153. if (p.getType().isPrimitive()) {
  154. type = PRIMITIVE_MAP.get(pClass.getName());
  155. if (type == null) {
  156. throw new UnsupportedOperationException(
  157. "Primitive datatype not supported: " + pClass.getName());
  158. }
  159. } else {
  160. String className = pClass.getFullyQualifiedName();
  161. type = Class.forName(className);
  162. }
  163. methodMeta.addParameter(type, p.getName());
  164. }
  165. }
  166. Type[] exceptions = method.getExceptions();
  167. if (exceptions != null && exceptions.length > 0) {
  168. //We only use the first declared exception because that is always thrown
  169. JavaClass cl = exceptions[0].getJavaClass();
  170. methodMeta.setExceptionClass(cl.getFullyQualifiedName());
  171. methodMeta.setSeverity(EventSeverity.FATAL); //In case it's not set in the comments
  172. }
  173. return methodMeta;
  174. }
  175. /**
  176. * Returns the event model that has been accumulated.
  177. * @return the event model.
  178. */
  179. public List<EventModel> getModels() {
  180. return this.models;
  181. }
  182. }