Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: a5c27c08fe7319e7008f6a95a92825009f6e548e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package org.eclipse.jem.internal.java.adapters.jdk;
/*******************************************************************************
 * Copyright (c) 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
/*
 *  $RCSfile: JavaMethodJDKAdaptor.java,v $
 *  $Revision: 1.1 $  $Date: 2003/10/27 17:12:30 $ 
 */

import java.util.List;

import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.xmi.XMIResource;
import org.eclipse.jem.internal.java.*;
import org.eclipse.jem.internal.java.adapters.ReadAdaptor;
import org.eclipse.jem.internal.java.impl.MethodImpl;
/**
 * Insert the type's description here.
 * Creation date: (6/6/2000 4:42:50 PM)
 * @author: Administrator
 */
public class JavaMethodJDKAdaptor extends JDKAdaptor {
	protected java.lang.reflect.AccessibleObject sourceAccessible = null; // Could be method or ctor.
	protected Class parentType = null;
	// cache a static empty Class[] for no parm methods
	protected static Class[] emptyClassArray = new Class[0];
	// cache a static empty String[], too
	protected static String[] emptyStringArray = new String[0];
	public JavaMethodJDKAdaptor(Notifier target, JavaJDKAdapterFactory anAdapterFactory) {
		super(target, anAdapterFactory);
	}
	/**
	 * addExceptions - reflect our exception list
	 */
	protected void addExceptions() {
		Class[] exceptions =
			(getSourceAccessible() instanceof java.lang.reflect.Method)
				? ((java.lang.reflect.Method) getSourceAccessible()).getExceptionTypes()
				: ((java.lang.reflect.Constructor) getSourceAccessible()).getExceptionTypes();
		//	EList exList = (EList) javaMethodTarget.primRefValue(JavaRefPackage.eINSTANCE.getMethod_JavaExceptions());
		List exList = getMethodTarget().getJavaExceptionsGen();
		for (int i = 0; i < exceptions.length; i++) {
			exList.add(createJavaClassRef(exceptions[i].getName()));
		}
	}

	protected MethodImpl getMethodTarget() {
		return (MethodImpl) getTarget();
	}
	/**
	 * addParameters - reflect our parms
	 */
	protected void addParameters() {
		Class[] parmTypes =
			(getSourceAccessible() instanceof java.lang.reflect.Method)
				? ((java.lang.reflect.Method) getSourceAccessible()).getParameterTypes()
				: ((java.lang.reflect.Constructor) getSourceAccessible()).getParameterTypes();
		MethodImpl javaMethodTarget = getMethodTarget();
		// 	List pList = (List) javaMethodTarget.primRefValue(JavaRefPackage.eINSTANCE.getMethod_Parameters());
		List pList = javaMethodTarget.getParametersGen();
		for (int i = 0; i < parmTypes.length; i++) {
			pList.add(createJavaParameter(javaMethodTarget, "arg" + i, getTypeName(parmTypes[i]))); //$NON-NLS-1$
		}
	}
	protected JavaClass getContainingJavaClass() {
		return ((Method) getTarget()).getContainingJavaClass();
	}
	/**
	 * getParentType - return the Class which corresponds to our parent JavaClass
	 * we're going to do this a lot, so cache it.
	 */
	protected Class getParentType() {
		if (parentType == null) {
			Method targetMethod = (Method) getTarget();
			JavaClass parentJavaClass = targetMethod.getContainingJavaClass();
			JavaClassJDKAdaptor pa = (JavaClassJDKAdaptor) EcoreUtil.getAdapter(parentJavaClass.eAdapters(), ReadAdaptor.TYPE_KEY);
			if (pa != null)
				parentType = pa.getSourceType();
		}
		return parentType;
	}
	/**
	 * getParmTypeSignatures - return an array of Classes for our parameter types
	 * 	For reflection purposes, we can only rely on our ID, since our parms may
	 *  not yet be known.
	 */
	protected Class[] getParmTypes() {
		Method javaMethodTarget = (Method) getTarget();
		String id = ((XMIResource) javaMethodTarget.eResource()).getID(javaMethodTarget);
		String[] typeNames = getTypeNamesFromMethodID(id);
		if (typeNames == null)
			return emptyClassArray;
		int n = typeNames.length;
		if (n == 0)
			return emptyClassArray;
		Class[] types = new Class[n];
		for (int i = 0; i < n; ++i) {
			types[i] = getType(typeNames[i]);
		}
		return types;
	}
	public Object getReflectionSource() {
		return getSourceAccessible();
	}
	/**
	 * getsourceMethod - return the java.lang.reflect.Method which describes our implementing method
	 */
	protected java.lang.reflect.AccessibleObject getSourceAccessible() {
		if (sourceAccessible == null) {
			Class parent = this.getParentType();
			if (parent != null) {
				Class[] parmTypes = this.getParmTypes();
				try {
					sourceAccessible = parent.getDeclaredMethod(((Method) getTarget()).getName(), parmTypes);
				} catch (NoSuchMethodException e) {
					// OK, can't reflect it
				}

				if (sourceAccessible == null) {
					// It wasn't a method, try for constructor.
					try {
						sourceAccessible = parent.getDeclaredConstructor(parmTypes);
					} catch (NoSuchMethodException e) {
						// OK, can't reflect it
					}
				}
			}
		}
		return sourceAccessible;
	}
	/**
	 * getValueIn method comment.
	 */
	public Object getValueIn(EObject object, EObject attribute) {
		// At this point, this adapter does not dynamically compute any values,
		// all values are pushed back into the target on the initial call.
		return super.getValueIn(object, attribute);
	}
	/**
	 * reflectValues - template method, subclasses override to pump values into target.
	 * on entry: UUID, name, containing package (and qualified name), and document must be set.
	 * Method adaptor:
	 *	- set modifiers
	 *	- set name
	 * 	- set return type
	 * 	- add parameters
	 * 	- add exceptions
	 */
	public boolean reflectValues() {
		if (getSourceAccessible() != null) {
			setModifiers();
			setNaming();
			setReturnType();
			addParameters();
			addExceptions();
			return true;
		}
		return false;
	}
	/**
	 * setModifiers - set the attribute values related to modifiers here
	 */
	protected void setModifiers() {
		Method methodTarget = (Method) getTarget();
		int modifiers =
			(getSourceAccessible() instanceof java.lang.reflect.Method)
				? ((java.lang.reflect.Method) getSourceAccessible()).getModifiers()
				: ((java.lang.reflect.Constructor) getSourceAccessible()).getModifiers();
		methodTarget.setAbstract(java.lang.reflect.Modifier.isAbstract(modifiers));
		methodTarget.setFinal(java.lang.reflect.Modifier.isFinal(modifiers));
		methodTarget.setNative(java.lang.reflect.Modifier.isNative(modifiers));
		methodTarget.setStatic(java.lang.reflect.Modifier.isStatic(modifiers));
		methodTarget.setSynchronized(java.lang.reflect.Modifier.isSynchronized(modifiers));
		methodTarget.setConstructor(getSourceAccessible() instanceof java.lang.reflect.Constructor);
		// Set visibility
		JavaClass javaClass = getContainingJavaClass();
		if ((javaClass.getKind() == TypeKind.INTERFACE_LITERAL) || (java.lang.reflect.Modifier.isPublic(modifiers)))
			methodTarget.setJavaVisibility(JavaVisibilityKind.PUBLIC_LITERAL);
		else if (java.lang.reflect.Modifier.isPrivate(modifiers))
			methodTarget.setJavaVisibility(JavaVisibilityKind.PRIVATE_LITERAL);
		else if (java.lang.reflect.Modifier.isProtected(modifiers))
			methodTarget.setJavaVisibility(JavaVisibilityKind.PROTECTED_LITERAL);
		else
			//Visibility must be package
			methodTarget.setJavaVisibility(JavaVisibilityKind.PACKAGE_LITERAL);
	}
	/**
	 * setNaming - set the naming values here
	 * 	- qualified name must be set first, that is the path to the real Java class
	 *	- ID
	 * 	- name-based UUID
	 */
	protected void setNaming() {
		//
		//	naming is currently a no-op since the name and UUID must be set prior to reflection
		//	...and ID is redundant with UUID.
		//	javaFieldTarget.setID(parent.getQualifiedName() + "_" + javaFieldTarget.getName());
	}
	/**
	 * setType - set our return type here
	 */
	protected void setReturnType() {
		if (getSourceAccessible() instanceof java.lang.reflect.Method) {
			Class type = ((java.lang.reflect.Method) getSourceAccessible()).getReturnType();
			Method javaMethodTarget = (Method) getTarget();
			/*
					JavaParameter newParameter = createJavaParameter(javaMethodTarget, "result", getTypeName(type));//$NON-NLS-1$
					newParameter.setParameterKind(MetaJavaParameterKind.RETURN);
					javaMethodTarget.getParameters().add(newParameter);
			*/
			javaMethodTarget.setEType(createJavaClassRef(getTypeName(type)));
		}
	}
}

Back to the top