|
| 1 | +package org.hamcrest.generator; |
| 2 | + |
| 3 | + |
| 4 | +import java.util.ArrayList; |
| 5 | +import static java.util.Collections.unmodifiableList; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +/** |
| 9 | + * Represents a Matcher Factory method. |
| 10 | + * |
| 11 | + * <p>This class uses Strings to represent things instead of java.lang.reflect equivalents, |
| 12 | + * allowing methods to be defined from sources other than reflection of classes in the |
| 13 | + * classpath. |
| 14 | + * |
| 15 | + * @author Joe Walnes |
| 16 | + * @see SugarGenerator |
| 17 | + * @see org.hamcrest.Factory |
| 18 | + */ |
| 19 | +public class FactoryMethod { |
| 20 | + |
| 21 | + private final String matcherClass; |
| 22 | + private final String factoryMethod; |
| 23 | + |
| 24 | + private String generifiedType; |
| 25 | + private List<Parameter> parameters = new ArrayList<Parameter>(); |
| 26 | + private List<String> exceptions = new ArrayList<String>(); |
| 27 | + private List<String> genericTypeParameters = new ArrayList<String>(); |
| 28 | + private String javaDoc; |
| 29 | + |
| 30 | + public FactoryMethod(String matcherClass, String factoryMethod) { |
| 31 | + this.matcherClass = matcherClass; |
| 32 | + this.factoryMethod = factoryMethod; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Original class this factory method came from. |
| 37 | + */ |
| 38 | + public String getMatcherClass() { |
| 39 | + return matcherClass; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Original name of factory method. |
| 44 | + */ |
| 45 | + public String getName() { |
| 46 | + return factoryMethod; |
| 47 | + } |
| 48 | + |
| 49 | + public void setGenerifiedType(String generifiedType) { |
| 50 | + this.generifiedType = generifiedType; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Generified type of matcher. |
| 55 | + * ie. 'public Matcher<THISBIT> blah(...)' |
| 56 | + */ |
| 57 | + public String getGenerifiedType() { |
| 58 | + return generifiedType; |
| 59 | + } |
| 60 | + |
| 61 | + public void addParameter(String type, String name) { |
| 62 | + parameters.add(new Parameter(type, name)); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * List of Parameters passed to factory method. |
| 67 | + * ie. 'public Matcher<...&ht;> blah(THIS, AND, THAT)' |
| 68 | + */ |
| 69 | + public List<Parameter> getParameters() { |
| 70 | + return unmodifiableList(parameters); |
| 71 | + } |
| 72 | + |
| 73 | + public void addException(String exception) { |
| 74 | + exceptions.add(exception); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * List of exceptions thrown by factory method. |
| 79 | + * ie. 'public Matcher<...> blah(...) throws THIS, THAT' |
| 80 | + */ |
| 81 | + public List<String> getExceptions() { |
| 82 | + return unmodifiableList(exceptions); |
| 83 | + } |
| 84 | + |
| 85 | + public void addGenericTypeParameter(String genericTypeParameter) { |
| 86 | + genericTypeParameters.add(genericTypeParameter); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * List of generic type parameters for factory method definition. |
| 91 | + * ie. 'public <THIS,THAT> Matcher<...> blah(...)' |
| 92 | + * |
| 93 | + * @return |
| 94 | + */ |
| 95 | + public List<String> getGenericTypeParameters() { |
| 96 | + return unmodifiableList(genericTypeParameters); |
| 97 | + } |
| 98 | + |
| 99 | + public void setJavaDoc(String javaDoc) { |
| 100 | + this.javaDoc = javaDoc; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * JavaDoc definition of factory method. |
| 105 | + * Excludes surrounding comment markers. |
| 106 | + * Note that using standard Java reflection it is not possible to obtain this, |
| 107 | + * however source code parsers can read this. |
| 108 | + */ |
| 109 | + public String getJavaDoc() { |
| 110 | + return javaDoc; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Represents a parameter passed to a factory method. |
| 115 | + * |
| 116 | + * @see FactoryMethod |
| 117 | + */ |
| 118 | + public static class Parameter { |
| 119 | + |
| 120 | + private final String type; |
| 121 | + private final String name; |
| 122 | + |
| 123 | + public Parameter(String type, String name) { |
| 124 | + this.type = type; |
| 125 | + this.name = name; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Type of parameter, including any generic declarations. |
| 130 | + */ |
| 131 | + public String getType() { |
| 132 | + return type; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Name of parameter, if it can be obtained. If it cannot |
| 137 | + * be obtained, a sensible default will returned instead. |
| 138 | + */ |
| 139 | + public String getName() { |
| 140 | + return name; |
| 141 | + } |
| 142 | + |
| 143 | + } |
| 144 | + |
| 145 | +} |
0 commit comments