|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.jmi.javamodel.getters; import java.io.PrintStream; import java.lang.reflect.Modifier; import java.util.Collections; import java.util.Iterator; import java.util.List; import junit.textui.TestRunner; import org.netbeans.jmi.javamodel.*; import org.netbeans.jmi.javamodel.codegen.Utility; import org.netbeans.junit.NbTestCase; import org.netbeans.junit.NbTestSuite; import org.netbeans.modules.javacore.ClassIndex; import org.netbeans.modules.javacore.internalapi.JavaMetamodel; /** * Test contains two major parts. It tries to resolve hashCode and equals * method in both - rt.jar and src.zip. Then, it checks signature of this * both methods. * * @author Pavel Flaska */ public class StringMethodsSigTest extends NbTestCase { JavaClass clazz; JavaModelPackage pkg; JavaMetamodel model; /** Creates a new instance of StringMethodsSigTest */ public StringMethodsSigTest() { super("StringMethodsSigTest"); } protected void setUp() { clazz = (JavaClass) Utility.findClass("examples.colorpicker.ColorPicker"); pkg = (JavaModelPackage) clazz.refImmediatePackage(); model = JavaMetamodel.getManager(); } public static NbTestSuite suite() { NbTestSuite suite = new NbTestSuite(StringMethodsSigTest.class); return suite; } /** * Tries to find String source file in src.zip, resolves it and check if * correct equals and hashCode methods are resolved. */ public void testMethodsFromSources() { boolean fail = true; Utility.beginTrans(true); try { model.setClassPath(model.getFileObject(clazz.getResource()), true); doTest(); fail = false; } finally { Utility.endTrans(fail); } } public void testMethodsFromSources2() { boolean fail = true; Utility.beginTrans(true); try { model.setClassPath(model.getFileObject(clazz.getResource()), true); doTest2(); fail = false; } finally { Utility.endTrans(fail); } } /** * Tries to find String class in rt.jar, resolves it and check if * correct equals and hashCode methods are resolved. */ public void testMethodsFromClasses() { boolean fail = true; Utility.beginTrans(true); try { model.setClassPath(model.getFileObject(clazz.getResource())); doTest(); fail = false; } finally { Utility.endTrans(fail); } } public void testMethodsFromClasses2() { boolean fail = true; Utility.beginTrans(true); try { model.setClassPath(model.getFileObject(clazz.getResource())); doTest2(); fail = false; } finally { Utility.endTrans(fail); } } /** * @param args the command line arguments */ public static void main(String[] args) { TestRunner.run(suite()); } //////////////////////////////////////////////////////////////////////////// // PRIVATE METHODS //////////////////////////////////////////////////////////////////////////// private void doTest() { PrintStream log = getLog(); JavaClass strType = (JavaClass) model.getDefaultExtent().getType().resolve("java.lang.String"); Resource res = strType.getResource(); log.println("doTest: Found in resource '" + res.getName() + "'."); Method method = strType.getMethod("hashCode", Collections.EMPTY_LIST, false); assertNotNull(method); log.println("\tMethod '" + method.getName() + "' found."); log.println("\t\tisFinal: '" + Modifier.isFinal(method.getModifiers()) + "'."); assertEquals(true, Modifier.isFinal(method.getModifiers())); Type objectType = model.getDefaultExtent().getType().resolve("java.lang.Object"); method = strType.getMethod("equals", Collections.singletonList(objectType), false); log.println("\tMethod '" + method.getName() + "' found."); assertNotNull(method); } private void doTest2() { PrintStream log = getLog(); JavaClass strType = (JavaClass) model.getDefaultExtent().getType().resolve("java.lang.String"); ParameterizedType parType = model.getDefaultExtent().getParameterizedType().resolveParameterizedType(strType, null, null); Resource res = strType.getResource(); log.println("doTest2: Found in resource '" + res.getName() + "'."); Method method = findMethod(parType, "hashCode"); assertNotNull(method); log.println("\tMethod '" + method.getName() + "' found."); log.println("\t\tisFinal: '" + Modifier.isFinal(method.getModifiers()) + "'."); assertEquals(true, Modifier.isFinal(method.getModifiers())); Type objectType = model.getDefaultExtent().getType().resolve("java.lang.Object"); method = findMethod(parType, "equals"); log.println("\tMethod '" + method.getName() + "' found."); assertNotNull(method); } private Method findMethod(ParameterizedType type, String name) { List features = type.getFeatures(); for (Iterator it = features.iterator(); it.hasNext(); ) { Feature feature = (Feature) it.next(); if (name.equals(feature.getName()) && (feature instanceof Method)) { return (Method) feature; } } return null; } } |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.