Skip to main content
summaryrefslogtreecommitdiffstats
blob: 444fad2c3afd827c109e19fb0adcf5a71b915bc9 (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
/*******************************************************************************
 * Copyright (c) 2001, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jem.internal.beaninfo.adapters;
/*
 *  $RCSfile: BeaninfoJavaReflectionKeyExtension.java,v $
 *  $Revision: 1.5 $  $Date: 2005/02/15 22:44:20 $ 
 */

import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.xmi.XMIResource;

import org.eclipse.jem.java.JavaClass;
import org.eclipse.jem.internal.java.adapters.IJavaReflectionKeyExtension;
import org.eclipse.jem.internal.java.adapters.JavaReflectionKey;

/**
 * Java Reflection Key extension to retrieve keys for beaninfo creations.
 * 
 * Handles attributes and behaviors.
 * 
 * @version 	1.0
 * @author R. L. Kulp  
 */
public class BeaninfoJavaReflectionKeyExtension implements IJavaReflectionKeyExtension {
	
	// The format of the keys are:
	// behaviors:  "classname/behavior/behaviorname"
	// structural features: "classname/featurename"
	public static final String 
		BEHAVIOR = "/operation/",	// Piece in key that indicates this is a behavior. //$NON-NLS-1$
		EVENT = "/event/",		// Piece in key that indicates this is an event. //$NON-NLS-1$
		FEATURE = "/";			// Piece in key that indicates this is an attribute. //$NON-NLS-1$
	
	/**
	 * Get the object for this key.
	 */
	public Object getObject(String key, JavaReflectionKey reflectionKey) {
		if (key != null) {
			int ibehave = key.indexOf(BEHAVIOR);
			if (ibehave > -1) {
				// We have a possible behavior.
				String classname = key.substring(0, ibehave);
				int ibehaveName = ibehave+BEHAVIOR.length();
				if (ibehaveName < key.length()) {
					JavaClass javaclass = getJavaClass(reflectionKey, classname);
					if (javaclass != null) {
						javaclass.getEOperations();	// Get the list introspected and populated if not already.
						return reflectionKey.primGet(key);	// It should now be there, 
					}
				}
			} else {
				int ievent = key.indexOf(EVENT);
				if (ievent > -1) {
					// We have a possible event.
					String classname = key.substring(0, ievent);
					int ieventName = ievent+EVENT.length();
					if (ieventName < key.length()) {
						JavaClass javaclass = getJavaClass(reflectionKey, classname);
						if (javaclass != null) {
							javaclass.getEvents();	// Get the list introspected and populated if not already.
							return reflectionKey.primGet(key);	// It should now be there, 
						}
					}
				} else {
					int ifeature = key.indexOf(FEATURE);
					if (ifeature > -1) {
						// We have a possible feature.
						String classname = key.substring(0, ifeature);
						int ifeatureName = ifeature+FEATURE.length();
						if (ifeatureName < key.length()) {
							String feature = key.substring(ifeatureName);
							JavaClass javaclass = getJavaClass(reflectionKey, classname);
							if (javaclass != null) {
								// This is just a hack until we can get ID situation straightened out.
								// Need to cause introspection of the attributes and references.
								javaclass.getEStructuralFeatures();
								Object result = reflectionKey.primGet(key);	// See if it now got added as an id.
								if (result == null) {
									// Well, it could of been added by the diff merge, which as of now can't handle ids.
									// So we will find it within the attributes/references.
									result = findFeature(feature, javaclass.getEReferences());
									if (result == null)
										result = findFeature(feature, javaclass.getEAttributes());									
									if (result instanceof EStructuralFeature) {
										// Need to add it as an id so next time won't come through here.
										((XMIResource) javaclass.eResource()).setID((EObject) result, key);	// So next time it will find it directly.
									}
								}
								return result;
							}
						}
					}
				}
			}
		}
						
		return null;
	}

	private EStructuralFeature findFeature(String featureName, List featureList) {
		Iterator itr = featureList.iterator();		
		while (itr.hasNext()) {
			EStructuralFeature feature = (EStructuralFeature) itr.next();
			if (featureName.equals(feature.getName())) {
				return feature;
			}
		}
		return null;
	}

	protected JavaClass getJavaClass(JavaReflectionKey reflectionKey, String classname) {
		Object eclass = reflectionKey.primGet(classname);
		if (eclass == null) 
			eclass = reflectionKey.getJavaType(classname);	// Let it create it.
		if (eclass instanceof JavaClass)
			return (JavaClass) eclass;
		else
			return null;
	}

}

Back to the top