1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.commonclipse;
18
19 import org.eclipse.jdt.core.IMethod;
20 import org.eclipse.jdt.core.IType;
21 import org.eclipse.jdt.core.JavaModelException;
22
23
24 /***
25 * Generator for equals(Object) methods.
26 * @author fgiust
27 * @version $Revision: 1.6 $ ($Author: fgiust $)
28 */
29 public final class EqualsGenerator extends Generator
30 {
31
32 /***
33 * class name for the Equals builder.
34 */
35 private static final String BUILDER_CLASS = "org.apache.commons.lang.builder.EqualsBuilder";
36
37 /***
38 * singleton for EqualsGenerator.
39 */
40 private static Generator instance = new EqualsGenerator();
41
42 /***
43 * use getInstance() to obtain an instance of EqualsGenerator.
44 */
45 private EqualsGenerator()
46 {
47 }
48
49 /***
50 * returns the EqualsGenerator instance.
51 * @return instance of EqualsGenerator
52 */
53 public static Generator getInstance()
54 {
55 return instance;
56 }
57
58 /***
59 * @see net.sf.commonclipse.Generator#getMethodName()
60 */
61 protected String getMethodName()
62 {
63 return "equals";
64 }
65
66 /***
67 * @see net.sf.commonclipse.Generator#createMethod(org.eclipse.jdt.core.IType)
68 */
69 protected String createMethod(IType type) throws JavaModelException
70 {
71
72 StringBuffer buffer = new StringBuffer();
73
74 buffer.append(getJavadoc());
75
76 String className = type.getElementName();
77
78 buffer.append("public boolean equals(");
79
80 if (CCPluginPreferences.getPreferences().useFinalParameters())
81 {
82 buffer.append("final ");
83 }
84
85 buffer.append("Object object) {\n");
86
87 if (CCPluginPreferences.getPreferences().addInstanceCheckToEquals())
88 {
89 buffer.append("if (object == this) {\nreturn true;\n}\n");
90 }
91
92 buffer.append("if ( !(object instanceof ");
93 buffer.append(className);
94 buffer.append(") ) {\nreturn false;\n}\n");
95 buffer.append(className);
96 buffer.append(" rhs = (");
97 buffer.append(className);
98 buffer.append(") object;\nreturn new EqualsBuilder()\n");
99
100 if (CCPluginPreferences.getPreferences().appendSuperToEquals())
101 {
102 buffer.append(".appendSuper(super.equals(object))\n");
103 }
104
105 buffer.append(buildAppenderList(type));
106
107 buffer.append(".isEquals();\n}\n");
108 return buffer.toString();
109 }
110
111 /***
112 * @see net.sf.commonclipse.Generator#getFieldAppender(java.lang.String, java.lang.String)
113 */
114 protected String getFieldAppender(String fieldName, String accessor)
115 {
116 return ".append(this." + fieldName + ", rhs." + fieldName + ")\n";
117 }
118
119 /***
120 * @see net.sf.commonclipse.Generator#getExistingMethod(org.eclipse.jdt.core.IType)
121 */
122 protected IMethod getExistingMethod(IType type)
123 {
124 return type.getMethod(getMethodName(), new String[]{"QObject;"});
125 }
126
127 /***
128 * @see net.sf.commonclipse.Generator#addImports(org.eclipse.jdt.core.IType)
129 */
130 protected void addImports(IType type) throws JavaModelException
131 {
132 type.getCompilationUnit().createImport(BUILDER_CLASS, null, null);
133 }
134
135 /***
136 * Generates the method javadoc.
137 * @return String javadoc
138 */
139 private String getJavadoc()
140 {
141 return "/**\n * @see java.lang.Object#equals(Object)\n */\n";
142 }
143
144 }