Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1c07092f8390747f86aed0afe6de3b385b7793f8 (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
/*******************************************************************************
 * Copyright (c) 2007, 2008 Oracle. 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:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.core.internal.resource.java;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Vector;

import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.FieldDeclaration;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jpt.core.internal.utility.jdt.JDTTools;
import org.eclipse.jpt.core.internal.utility.jdt.JDTType;
import org.eclipse.jpt.core.resource.java.AccessType;
import org.eclipse.jpt.core.resource.java.Annotation;
import org.eclipse.jpt.core.resource.java.JavaResourcePersistentAttribute;
import org.eclipse.jpt.core.resource.java.JavaResourcePersistentType;
import org.eclipse.jpt.core.resource.java.JpaCompilationUnit;
import org.eclipse.jpt.core.resource.java.NestableAnnotation;
import org.eclipse.jpt.core.utility.jdt.Type;
import org.eclipse.jpt.utility.MethodSignature;
import org.eclipse.jpt.utility.internal.CollectionTools;
import org.eclipse.jpt.utility.internal.Counter;
import org.eclipse.jpt.utility.internal.iterators.CloneIterator;
import org.eclipse.jpt.utility.internal.iterators.CompositeIterator;
import org.eclipse.jpt.utility.internal.iterators.FilteringIterator;

public class JavaResourcePersistentTypeImpl
	extends AbstractJavaResourcePersistentMember<Type>
	implements JavaResourcePersistentType
{
	private String qualifiedName;

	private String name;

	private String superClassQualifiedName;

	private boolean abstract_;  // 'abstract' is a reserved word

	/**
	 * store all member types including those that aren't persistable so we can
	 * generate validation errors
	 */
	private final Vector<JavaResourcePersistentType> nestedTypes;

	private final Vector<JavaResourcePersistentAttribute> fields;

	private final Vector<JavaResourcePersistentAttribute> methods;

	private AccessType accessType;


	// ********** construction **********

	/**
	 * build top-level persistent type
	 */
	// TODO use JPA factory
	public static JavaResourcePersistentType newInstance(
			JpaCompilationUnit jpaCompilationUnit,
			TypeDeclaration typeDeclaration,
			CompilationUnit astRoot) {
		Type type = new JDTType(
				typeDeclaration,
				jpaCompilationUnit.getCompilationUnit(),
				jpaCompilationUnit.getModifySharedDocumentCommandExecutorProvider(),
				jpaCompilationUnit.getAnnotationEditFormatter());
		JavaResourcePersistentType jrpt = new JavaResourcePersistentTypeImpl(jpaCompilationUnit, type);
		jrpt.initialize(astRoot);
		return jrpt;	
	}

	/**
	 * build nested persistent type
	 */
	// TODO use JPA factory
	protected static JavaResourcePersistentType newInstance(
			JpaCompilationUnit jpaCompilationUnit,
			Type declaringType,
			TypeDeclaration typeDeclaration,
			int occurrence,
			CompilationUnit astRoot) {
		Type type = new JDTType(
				declaringType,
				typeDeclaration,
				occurrence,
				jpaCompilationUnit.getCompilationUnit(),
				jpaCompilationUnit.getModifySharedDocumentCommandExecutorProvider(),
				jpaCompilationUnit.getAnnotationEditFormatter());
		JavaResourcePersistentType jrpt = new JavaResourcePersistentTypeImpl(jpaCompilationUnit, type);
		jrpt.initialize(astRoot);
		return jrpt;	
	}

	public JavaResourcePersistentTypeImpl(JpaCompilationUnit jpaCompilationUnit, Type type) {
		super(jpaCompilationUnit, type);
		this.nestedTypes = new Vector<JavaResourcePersistentType>(); 
		this.fields = new Vector<JavaResourcePersistentAttribute>();
		this.methods = new Vector<JavaResourcePersistentAttribute>();
	}

	@Override
	public void initialize(CompilationUnit astRoot) {
		super.initialize(astRoot);
		this.qualifiedName = this.buildQualifiedName(astRoot);
		this.name = this.buildName(astRoot);
		this.superClassQualifiedName = this.buildSuperClassQualifiedName(astRoot);
		this.abstract_ = this.buildAbstract(astRoot);
		this.initializeNestedTypes(astRoot);
		this.initializeFields(astRoot);
		this.initializeMethods(astRoot);
		this.accessType = this.buildAccessType();
	}

	protected void initializeNestedTypes(CompilationUnit astRoot) {
		CounterMap counters = new CounterMap();
		for (TypeDeclaration td : this.getMember().getTypes(astRoot)) {
			String tdName = td.getName().getFullyQualifiedName();
			int occurrence = counters.increment(tdName);
			this.nestedTypes.add(this.buildNestedType(td, occurrence, astRoot));
		}
	}

	protected void initializeFields(CompilationUnit astRoot) {
		CounterMap counters = new CounterMap();
		for (FieldDeclaration fieldDeclaration : this.getMember().getFields(astRoot)) {
			for (VariableDeclarationFragment fragment : fragments(fieldDeclaration)) {
				String fieldName = fragment.getName().getFullyQualifiedName();
				int occurrence = counters.increment(fieldName);
				this.fields.add(this.buildField(fieldName, occurrence, astRoot));
			}
		}
	}

	protected void initializeMethods(CompilationUnit astRoot) {
		CounterMap counters = new CounterMap();
		for (MethodDeclaration methodDeclaration : this.getMember().getMethods(astRoot)) {
			MethodSignature signature = JDTTools.buildMethodSignature(methodDeclaration);
			int occurrence = counters.increment(signature);
			this.methods.add(this.buildMethod(signature, occurrence, astRoot));
		}
	}


	// ********** AbstractJavaResourcePersistentMember implementation **********

	@Override
	protected Annotation buildMappingAnnotation(String mappingAnnotationName) {
		return getAnnotationProvider().buildTypeMappingAnnotation(this, getMember(), mappingAnnotationName);
	}
	
	@Override
	protected Annotation buildNullMappingAnnotation(String annotationName) {
		return getAnnotationProvider().buildNullTypeMappingAnnotation(this, getMember(), annotationName);
	}

	@Override
	protected Annotation buildAnnotation(String annotationName) {
		return getAnnotationProvider().buildTypeAnnotation(this, getMember(), annotationName);
	}
	
	@Override
	protected Annotation buildNullAnnotation(String annotationName) {
		return getAnnotationProvider().buildNullTypeAnnotation(this, getMember(), annotationName);
	}
	
	@Override
	protected ListIterator<String> possibleMappingAnnotationNames() {
		return getAnnotationProvider().typeMappingAnnotationNames();
	}
	
	@Override
	protected boolean isPossibleAnnotation(String annotationName) {
		return CollectionTools.contains(getAnnotationProvider().typeAnnotationNames(), annotationName);
	}
	
	@Override
	protected boolean isPossibleMappingAnnotation(String annotationName) {
		return CollectionTools.contains(getAnnotationProvider().typeMappingAnnotationNames(), annotationName);
	}
	
	@Override
	@SuppressWarnings("unchecked")
	//overriding purely to suppress the warning you get at the class level
	public ListIterator<NestableAnnotation> annotations(String nestableAnnotationName, String containerAnnotationName) {
		return super.annotations(nestableAnnotationName, containerAnnotationName);
	}
	
	@Override
	@SuppressWarnings("unchecked")
	//overriding purely to suppress the warning you get at the class level
	public Iterator<Annotation> mappingAnnotations() {
		return super.mappingAnnotations();
	}
	
	@Override
	@SuppressWarnings("unchecked")
	//overriding purely to suppress the warning you get at the class level
	public Iterator<Annotation> annotations() {
		return super.annotations();
	}

	@Override
	public void resolveTypes(CompilationUnit astRoot) {
		super.resolveTypes(astRoot);

		this.setSuperClassQualifiedName(this.buildSuperClassQualifiedName(astRoot));

		for (Iterator<JavaResourcePersistentAttribute> stream = this.fields_(); stream.hasNext(); ) {
			stream.next().resolveTypes(astRoot);
		}

		// a new type can trigger a method parameter type to be a resolved,
		// fully-qualified name, so we need to rebuild our list of methods:
		//     "setFoo(Foo)" is not the same as "setFoo(com.bar.Foo)"
		// and, vice-versa, a removed type can "unresolve" a parameter type
		this.updateMethods(astRoot);

		for (Iterator<JavaResourcePersistentAttribute> stream = this.methods_(); stream.hasNext(); ) {
			stream.next().resolveTypes(astRoot);
		}
		for (Iterator<JavaResourcePersistentType> stream = this.nestedTypes_(); stream.hasNext(); ) {
			stream.next().resolveTypes(astRoot);
		}
	}

	@Override
	public void toString(StringBuilder sb) {
		sb.append(this.name);
	}


	// ******** JavaResourcePersistentType implementation ********

	public JavaResourcePersistentType getJavaPersistentTypeResource(String fullyQualifiedTypeName) {
		// TODO not sure why a null name is coming through here...  ~bjv
//		if (fullyQualifiedTypeName.equals(this.getQualifiedName())) {
		if (this.getQualifiedName().equals(fullyQualifiedTypeName)) {
			return this;
		}
		for (Iterator<JavaResourcePersistentType> stream = this.nestedTypes(); stream.hasNext(); ) {
			JavaResourcePersistentType jrpt = stream.next();
			// recurse
			JavaResourcePersistentType nestedJRPT = jrpt.getJavaPersistentTypeResource(fullyQualifiedTypeName);
			if (nestedJRPT != null) {
				return nestedJRPT;
			}
		}
		return null;
	}

	/**
	 * check only persistable attributes
	 */
	public boolean hasAnyAttributeAnnotations() {
		for (Iterator<JavaResourcePersistentAttribute> stream = this.attributes(); stream.hasNext(); ) {
			if (stream.next().hasAnyAnnotation()) {
				return true;
			}
		}
		return false;
	}
	

	// ********** nested types **********

	/**
	 * return only persistable nested types
	 */
	public Iterator<JavaResourcePersistentType> nestedTypes() {
		//TODO since we are filtering how do we handle the case where a type becomes persistable?
		//what kind of change notificiation for that case?
		return new FilteringIterator<JavaResourcePersistentType, JavaResourcePersistentType>(this.nestedTypes_()) {
			@Override
			protected boolean accept(JavaResourcePersistentType jrpt) {
				return jrpt.isPersistable();
			}
		};
	}

	/**
	 * *all* nested types
	 */
	protected Iterator<JavaResourcePersistentType> nestedTypes_() {
		return new CloneIterator<JavaResourcePersistentType>(this.nestedTypes);
	}

	protected void addNestedType(JavaResourcePersistentType nestedType) {
		this.addItemToCollection(nestedType, this.nestedTypes, NESTED_TYPES_COLLECTION);
	}

	protected void removeNestedType(JavaResourcePersistentType nestedType) {
		this.removeItemFromCollection(nestedType, this.nestedTypes, NESTED_TYPES_COLLECTION);
	}

	protected void removeNestedTypes(Collection<JavaResourcePersistentType> remove) {
		this.removeItemsFromCollection(remove, this.nestedTypes, NESTED_TYPES_COLLECTION);
	}

	protected JavaResourcePersistentType getNestedType(String typeName, int occurrence) {
		for (Iterator<JavaResourcePersistentType> stream = this.nestedTypes_(); stream.hasNext(); ) {
			JavaResourcePersistentType nestedType = stream.next();
			if (nestedType.isFor(typeName, occurrence)) {
				return nestedType;
			}
		}
		return null;
	}


	// ********** attributes **********

	/**
	 * return only persistable attributes
	 */
	// TODO since we are filtering, how do we handle the case where an attribute becomes persistable?
	// what kind of change notification for that case?
	public Iterator<JavaResourcePersistentAttribute> attributes() {
		return persistableMembers(this.attributes_());
	}

	/**
	 * *all* fields and methods
	 */
	@SuppressWarnings("unchecked")
	protected Iterator<JavaResourcePersistentAttribute> attributes_() {
		return new CompositeIterator<JavaResourcePersistentAttribute>(this.fields_(), this.methods_());
	}


	// ********** fields **********

	/**
	 * return only persistable fields
	 */
	public Iterator<JavaResourcePersistentAttribute> fields() {
		return persistableMembers(this.fields_());
	}
	
	/**
	 * *all* fields
	 */
	protected Iterator<JavaResourcePersistentAttribute> fields_() {
		return new CloneIterator<JavaResourcePersistentAttribute>(this.fields);
	}

	protected void addField(JavaResourcePersistentAttribute attribute) {
		this.addItemToCollection(attribute, this.fields, ATTRIBUTES_COLLECTION);
	}

	protected void removeField(JavaResourcePersistentAttribute attribute) {
		this.removeItemFromCollection(attribute, this.fields, ATTRIBUTES_COLLECTION);
	}

	protected void removeFields(Collection<JavaResourcePersistentAttribute> remove) {
		this.removeItemsFromCollection(remove, this.fields, ATTRIBUTES_COLLECTION);
	}

	protected JavaResourcePersistentAttribute getField(String fieldName, int occurrence) {
		for (Iterator<JavaResourcePersistentAttribute> stream = this.fields_(); stream.hasNext(); ) {
			JavaResourcePersistentAttribute field = stream.next();
			if (field.isFor(fieldName, occurrence)) {
				return field;
			}
		}
		return null;
	}


	// ********** methods **********

	/**
	 * return only persistable properties
	 */
	public Iterator<JavaResourcePersistentAttribute> properties() {
		return persistableMembers(this.methods_());
	}

	/**
	 * *all* methods
	 */
	protected Iterator<JavaResourcePersistentAttribute> methods_() {
		return new CloneIterator<JavaResourcePersistentAttribute>(this.methods);
	}

	protected void addMethod(JavaResourcePersistentAttribute attribute) {
		this.addItemToCollection(attribute, this.methods, ATTRIBUTES_COLLECTION);
	}

	protected void removeMethod(JavaResourcePersistentAttribute attribute) {
		this.removeItemFromCollection(attribute, this.methods, ATTRIBUTES_COLLECTION);
	}

	protected void removeMethods(Collection<JavaResourcePersistentAttribute> remove) {
		this.removeItemsFromCollection(remove, this.methods, ATTRIBUTES_COLLECTION);
	}

	protected JavaResourcePersistentAttribute getMethod(MethodSignature signature, int occurrence) {
		for (Iterator<JavaResourcePersistentAttribute> stream = this.methods_(); stream.hasNext(); ) {
			JavaResourcePersistentAttribute method = stream.next();
			if (method.isFor(signature, occurrence)) {
				return method;
			}
		}
		return null;
	}


	// ********** simple instance variables **********

	public String getQualifiedName() {
		return this.qualifiedName;
	}

	protected void setQualifiedName(String qualifiedName) {
		String old = this.qualifiedName;
		this.qualifiedName = qualifiedName;
		this.firePropertyChanged(QUALIFIED_NAME_PROPERTY, old, qualifiedName);
	}

	public String getName() {
		return this.name;
	}

	protected void setName(String name) {
		String old = this.name;
		this.name = name;
		this.firePropertyChanged(NAME_PROPERTY, old, name);
	}

	public String getSuperClassQualifiedName() {
		return this.superClassQualifiedName;
	}

	protected void setSuperClassQualifiedName(String superClassQualifiedName) {
		String old = this.superClassQualifiedName;
		this.superClassQualifiedName = superClassQualifiedName;
		this.firePropertyChanged(SUPER_CLASS_QUALIFIED_NAME_PROPERTY, old, superClassQualifiedName);
	}

	public boolean isAbstract() {
		return this.abstract_;
	}

	protected void setAbstract(boolean abstract_) {
		boolean old = this.abstract_;
		this.abstract_ = abstract_;
		this.firePropertyChanged(ABSTRACT_PROPERTY, old, abstract_);
	}

	public AccessType getAccess() {
		return this.accessType;
	}

	// TODO
	//seems we could have a public changeAccess() api which would
	//move all annotations from fields to their corresponding methods or vice versa
	//though of course it's more complicated than that since what if the
	//corresponding field/method does not exist?
	//making this internal since it should only be set based on changes in the source, the
	//context model should not need to set this
	protected void setAccess(AccessType accessType) {
		AccessType old = this.accessType;
		this.accessType = accessType;
		this.firePropertyChanged(ACCESS_PROPERTY, old, accessType);
	}


	// ********** update from Java **********

	@Override
	public void update(CompilationUnit astRoot) {
		super.update(astRoot);
		this.setQualifiedName(this.buildQualifiedName(astRoot));
		this.setName(this.buildName(astRoot));
		this.setSuperClassQualifiedName(this.buildSuperClassQualifiedName(astRoot));
		this.setAbstract(this.buildAbstract(astRoot));
		this.updateNestedTypes(astRoot);
		this.updateFields(astRoot);
		this.updateMethods(astRoot);
		this.setAccess(this.buildAccessType());
	}

	protected void updateNestedTypes(CompilationUnit astRoot) {
		CounterMap counters = new CounterMap();
		@SuppressWarnings("unchecked")
		Vector<JavaResourcePersistentType> nestedTypesToRemove = (Vector<JavaResourcePersistentType>) this.nestedTypes.clone();
		for (TypeDeclaration typeDeclaration : this.getMember().getTypes(astRoot)) {
			String tdName = typeDeclaration.getName().getFullyQualifiedName();
			int occurrence = counters.increment(tdName);

			JavaResourcePersistentType nestedType = this.getNestedType(tdName, occurrence);
			if (nestedType == null) {
				this.addNestedType(this.buildNestedType(typeDeclaration, occurrence, astRoot));
			} else {
				nestedTypesToRemove.remove(nestedType);
				nestedType.update(astRoot);
			}
		}
		this.removeNestedTypes(nestedTypesToRemove);
	}

	protected void updateFields(CompilationUnit astRoot) {
		CounterMap counters = new CounterMap();
		@SuppressWarnings("unchecked")
		Vector<JavaResourcePersistentAttribute> fieldsToRemove = (Vector<JavaResourcePersistentAttribute>) this.fields.clone();
		for (FieldDeclaration fieldDeclaration : this.getMember().getFields(astRoot)) {
			for (VariableDeclarationFragment fragment : fragments(fieldDeclaration)) {
				String fieldName = fragment.getName().getFullyQualifiedName();
				int occurrence = counters.increment(fieldName);

				JavaResourcePersistentAttribute field = this.getField(fieldName, occurrence);
				if (field == null) {
					this.addField(this.buildField(fieldName, occurrence, astRoot));
				} else {
					fieldsToRemove.remove(field);
					field.update(astRoot);
				}
			}
		}
		this.removeFields(fieldsToRemove);
	}

	protected void updateMethods(CompilationUnit astRoot) {
		CounterMap counters = new CounterMap();
		@SuppressWarnings("unchecked")
		Vector<JavaResourcePersistentAttribute> methodsToRemove = (Vector<JavaResourcePersistentAttribute>) this.methods.clone();
		for (MethodDeclaration methodDeclaration : this.getMember().getMethods(astRoot)) {
			MethodSignature signature = JDTTools.buildMethodSignature(methodDeclaration);
			int occurrence = counters.increment(signature);

			JavaResourcePersistentAttribute method = this.getMethod(signature, occurrence);
			if (method == null) {
				this.addMethod(this.buildMethod(signature, occurrence, astRoot));
			} else {
				methodsToRemove.remove(method);
				method.update(astRoot);
			}
		}
		this.removeMethods(methodsToRemove);
	}


	// ********** building state from AST **********

	protected String buildQualifiedName(CompilationUnit astRoot) {
		return this.getMember().getBinding(astRoot).getQualifiedName();
	}

	protected String buildName(CompilationUnit astRoot) {
		return this.getMember().getBinding(astRoot).getName();
	}

	protected String buildSuperClassQualifiedName(CompilationUnit astRoot) {
		ITypeBinding typeBinding = this.getMember().getBinding(astRoot);
		if (typeBinding == null) {
			return null;
		}
		ITypeBinding superClassTypeBinding = typeBinding.getSuperclass();
		if (superClassTypeBinding == null) {
			return null;
		}
		return superClassTypeBinding.getTypeDeclaration().getQualifiedName();
	}

	protected boolean buildAbstract(CompilationUnit astRoot) {
		return Modifier.isAbstract(this.getMember().getBinding(astRoot).getModifiers());	
	}

	// TODO use JPA factory
	protected JavaResourcePersistentType buildNestedType(TypeDeclaration nestedTypeDeclaration, int occurrence, CompilationUnit astRoot) {
		return newInstance(this.getJpaCompilationUnit(), this.getMember(), nestedTypeDeclaration, occurrence, astRoot);
	}

	// TODO use JPA factory
	protected JavaResourcePersistentAttribute buildField(String fieldName, int occurrence, CompilationUnit astRoot) {
		return JavaResourcePersistentAttributeImpl.newInstance(this, this.getMember(), fieldName, occurrence, this.getJpaCompilationUnit(), astRoot);
	}

	// TODO use JPA factory
	protected JavaResourcePersistentAttribute buildMethod(MethodSignature signature, int occurrence, CompilationUnit astRoot) {
		return JavaResourcePersistentAttributeImpl.newInstance(this, this.getMember(), signature, occurrence, this.getJpaCompilationUnit(), astRoot);
	}

	/**
	 * Return the AccessType currently implied by the Java source code:
	 *     - if only Fields are annotated => FIELD
	 *     - if only Properties are annotated => PROPERTY
	 *     - if both Fields and Properties are annotated => FIELD
	 *     - if nothing is annotated
	 *     		- and fields exist => FIELD
	 *     		- and properties exist, but no fields exist => PROPERTY
	 *     		- and neither fields nor properties exist => null at this level (FIELD in the context model)
	 */
	protected AccessType buildAccessType() {
		boolean hasPersistableFields = false;
		boolean hasPersistableProperties = false;
		for (JavaResourcePersistentAttribute field : CollectionTools.iterable(fields())) {
			hasPersistableFields = true;
			if (field.hasAnyAnnotation()) {
				// any field is annotated => FIELD
				return AccessType.FIELD;
			}
		}
		for (JavaResourcePersistentAttribute property : CollectionTools.iterable(properties())) {
			hasPersistableProperties = true;
			if (property.hasAnyAnnotation()) {
				// none of the fields are annotated and a getter is annotated => PROPERTY
				return AccessType.PROPERTY;
			}
		}

		if (hasPersistableProperties && !hasPersistableFields) {
			return AccessType.PROPERTY;
		}
		//no annotations exist, access is null at the resource model level
		return null;
	}


	// ********** static methods **********

	// minimize scope of suppressed warnings
	@SuppressWarnings("unchecked")
	protected static List<VariableDeclarationFragment> fragments(FieldDeclaration fd) {
		return fd.fragments();
	}


	// ********** StringCounter **********

	protected static class CounterMap {
		HashMap<Object, Counter> counters = new HashMap<Object, Counter>();

		/**
		 * Return the incremented count for the specified object.
		 */
		int increment(Object o) {
			Counter counter = this.counters.get(o);
			if (counter == null) {
				counter = new Counter();
				this.counters.put(o, counter);
			}
			counter.increment();
			return counter.count();
		}
	}

}

Back to the top