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-2006 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: package org.netbeans.modules.beans.beaninfo;
043:
044: import java.lang.reflect.Modifier;
045: import java.util.HashSet;
046: import java.util.List;
047: import java.util.Iterator;
048: import java.util.ArrayList;
049: import java.util.Collections;
050:
051: import org.netbeans.modules.beans.JMIUtils;
052: import org.netbeans.modules.beans.GenerateBeanException;
053: import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
054: import org.netbeans.jmi.javamodel.*;
055:
056: import javax.jmi.reflect.JmiException;
057:
058: /** Singleton - utility class
059:
060:
061: @author Petr Hrebejk
062: */
063: final class BiSuperClass extends Object {
064:
065: /** Creates a ClassElement containing all methods from classElement and it's superclasses */
066:
067: static JavaClass createForClassElement(JavaClass classElement)
068: throws GenerateBeanException {
069: assert JMIUtils.isInsideTrans();
070: try {
071: JavaModelPackage jmodel = JavaMetamodel.getManager()
072: .getJavaExtent(classElement);
073: JavaClass result = jmodel.getJavaClass().createJavaClass();
074:
075: result.setName(classElement.getName());
076:
077: JavaClass ce = classElement;
078: int methodsAdded = 0; // Workaround for getMethd
079:
080: HashSet visited = new HashSet();
081: while (ce != null && visited.add(ce)
082: && !"java.lang.Object".equals(ce.getName())) { // NOI18N
083: List/*<Method>*/methods = JMIUtils.getMethods(ce);
084: for (Iterator it = methods.iterator(); it.hasNext();) {
085: Method method = (Method) it.next();
086: if ((method.getModifiers() & Modifier.PUBLIC) == 0)
087: continue;
088:
089: if (methodsAdded == 0
090: || result.getMethod(method.getName(),
091: getParameterTypes(method), false) == null) {
092: Method methodToAdd = duplicateMethod(method,
093: jmodel);
094: result.getFeatures().add(methodToAdd);
095: methodsAdded++;
096: }
097: }
098:
099: ce = ce.getSuperClass();
100: }
101:
102: /*
103: MethodElement[] methods = result.getMethods();
104: for( int i = 0; i < methods.length; i++ )
105: System.out.println ( methods[i].getName() );
106: */
107: return result;
108: } catch (JmiException e) {
109: throw new GenerateBeanException(e);
110: }
111: }
112:
113: /**
114: * makes shallow copy of method in particular extent.
115: * @param orig original method
116: * @param jmodel extent where the copy is created
117: * @return new method
118: * @throws JmiException
119: */
120: private static Method duplicateMethod(Method orig,
121: JavaModelPackage jmodel) throws JmiException {
122: Method m = jmodel.getMethod().createMethod(orig.getName(),
123: null, orig.getModifiers(), null, null, null, null,
124: duplicateTypeParameters(orig, jmodel),
125: duplicateParameters(orig, jmodel),
126: duplicateExceptionNames(orig, jmodel), null,
127: orig.getDimCount());
128: m.setType(orig.getType());
129: return m;
130: }
131:
132: private static List duplicateTypeParameters(Method orig,
133: JavaModelPackage jmodel) {
134: List tparams = orig.getTypeParameters();
135: if (tparams == null) {
136: return null;
137: }
138:
139: List duplicates = new ArrayList(tparams.size());
140: for (Iterator it = tparams.iterator(); it.hasNext();) {
141: TypeParameter typeParameter = (TypeParameter) it.next();
142: TypeParameter duplicate = duplicateTypeParameter(
143: typeParameter, jmodel);
144: duplicates.add(duplicate);
145: }
146: return duplicates;
147: }
148:
149: private static TypeParameter duplicateTypeParameter(
150: TypeParameter typeParameter, JavaModelPackage jmodel) {
151: TypeParameter duplicate = jmodel.getTypeParameter()
152: .createTypeParameter();
153: duplicate.setName(typeParameter.getName());
154: duplicate.setSuperClass(typeParameter.getSuperClass());
155: List/*<JavaClass>*/bounds = typeParameter.getInterfaces();
156: List/*<JavaClass>*/copyOfBounds = new ArrayList(bounds.size());
157: for (Iterator it = bounds.iterator(); it.hasNext();) {
158: Object bound = it.next();
159: if (bound instanceof TypeParameter) {
160: copyOfBounds.add(duplicateTypeParameter(
161: (TypeParameter) bound, jmodel));
162: } else {
163: copyOfBounds.add(bound);
164: }
165: }
166: duplicate.getInterfaces().addAll(copyOfBounds);
167: return duplicate;
168: }
169:
170: /**
171: * makes shallow copy of parameters in particular extent.
172: * @param orig method with original list of parameters
173: * @param jmodel extent where the copy is created
174: * @return list of parameters
175: * @throws JmiException
176: */
177: private static List duplicateParameters(Method orig,
178: JavaModelPackage jmodel) throws JmiException {
179: List parameters = orig.getParameters();
180: if (parameters == null) {
181: return null;
182: }
183: List duplicates = new ArrayList(parameters.size());
184: for (Iterator it = parameters.iterator(); it.hasNext();) {
185: Parameter parameter = (Parameter) it.next();
186: Parameter duplicate = jmodel.getParameter()
187: .createParameter(parameter.getName(), null,
188: parameter.isFinal(), null,
189: parameter.getDimCount(),
190: parameter.isVarArg());
191: duplicate.setType(parameter.getType());
192: duplicates.add(duplicate);
193: }
194: return duplicates;
195: }
196:
197: private static List/*<Type>*/getParameterTypes(Method method)
198: throws JmiException {
199: List/*<Parameter>*/params = method.getParameters();
200: if (params == null) {
201: return Collections.EMPTY_LIST;
202: }
203: List types = new ArrayList(params.size());
204: for (Iterator it = params.iterator(); it.hasNext();) {
205: Parameter param = (Parameter) it.next();
206: types.add(param.getType());
207: }
208: return types;
209: }
210:
211: private static List/*<MultipartId>*/duplicateExceptionNames(
212: Method orig, JavaModelPackage jmodel) {
213: List/*<JavaClass>*/exIds = orig.getExceptions();
214:
215: List/*<MultipartId>*/duplicates = new ArrayList/*<MultipartId>*/(
216: exIds.size());
217: for (Iterator it = exIds.iterator(); it.hasNext();) {
218: JavaClass ex = (JavaClass) it.next();
219: MultipartId duplicate = jmodel.getMultipartId()
220: .createMultipartId(ex.getName(), null, null);
221: duplicates.add(duplicate);
222: }
223: return duplicates;
224: }
225:
226: }
|