KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > bridge > MethodsCollection


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.java.bridge;
21
22 // import java.beans.PropertyChangeEvent;
23
import java.util.*;
24
25 import org.openide.src.*;
26
27 import javax.jmi.reflect.RefObject;
28
29 import org.netbeans.jmi.javamodel.Method;
30 import org.netbeans.jmi.javamodel.Parameter;
31 import org.netbeans.jmi.javamodel.TypeReference;
32
33 class MethodsCollection extends ObjectsCollection {
34
35     static final MethodElement[] EMPTY = new MethodElement[0];
36
37     public MethodsCollection(FeaturesCollection members) {
38         super (members);
39     }
40
41     public RefObject createFeature(RefObject parent, Element elem) {
42         Method res = members.createMethod ((MethodElement) elem);
43         // res.setDeclaringClass ((JavaClass) parent);
44
return res;
45     }
46
47     public Element [] getEmptyArray () {
48         return EMPTY;
49     }
50     
51     public String JavaDoc getPropertyName () {
52         return ElementProperties.PROP_METHODS;
53     }
54     
55     public boolean isOfType (RefObject feature) {
56         return feature instanceof Method;
57     }
58     
59     public Element createElement (RefObject method) {
60         return (MethodElement) members.model.createMethod (members.getParentClass (), (Method)method).getElement ();
61     }
62  
63     public MethodElement getMethod(Identifier name, Type[] argtypes) {
64         members.repository.beginTrans(false);
65         try {
66             if (isValid()) {
67                 String JavaDoc methodName = name.getName();
68                 if (argtypes == null)
69                     argtypes = ObjectsCollection.NO_TYPES;
70                 List args = members.typesToDescriptors (argtypes);
71                 Method method = members.javaClass.getMethod (methodName, args, false);
72                 return method == null ? null : (MethodElement)cachedElement (method);
73             } else {
74                 return null;
75             }
76         } finally {
77             members.repository.endTrans();
78         }
79     }
80     
81     public MethodElement [] getMethods() {
82         return (MethodElement []) getElements ();
83     }
84     
85     public boolean matches (Element elem, RefObject f) {
86         Method method = (Method) f;
87         MethodElement methodElem = (MethodElement) elem;
88         
89         // check name
90
if (!methodElem.getName ().getName ().equals (method.getName ()))
91             return false;
92         
93         // check return type
94
Type returnType2 = methodElem.getReturn ();
95         TypeReference returnType = method.getTypeName();
96         if (returnType == null) {
97             if (returnType2 != null)
98                 return false;
99         } else if (!members.parentImpl.typeReferenceToType (returnType).equals (returnType2))
100             return false;
101         
102         // check parameters
103
List params = method.getParameters();
104         MethodParameter [] params2 = methodElem.getParameters ();
105         if (params.size () != params2.length)
106             return false;
107         Iterator iter = params.iterator ();
108         for (int x = 0; x < params2.length; x++) {
109             TypeReference typeRef = ((Parameter) iter.next ()).getTypeName ();
110             Type type2 = params2 [x].getType ();
111             if (typeRef == null) {
112                 if (type2 != null)
113                     return false;
114             } else if (!members.parentImpl.typeReferenceToType (typeRef).equals (type2))
115                 return false;
116         }
117         
118         return true;
119     }
120     
121     public int getPositionalValue () {
122         return ObjectsCollection.POS_VAL_METHOD;
123     }
124 }
125
Popular Tags