Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4d9ffb3258459c251c799b4ff12dbcd561641f5c (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
/*******************************************************************************
 * Copyright (c) 2001, 2006 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.adapters.jdom;
/*


 */

import java.util.*;
import java.util.logging.Level;

import org.eclipse.core.resources.IResource;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.xmi.XMIResource;
import org.eclipse.jdt.core.*;


import org.eclipse.jem.internal.java.adapters.*;
import org.eclipse.jem.internal.java.adapters.nls.ResourceHandler;
import org.eclipse.jem.internal.plugin.JavaPlugin;
import org.eclipse.jem.java.*;
import org.eclipse.jem.java.internal.impl.JavaClassImpl;
import org.eclipse.jem.util.TimerTests;
import org.eclipse.jem.util.UIContextDetermination;
import org.eclipse.jem.util.logger.proxy.Logger;


public class JavaClassJDOMAdaptor extends JDOMAdaptor implements IJavaClassAdaptor {
	private static final String OBJECT_TYPE_NAME = "java.lang.Object"; //$NON-NLS-1$

	/*
	 * Step ids used for TimerTests of performance testing.
	 */
	public static final String REFLECT_CLASS = "Reflect JDOM Class"; //$NON-NLS-1$
	public static final String REFLECT_METHODS = "Reflect all JDOM methods for a class"; //$NON-NLS-1$
	public static final String REFLECT_FIELDS = "Reflect all JDOM fields for a class"; //$NON-NLS-1$
	
	
	protected IType sourceType = null;
	protected JavaReflectionAdapterFactory adapterFactory;
	private Map typeResolutionCache = new HashMap(25);
	private boolean hasReflectedFields, isReflectingFields;
	private boolean hasReflectedMethods, isReflectingMethods;
	
	public JavaClassJDOMAdaptor(Notifier target, IJavaProject workingProject, JavaReflectionAdapterFactory inFactory) {
		super(target, workingProject);
		setAdapterFactory(inFactory);
	}
	
	private Map existingFields = new HashMap(); 
	/*
	 * addFields - reflect our fields
	 */
	protected boolean addFields() {

		// The algorithm we will use is:
		// 1) Pass through the IField's of this class
		//    a) If it is in existingFields, then add to newExisting the entry from
		//       oldExisting (deleting from oldExisting at the same time), and flush the field. This is so next we re-get any changed parts of it.
		//    b) else not existing, then create new field and add to the new fields list.
		// 2) Remove from the fields list any still left in oldExisting. These are ones that no longer exist.
		// 3) Add all of the news ones to the fields.
		//       
		IField[] fields = null;
		try {
			fields = getSourceType().getFields();
		} catch (JavaModelException e) {
			Logger.getLogger().log(e, Level.WARNING);
			return false;	
		}
		XMIResource resource = (XMIResource) getJavaClassTarget().eResource();		
		Field field = null;
		JavaFieldJDOMAdaptor adapter = null;
		Map newExisting = new HashMap(fields.length);
		List newFields = new ArrayList();
		for (int i = 0; i < fields.length; i++) {
			IField ifield = fields[i];
			field = (Field) existingFields.remove(ifield);	// Get the existing field (which is the value) from the collection keyed by IField.
			if (field != null) {
				// It is an existing method. So just put over to newExisting. Then flush it.
				newExisting.put(ifield, field);
				// Since this is a new method, it is not attached to a resource, so we need to explicitly create the adapter.
				adapter = (JavaFieldJDOMAdaptor) EcoreUtil.getExistingAdapter(field, ReadAdaptor.TYPE_KEY);
				if (adapter == null)
					adapter = (JavaFieldJDOMAdaptor) getAdapterFactory().adaptNew(field, ReadAdaptor.TYPE_KEY);
				else
					adapter.flushReflectedValuesIfNecessaryNoNotification(true);
				adapter.setSourceField(ifield);	// Give it this new IField
			} else {
				// It is a new method. Create the new method, add to newExisting, and add to newMethods list.
				field = createJavaField(ifield, resource);
				newExisting.put(ifield, field);				
				newFields.add(field);
				adapter = (JavaFieldJDOMAdaptor) getAdapterFactory().adaptNew(field, ReadAdaptor.TYPE_KEY);
				if (adapter != null)
					adapter.setSourceField(ifield);
			}
		}
		
		BasicEList fieldsList = (BasicEList) getJavaClassTarget().getFieldsGen();
		if (!existingFields.isEmpty()) {
			// Now any still left in old existing are deleted. So we make them proxies and then remove them from fields list.			
			URI baseURI = resource.getURI();
			Collection toDelete = existingFields.values();
			for (Iterator itr = toDelete.iterator(); itr.hasNext();) {
				InternalEObject m = (InternalEObject) itr.next();
				String id = resource.getID(m);
				if (id != null)
					m.eSetProxyURI(baseURI.appendFragment(id));
			}
			fieldsList.removeAll(toDelete);
		}
		
		if (!newFields.isEmpty()) {
			// Now add in the news ones
			fieldsList.addAllUnique(newFields);
		}
		
		// Finally set current existing to the new map we created.
		existingFields = newExisting;
		return true;			
	}
	
	private Map existingMethods = new HashMap(); 
	/*
	 * addMethods - reflect our methods. Merge in with the previous.
	 */
	protected boolean addMethods() {
		// The algorithm we will use is:
		// 1) Pass through the IMethod's of this class
		//    a) If it is in existingMethods, then add to newExisting the entry from
		//       oldExisting (deleting from oldExisting at the same time), and flush the method. This is so next we re-get any changed parts of it.
		//    b) else not existing, then create new method and add to the new methods list.
		// 2) Remove from the methods list any still left in oldExisting. These are ones that no longer exist.
		// 3) Add all of the news ones to the methods.
		//       
		IMethod[] methods = null;
		try {
			methods = getSourceType().getMethods();
		} catch (JavaModelException e) {
			Logger.getLogger().log(e, Level.WARNING);
			return false;	
		}
		XMIResource resource = (XMIResource) getJavaClassTarget().eResource();		
		Method method = null;
		JavaMethodJDOMAdaptor adapter = null;
		Map newExisting = new HashMap(methods.length);
		List newMethods = new ArrayList();
		for (int i = 0; i < methods.length; i++) {
			IMethod im = methods[i];
			method = (Method) existingMethods.remove(im);	// Get the existing method (which is the value) from the collection keyed by IMethod.
			if (method != null) {
				// It is an existing method. So just put over to newExisting. Then flush it.
				newExisting.put(im, method);
				adapter = (JavaMethodJDOMAdaptor) retrieveAdaptorFrom(method);
				if (adapter != null) {
					adapter.flushReflectedValuesIfNecessaryNoNotification(true);
					adapter.setSourceMethod(im);	// Give it this new IMethod
				}
			} else {
				// It is a new method. Create the new method, add to newExisting, and add to newMethods list.
				method = createJavaMethod(im, resource);
				newExisting.put(im, method);				
				newMethods.add(method);
				// Since this is a new method, it is not attached to a resource, so we need to explicitly create the adapter.
				adapter = (JavaMethodJDOMAdaptor) getAdapterFactory().adaptNew(method, ReadAdaptor.TYPE_KEY);
				if (adapter != null)
					adapter.setSourceMethod(methods[i]);
			}
		}
		
		BasicEList methodsList = (BasicEList) getJavaClassTarget().getMethodsGen();
		if (!existingMethods.isEmpty()) {
			// Now any still left in old existing are deleted. So we make them proxies and then remove them from methods list.
			URI baseURI = resource.getURI();
			Collection toDelete = existingMethods.values();
			for (Iterator itr = toDelete.iterator(); itr.hasNext();) {
				InternalEObject m = (InternalEObject) itr.next();
				String id = resource.getID(m);
				if (id != null)
					m.eSetProxyURI(baseURI.appendFragment(id));
			}
			methodsList.removeAll(toDelete);
		}
		
		if (!newMethods.isEmpty()) {
			// Now add in the news ones
			methodsList.addAllUnique(newMethods);
		}
		
		// Finally set current existing to the new map we created.
		existingMethods = newExisting;
		return true;
	}
	/**
	 * Clear source Type ;
	 */
	protected void clearSource() {
		sourceType = null;
	}

	/**
	 * Clear the reflected fields list.
	 */
	protected boolean flushFields() {
		// First turn them all into proxies so that any holders will re-resolve to maybe the new one if class comes back.
		existingFields.clear();
		XMIResource res = (XMIResource) getJavaClassTarget().eResource();
		URI baseURI = res.getURI();
		List fields = getJavaClassTarget().getFieldsGen();
		int msize = fields.size();
		for (int i = 0; i < msize; i++) {
			InternalEObject f = (InternalEObject) fields.get(i);
			String id = res.getID(f);
			if (id != null)
				f.eSetProxyURI(baseURI.appendFragment(id));
		}
		fields.clear();	// Now we can clear it.
		return true;
		}
	/**
	 * Clear the implements list.
	 */
	protected boolean flushImplements() {
		getJavaClassTarget().getImplementsInterfacesGen().clear();
		return true;
	}
	/**
	 * Clear the reflected methods list.
	 */
	protected boolean flushMethods() {
		// First turn them all into proxies so that any holders will re-resolve to maybe the new one if class comes back.
		existingMethods.clear();
		XMIResource res = (XMIResource) getJavaClassTarget().eResource();
		URI baseURI = res.getURI();
		List methods = getJavaClassTarget().getMethodsGen();
		int msize = methods.size();
		for (int i = 0; i < msize; i++) {
			InternalEObject m = (InternalEObject) methods.get(i);
			String id = res.getID(m);
			if (id != null)
				m.eSetProxyURI(baseURI.appendFragment(id));
		}
		methods.clear();	// Now we can clear it.
		return true;
	}
	protected boolean flushModifiers() {
		JavaClass javaClassTarget = (JavaClass) getTarget();
		javaClassTarget.setAbstract(false);
		javaClassTarget.setFinal(false);
		javaClassTarget.setPublic(false);
		javaClassTarget.setKind(TypeKind.UNDEFINED_LITERAL);
		return true;
	}
	protected boolean flushInnerClasses() {
		getJavaClassTarget().getDeclaredClassesGen().clear();
		return true;
	}
	
	protected boolean flushAndClearCachedModelObject;
	
	/**
	 * Clear the reflected values.
	 */
	protected boolean flushReflectedValues(boolean clearCachedModelObject) {
		flushAndClearCachedModelObject = clearCachedModelObject;
		return true;
	}
	
	/*
	 * This is called before a reflect if a real flush is needed.
	 */
	private void flushNow() {
		if (flushAndClearCachedModelObject)
			setSourceType(null);
		typeResolutionCache.clear();
		flushModifiers();
		flushSuper();
		flushImplements();
		if (flushAndClearCachedModelObject) {
			// Don't flush these yet. We will try to reuse them on the next reflush. If clear model too, then flush them. This usually means class has been deleted, so why keep them around.
			flushMethods();
			flushFields();
		}
		// Even if we didn't flush the fields/methods, we do need to mark as not reflected so on next usage we will merge in the changes.
		hasReflectedMethods = false;
		hasReflectedFields = false;
		
		flushInnerClasses();
		flushAndClearCachedModelObject = false;
	}


	/**
	 * @see com.ibm.etools.java.adapters.JavaReflectionAdaptor#postFlushReflectedValuesIfNecessary()
	 */
	protected void postFlushReflectedValuesIfNecessary(boolean isExisting) {
		getJavaClassTarget().setReflected(false);
		super.postFlushReflectedValuesIfNecessary(isExisting);
	}

	/**
	 * Set the supertype to be null.
	 */
	protected boolean flushSuper() {
		List targetSupers = getJavaClassTarget().primGetESuperTypes();
		targetSupers.clear();
		return true;
	}
	protected JavaReflectionAdapterFactory getAdapterFactory() {
		return adapterFactory;
	}
	/**
	 * getBinaryType - return the IType which describes our existing Java class file
	 */
	protected IType getBinaryType() {
		return this.getBinaryType(((JavaClass) getTarget()).getQualifiedName());
	}
	/**
	 * Return the target typed to a JavaClass.
	 */
	protected JavaClassImpl getJavaClassTarget() {
		return (JavaClassImpl) getTarget();
	}
	public Object getReflectionSource() {
		return getSourceType();
	}
	
    /* (non-Javadoc)
     * @see org.eclipse.jem.internal.java.adapters.JavaReflectionAdaptor#hasReflectionSource()
     */
    public boolean hasCachedReflectionSource() {
        return sourceType != null;
    }
	/**
	 * getSourceType - return the IType which describes our existing Java class or source file
	 */
	public IType getSourceType() {
		if (sourceType == null || flushAndClearCachedModelObject) {
			JavaClassImpl javaClass = (JavaClassImpl) getTarget();
			sourceType = JDOMSearchHelper.findType(javaClass.getJavaPackage().getName(), javaClass.primGetName(), getSourceProject());
			/*
			 * If the sourceType was found and we are not in the middle of
			 * reflecting it is necessary to ensure that the hasReflected
			 * is set back to false.  If this is not done then the system
			 * assumes that the content in the target model object has been
			 * reflected from this found sourceType which is not the case.
			 */
			if (hasValidReflection())
				flushReflectedValuesIfNecessaryNoNotification(false);
		}
		return sourceType;
	}
	/**
	 * getSourceType - return the IType which describes our existing Java class or source file
	 */
	protected IType getType() {
		return getSourceType();
	}
	protected Map getTypeResolutionCache() {
		return typeResolutionCache;
	}
	/**
	 * 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);
	}
	/**
	 * Return true if the sourceType is null or if
	 * it is a binary type.
	 */
	public boolean isSourceTypeFromBinary() {
		if (getSourceType() == null)
			return false; //must be new?
		return getSourceType().isBinary();
	}


	protected JavaClass reflectJavaClass(String qualifiedName) {
		IType type = JDOMSearchHelper.findType(qualifiedName, true, getSourceProject(), this);
		if (type != null)
			return reflectJavaClass(type);
		else
			return createJavaClassRef(qualifiedName);
	}
	protected JavaClass reflectJavaClass(IType aType) {
		if (aType != null) {
			JavaClass javaClass = (JavaClass) JavaRefFactory.eINSTANCE.reflectType(aType.getFullyQualifiedName(), (EObject) getTarget());
			if (javaClass != null) {
				JavaClassJDOMAdaptor adaptor = (JavaClassJDOMAdaptor) EcoreUtil.getAdapter(javaClass.eAdapters(), ReadAdaptor.TYPE_KEY);
				if (adaptor != null)
					adaptor.setSourceType(aType);
			}
			return javaClass;
		}
		return null;
	}
	/**
	 * reflectValues - template method, subclasses override to pump values into target.
	 * on entry: name, containing package (and qualified name), and document must be set.
	 * Return true always and the JavaReflectionSynchronizer will flush with the type can
	 * be found again.  In headless mode, return true only if the type is found.  This is
	 * needed becauce most headless tasks are done under one operation and the JavaReflectionSynchronizer
	 * may not have a chance to flush a bad reflection before the real type needs to be found and can be found.
	 * JavaClass adaptor:
	 * - set modifiers
	 * - set name
	 * - set reference to super
	 * - create methods
	 * - create fields
	 * - add imports
	 */
	public boolean reflectValues() {
		if (hasFlushed) {
			// We flush sometime in the past since last reflect. So now do the actual flush.
			flushNow();
		}
		super.reflectValues();
		boolean isHeadless = UIContextDetermination.getCurrentContext() == UIContextDetermination.HEADLESS_CONTEXT;
		if (canReflect()) {
			TimerTests.basicTest.startCumulativeStep(REFLECT_CLASS);
			try {
				ICompilationUnit cu = getSourceType().getCompilationUnit();
				boolean isWC = cu != null ? cu.isWorkingCopy() : false;
				IResource res = isWC ? getSourceType().getResource() : null;
				// We are only interested in physical classes. If still just in working copy and not yet put out to
				// disk, we don't should treat as not exist. Anything else is considered existing because we got past
				// getSourceType.exists. This will return the truth for non-wc. But for wc types it will return true,
				// even though not physically on disk (such as just creating it and hadn't saved it yet). So for wc types
				// we need to test the actual resource.
				// Test is OK if not wc, or if wc, then there is a res. and it is accessible.
				if (!isWC || (res != null && res.isAccessible())) {
					setModifiers();
					setNaming();
					try {
						setSuper();
					} catch (InheritanceCycleException e) {
						JavaPlugin.getDefault().getLogger().log(e);
					}
					setImplements();
					reflectInnerClasses();
					setDeclaringClass();
					//addImports();
					if (isHeadless) {
						registerWithFactory();
						return true;
					}
				}
			} finally {
				TimerTests.basicTest.stopCumulativeStep(REFLECT_CLASS);
			}
		}
		if (isHeadless)
			return false;
		else {
			registerWithFactory();
			return true;
		}
	}
	
	protected void setDeclaringClass() {
		IType declaringType = getSourceType().getDeclaringType();
		if (declaringType != null) {
			// Need to get it and reflect it so that the declared type of this target is set correctly. We can just
			// set it ourselves directly because ECore would try to add it to the list of inner classes of the declaring type. This
			// would cause it to be added twice, once from the reflection caused by the inverse setting, and once from our doing
			// the inverse setting itself.
			ResourceSet set = getTargetResource().getResourceSet();
			String packageName = declaringType.getPackageFragment().getElementName();
			JavaClassImpl declaringClass = (JavaClassImpl) JavaRefFactory.eINSTANCE.reflectType(packageName, declaringType.getTypeQualifiedName(), set);
			declaringClass.getDeclaredClasses();	// This will cause it to put us into its list and also set our declaring class to this declaring type.
		}
	}
	
	
	/**
	 * @return
	 */
	private boolean canReflect() {
		return isResourceLoaded() && getSourceProject() != null && getSourceType() != null && getSourceType().exists();
	}
	public synchronized boolean reflectFieldsIfNecessary() {
		if (reflectValuesIfNecessary() && canReflect()) {
			if (!hasReflectedFields && !isReflectingFields) {
				isReflectingFields = true;
				try {
					TimerTests.basicTest.startCumulativeStep(REFLECT_FIELDS);
					addFields();
					hasReflectedFields = true;
				} catch (Exception e) {
					hasReflectedFields = false;
					Logger logger = Logger.getLogger();
					if (logger.isLoggingLevel(Level.WARNING)) {
						logger.log(ResourceHandler.getString("Failed_reflecting_values_ERROR_"), Level.WARNING); //$NON-NLS-1$ = "Failed reflecting values!!!"
						logger.logWarning(e); 
					}					
				} finally {
					isReflectingFields = false;
					TimerTests.basicTest.stopCumulativeStep(REFLECT_FIELDS);
				}
			}
			return hasReflectedFields;
		} else
			return false;	// Couldn't reflect the base values, so couldn't do fields either
	}
	public synchronized boolean reflectMethodsIfNecessary() {
		if (reflectValuesIfNecessary() && canReflect()) {
			if (!hasReflectedMethods && !isReflectingMethods) {
				isReflectingMethods = true;
				try {
					TimerTests.basicTest.startCumulativeStep(REFLECT_METHODS);
					hasReflectedMethods = addMethods();
				} catch (Exception e) {
					hasReflectedMethods = false;
					Logger logger = Logger.getLogger();
					if (logger.isLoggingLevel(Level.WARNING)) {
						logger.log(ResourceHandler.getString("Failed_reflecting_values_ERROR_"), Level.WARNING); //$NON-NLS-1$ = "Failed reflecting values!!!"
						logger.logWarning(e); 
					}					
				} finally {
					isReflectingMethods = false;
					if (!hasReflected)
						flushMethods();	// Something bad happened, so we will do a complete flush to be on safe side.
					TimerTests.basicTest.stopCumulativeStep(REFLECT_METHODS);
				}
			}
			return hasReflectedMethods;
		} else
			return false;	// Couldn't reflect the base values, so couldn't do fields either
	}
	
	private void registerWithFactory() {
		getAdapterFactory().registerReflection(getJavaClassTarget().getQualifiedNameForReflection(), this);
	}

	/**
	 * @see com.ibm.etools.java.adapters.ReflectionAdaptor#notifyChanged(new ENotificationImpl((InternalEObject)Notifier, int,(EStructuralFeature) EObject, Object, Object, int))
	 */
	public void notifyChanged(Notification notification) {
		if (notification.getEventType() == Notification.REMOVING_ADAPTER
			&& notification.getOldValue() == this
			&& notification.getNotifier() == getTarget())
			getAdapterFactory().unregisterReflection(getJavaClassTarget().getQualifiedNameForReflection());

	}

	protected void setAdapterFactory(JavaReflectionAdapterFactory inFactory) {
		adapterFactory = inFactory;
	}
	/**
	 * setImplements - set our implemented/super interfaces here
	 * For an interface, these are superclasses.
	 * For a class, these are implemented interfaces.
	 */
	protected void setImplements() {
		try {
			String[] interfaceNames = getSourceType().getSuperInterfaceNames();
			JavaClass ref;
			// needs work, the names above will be simple names if we are relfecting from a source file
			List list = getJavaClassTarget().getImplementsInterfacesGen();
			for (int i = 0; i < interfaceNames.length; i++) {
				String name = interfaceNames[i];
				// if a class implements an interface that is actually an inner interface, the inner interface
				// package is not resolved correctly without getting the fully qualified name. For example,
				// public class TestBean implements SomeOtherSimpleClass.SomeInterface
				// without getting the fully qualified name, we are using SomeOtherSimpleClass as the 
				// package name which is incorrect.
				String innertypeName = JDOMSearchHelper.getResolvedTypeName(name, getType(), getTypeResolutionCache());
				if (innertypeName != null) {
					name = innertypeName;
				}
				ref = reflectJavaClass(name);
				list.add(ref);
			}
		} catch (JavaModelException npe) {
			// name stays null and we carry on
		}
	}
	/**
	 * setModifiers - set the attribute values related to modifiers here
	 */
	protected void setModifiers() {
		JavaClass javaClassTarget = (JavaClass) getTarget();
		try {
			javaClassTarget.setAbstract(Flags.isAbstract(getSourceType().getFlags()));
			javaClassTarget.setFinal(Flags.isFinal(getSourceType().getFlags()));
			javaClassTarget.setPublic(Flags.isPublic(getSourceType().getFlags()));
			// Set type to class or interface, not yet handling EXCEPTION
			if (getSourceType().isClass())
				javaClassTarget.setKind(TypeKind.CLASS_LITERAL);
			else
				javaClassTarget.setKind(TypeKind.INTERFACE_LITERAL);
		} catch (JavaModelException npe) {
			Logger logger = JavaPlugin.getDefault().getLogger();
			if (logger.isLoggingLevel(Level.WARNING))
				logger.log(ResourceHandler.getString("Error_Introspecting_Flags_ERROR_", new Object[] { javaClassTarget.getQualifiedName(), npe.getMessage()}), Level.WARNING); //$NON-NLS-1$ = "error introspecting flags on {0}"
		}
	}
	/**
	 * setNaming - set the naming values here
	 * 	- qualified name (package name + name) must be set first, that is the path to the real Java class
	 *	- ID - simple name, identity within a package document
	 * 	- null UUID
	 */
	protected void setNaming() {
		/* Naming has been provided by the JavaReflectionKey 
		JavaClass javaClassTarget = (JavaClass) getTarget();
		String packageName = getSourceType().getPackageFragment().getElementName();
		javaClassTarget.refSetUUID((String)null);
		((XMIResource)javaClassTarget.eResource()).setID(javaClassTarget,getSourceType().getElementName());
		*/
	}
	protected void setSourceType(IType aType) {
		sourceType = aType;
	}
	/**
	 * setSuper - set our supertype here, implemented interface are handled separately
	 */
	protected void setSuper() throws InheritanceCycleException {
		String superName = null;
		IType superType = null;
		try {
			if (!getSourceType().isInterface()) {
				superName = getSourceType().getSuperclassName();
				// binary types will always have fully-qualified super names, so no need to do any searching.
				if (!getSourceType().isBinary() && superName != null && isTargetInner()) {
					IType declaringType = getSourceType().getDeclaringType();
					if (declaringType != null) {
						//Get all parent InnerTypes
						IType[] inners = declaringType.getTypes();
						for (int i = 0; i < inners.length; i++) {
							IType type = inners[i];
							if (superName.equals(type.getElementName())) {
								superName = declaringType.getElementName() + '.' + superName;
								reflectInnerClasses(declaringType);
								superType = type;
								break;
							}
						}
					}
				}
				
				//Source files return null if extends does not exist.
				if (superName == null && !getSourceType().getFullyQualifiedName().equals(OBJECT_TYPE_NAME))
					superName = OBJECT_TYPE_NAME;
				if (superName != null) {
					JavaClass javaClassTarget = (JavaClass) getTarget();
					if (superType != null)
						javaClassTarget.setSupertype(reflectJavaClass(superType));
					else
						javaClassTarget.setSupertype(reflectJavaClass(superName));
				}
			}
		} catch (JavaModelException npe) {
		}
	}
	private boolean isTargetInner() {
		JavaClassImpl javaClass = (JavaClassImpl) getTarget();
		return (javaClass.getName().indexOf('$') != -1);
	}
	/**
	 * Return true if the sourceType can be found.
	 */
	public boolean sourceTypeExists() {
		return getSourceType() != null;
	}
	protected void reflectInnerClasses() {
		IType[] innerClasses = null;
		try {
			innerClasses = getSourceType().getTypes();
		} catch (JavaModelException e) {
		}
		if (innerClasses != null && innerClasses.length != 0) {
			List declaredClasses = getJavaClassTarget().getDeclaredClassesGen();
			JavaClass inner;
			ResourceSet set = getTargetResource().getResourceSet();
			String packageName = getSourceType().getPackageFragment().getElementName();
			for (int i = 0; i < innerClasses.length; i++) {
				inner = (JavaClass) JavaRefFactory.eINSTANCE.reflectType(packageName, innerClasses[i].getTypeQualifiedName(), set);
				declaredClasses.add(inner);
			}
		}
	}
	protected void reflectInnerClasses(IType aType) {
		IType[] innerClasses = null;
		try {
			innerClasses = aType.getTypes();
		} catch (JavaModelException e) {
		}
		if (innerClasses != null && innerClasses.length != 0) {
			ResourceSet set = getTargetResource().getResourceSet();
			String packageName = aType.getPackageFragment().getElementName();
			JavaClassImpl parentType = (JavaClassImpl) JavaRefFactory.eINSTANCE.reflectType(packageName, aType.getTypeQualifiedName(), set);
			List declaredClasses = parentType.getDeclaredClassesGen();
			JavaClass inner;
			
			
			for (int i = 0; i < innerClasses.length; i++) {
				inner = (JavaClass) JavaRefFactory.eINSTANCE.reflectType(packageName, innerClasses[i].getTypeQualifiedName(), set);
				declaredClasses.add(inner);
			}
		}
	}
	
	/**
	 * Test if we have a valid source and has been reflected. This is to only
	 * to be used by this adapter and by the JDOM adapter factory. It has a 
	 * very specific meaning and may change as needed. It should not be used
	 * for any other purpose. It is not an API.
	 * @return
	 * 
	 * @since 1.2.0
	 */
	synchronized boolean hasValidReflection() {
		return sourceType != null && hasReflected && !isReflecting;
	}
}

Back to the top