001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /*
043: * ReflectionElementVerifier.java
044: *
045: * Created on April 17, 2007, 1:14 PM
046: *
047: * To change this template, choose Tools | Template Manager
048: * and open the template in the editor.
049: */
050:
051: package org.netbeans.test.umllib.project.verifier.elem;
052:
053: import java.lang.reflect.Field;
054: import java.lang.reflect.Method;
055: import java.lang.reflect.Modifier;
056:
057: import java.util.List;
058: import org.netbeans.test.umllib.project.JavaProject;
059: import org.netbeans.test.umllib.project.elem.IArgumentElem;
060: import org.netbeans.test.umllib.project.elem.IAttributeElem;
061: import org.netbeans.test.umllib.project.elem.IClassElem;
062: import org.netbeans.test.umllib.project.elem.IInterfaceElem;
063: import org.netbeans.test.umllib.project.elem.IJavaElem;
064: import org.netbeans.test.umllib.project.elem.IOperationElem;
065: import org.netbeans.test.umllib.project.elem.VisibilityType;
066:
067: /**
068: *
069: * @author <A HREF="mailto:sunflower@netbeans.org">Alexandr Scherbatiy</A>
070: */
071: public class ReflectionElementVerifier extends AElementVerifier {
072:
073: private Class cls;
074:
075: public ReflectionElementVerifier(IJavaElem javaElem,
076: String javaProjectName) {
077: this (javaElem, new JavaProject(javaProjectName));
078:
079: }
080:
081: public ReflectionElementVerifier(IJavaElem javaElem,
082: JavaProject javaProject) {
083: this (javaProject.getJavaClass(javaElem.getFullName()), javaElem);
084: }
085:
086: public ReflectionElementVerifier(Class cls, IJavaElem javaElem) {
087: super (javaElem);
088: if (cls == null) {
089: appendMessage("!!!--- Byte code was not generated for \""
090: + javaElem.getFullName() + "\" element---!!!");
091: //fail("Byte code was not generated for \"" + javaElem.getFullName() +"\" element");
092: }
093: this .cls = cls;
094:
095: System.out
096: .println("-------------------------------------------------");
097: showClass(cls);
098: System.out
099: .println("-------------------------------------------------");
100: System.out.println("[Elem]");
101: System.out.println(javaElem);
102: System.out
103: .println("-------------------------------------------------");
104: }
105:
106: protected void verifyName(IJavaElem javaElem) {
107: if (!javaElem.getFullName().equals(cls.getName())) {
108: appendMessage(javaElem.getName() + " != " + cls.getName());
109: }
110: }
111:
112: protected void verifySuperClass(IClassElem super Class) {
113: System.out.println("Super Class is not verified!!!");
114: }
115:
116: protected void verifySuperInterfaceList(
117: List<IInterfaceElem> super InterfaceList) {
118:
119: System.out.println("[super interfaces]");
120:
121: Class<?>[] interfaces = cls.getInterfaces();
122:
123: //System.out.println("interfaces: " + interfaces);
124: //System.out.println("list : " + superInterfaceList);
125:
126: if (interfaces.length != super InterfaceList.size()) {
127: appendMessage("super interface number " + interfaces.length
128: + " != " + super InterfaceList.size());
129: }
130:
131: for (IInterfaceElem interfaceElem : super InterfaceList) {
132:
133: String name = interfaceElem.getFullName();
134:
135: boolean flag = false;
136:
137: for (Class c : interfaces) {
138: //System.out.println("elem: \"" + c.getName() + "\"");
139: if (c.getName().equals(name)) {
140: flag = true;
141: break;
142: }
143: }
144:
145: if (!flag) {
146: //message += "[ (-) " + "Interface" + ": " + name + " ]";
147: appendMessage("SuperInterface" + ": " + name);
148: }
149: }
150:
151: }
152:
153: protected void verifyAttribute(IAttributeElem attributeElem) {
154: System.out.println("[attribute]: " + attributeElem);
155: try {
156: Field field = cls.getDeclaredField(attributeElem.getName());
157:
158: if (!verifyVisibility(field.getModifiers(), attributeElem
159: .getVisibility())) {
160: appendMessage("Visibility: "
161: + attributeElem.getVisibility());
162: }
163:
164: //System.out.println("type = \"" + field.getGenericType() + "\"");
165:
166: String type1 = attributeElem.getType().getFullName();
167: String type2 = field.getType().getName();
168:
169: if (!type1.equals(type2)) {
170: appendMessage("Type: \"" + type1 + "\" != \"" + type2
171: + "\"");
172: }
173:
174: } catch (Exception e) {
175: e.printStackTrace(System.out);
176: appendMessage("Attribute: \"" + attributeElem
177: + "\" not found!");
178: }
179:
180: }
181:
182: protected void verifyOperation(IOperationElem operationElem) {
183: System.out.println("[operation] : " + operationElem);
184:
185: try {
186:
187: Method[] methods = cls.getDeclaredMethods();
188:
189: boolean flag = false;
190:
191: for (Method m : methods) {
192: //System.out.println("[in] flag = " + flag);
193: System.out.println("[method]: \"" + m + "\"");
194:
195: if (m.getName().equals(operationElem.getName())) {
196: Class[] paramType = m.getParameterTypes();
197: List<IArgumentElem> argList = operationElem
198: .getArgumentList();
199:
200: if (paramType.length == argList.size()) {
201: flag = true;
202:
203: for (int i = 0; i < paramType.length; i++) {
204: String paramName1 = paramType[i].getName();
205: String paramName2 = argList.get(i)
206: .getType().getFullName();
207:
208: System.out.println(" param = \""
209: + paramName1 + "\"");
210: System.out.println(" = \""
211: + paramName2 + "\"");
212:
213: if (!paramName1.equals(paramName2)) {
214: flag = false;
215: break;
216: }
217: }
218:
219: System.out.println("[out] flag = " + flag);
220:
221: if (flag) {
222: String retType1 = m.getReturnType()
223: .getName();
224: String retType2 = operationElem.getType()
225: .getFullName();
226:
227: System.out.println(" return type = \""
228: + retType1 + "\"");
229: System.out.println(" = \""
230: + retType2 + "\"");
231:
232: // Verify Return Type
233: if (retType1.equals(retType2)) {
234: if (verifyVisibility(m.getModifiers(),
235: operationElem.getVisibility())) {
236: return;
237: } else {
238: appendMessage(operationElem
239: .getName()
240: + " "
241: + "Visibility: "
242: + operationElem
243: .getVisibility());
244: }
245: } else {
246: appendMessage(operationElem.getName()
247: + " " + "Return Type: \""
248: + retType1 + "\" != \""
249: + retType2 + "\"");
250: }
251:
252: }
253:
254: }
255: }
256:
257: }
258:
259: //System.out.println("[end] flag = " + flag);
260: if (!flag) {
261: appendMessage("Operation not found: " + operationElem);
262: }
263:
264: } catch (Exception e) {
265: e.printStackTrace(System.out);
266: appendMessage("Operation: " + operationElem);
267: }
268: }
269:
270: private boolean verifyVisibility(int modifier,
271: VisibilityType visibility) {
272: System.out.println("[visibility] " + visibility + " "
273: + modifier);
274:
275: switch (visibility) {
276: case PUBLIC:
277: return Modifier.isPublic(modifier);
278: case PROTECTED:
279: return Modifier.isProtected(modifier);
280: case PRIVATE:
281: return Modifier.isPrivate(modifier);
282: case PACKAGE:
283: return !Modifier.isPrivate(modifier)
284: && !Modifier.isProtected(modifier)
285: && !Modifier.isPublic(modifier);
286: default:
287: return false;
288:
289: }
290:
291: }
292:
293: protected void verifyNestedElem(IJavaElem javaElem) {
294:
295: //boolean flag = false;
296: for (Class c : cls.getClasses()) {
297:
298: String name1 = c.getName();
299: String name2 = javaElem.getFullName();
300:
301: System.out.println("[nested] \"" + name1 + "\"");
302: System.out.println(" \"" + name2 + "\"");
303:
304: if (name1.equals(name2)) {
305: return;
306: }
307: }
308:
309: appendMessage("Nested class not found: "
310: + javaElem.getFullName());
311:
312: }
313:
314: private void showClass(Class cls) {
315:
316: System.out.println("---------------------------------------");
317: System.out.println("[class]");
318: System.out.println("Name: " + cls.getName());
319:
320: System.out.println("Super Class: " + cls.getSuperclass());
321:
322: System.out.println("Super Interfaces: ");
323: for (Class si : cls.getInterfaces()) {
324: System.out.println(si);
325: }
326:
327: System.out.println("Fields:\n");
328: for (Field field : cls.getDeclaredFields()) {
329: System.out.println(field);
330: }
331:
332: System.out.println("Methods:\n");
333: for (Method method : cls.getDeclaredMethods()) {
334: System.out.println(method);
335: }
336:
337: //Class[] nested = cls.getClasses();
338:
339: for (Class c : cls.getClasses()) {
340: System.out.println(" nested: " + c.getName());
341: }
342:
343: System.out.println("---------------------------------------");
344:
345: }
346:
347: }
|