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 package org.jomc.ri.model;
37
38 import java.lang.ref.Reference;
39 import java.lang.ref.WeakReference;
40 import java.lang.reflect.Constructor;
41 import java.lang.reflect.Method;
42 import java.util.Map;
43 import javax.xml.bind.annotation.XmlTransient;
44 import org.jomc.model.Instance;
45 import org.jomc.model.JavaTypeName;
46 import org.jomc.model.ModelObjectException;
47 import org.jomc.model.Specification;
48 import org.jomc.util.WeakIdentityHashMap;
49 import static org.jomc.ri.model.RuntimeModelObjects.BOOTSTRAP_CLASSLOADER_KEY;
50 import static org.jomc.ri.model.RuntimeModelObjects.classesByClassLoaderAndNameCache;
51 import static org.jomc.ri.model.RuntimeModelObjects.createMap;
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
76
77
78 public class RuntimeInstance extends Instance implements RuntimeModelObject
79 {
80
81
82
83
84
85 @XmlTransient
86 static final Map<ClassLoader, Map<String, Reference<Class<?>[]>>> classesByClassLoaderAndInstanceCache =
87 new WeakIdentityHashMap<ClassLoader, Map<String, Reference<Class<?>[]>>>();
88
89
90
91
92 @XmlTransient
93 static final Map<ClassLoader, Map<String, Reference<Constructor<?>>>> constructorsByClassLoaderAndInstanceCache =
94 new WeakIdentityHashMap<ClassLoader, Map<String, Reference<Constructor<?>>>>();
95
96
97
98
99 @XmlTransient
100 static final Map<ClassLoader, Map<String, Reference<Method>>> methodsByClassLoaderAndInstanceCache =
101 new WeakIdentityHashMap<ClassLoader, Map<String, Reference<Method>>>();
102
103
104
105
106 @XmlTransient
107 static final Map<ClassLoader, Map<String, Boolean>> assignableFlagsByClassLoaderAndInstanceCache =
108 new WeakIdentityHashMap<ClassLoader, Map<String, Boolean>>();
109
110
111
112
113 @XmlTransient
114 static final Map<ClassLoader, Map<String, Reference<Class<?>>>> proxyClassesByClassLoaderAndInstanceCache =
115 new WeakIdentityHashMap<ClassLoader, Map<String, Reference<Class<?>>>>();
116
117
118
119
120 @XmlTransient
121 private volatile String javaClassFactoryMethodName;
122
123
124
125
126 @XmlTransient
127 private volatile JavaTypeName javaTypeName;
128
129
130
131
132
133
134
135
136 public RuntimeInstance( final Instance instance )
137 {
138 super( instance );
139
140 if ( this.getAuthors() != null )
141 {
142 this.setAuthors( RuntimeModelObjects.getInstance().copyOf( this.getAuthors() ) );
143 }
144 if ( this.getDependencies() != null )
145 {
146 this.setDependencies( RuntimeModelObjects.getInstance().copyOf( this.getDependencies() ) );
147 }
148 if ( this.getDocumentation() != null )
149 {
150 this.setDocumentation( RuntimeModelObjects.getInstance().copyOf( this.getDocumentation() ) );
151 }
152 if ( this.getMessages() != null )
153 {
154 this.setMessages( RuntimeModelObjects.getInstance().copyOf( this.getMessages() ) );
155 }
156 if ( this.getProperties() != null )
157 {
158 this.setProperties( RuntimeModelObjects.getInstance().copyOf( this.getProperties() ) );
159 }
160 if ( this.getSpecifications() != null )
161 {
162 this.setSpecifications( RuntimeModelObjects.getInstance().copyOf( this.getSpecifications() ) );
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 @Override
190 public Class<?> getJavaClass( final ClassLoader classLoader )
191 throws ModelObjectException, ClassNotFoundException
192 {
193 Class<?> javaClass = null;
194
195 if ( this.getJavaTypeName() != null )
196 {
197 ClassLoader classLoaderKey = classLoader;
198 if ( classLoaderKey == null )
199 {
200 classLoaderKey = BOOTSTRAP_CLASSLOADER_KEY;
201 }
202
203 synchronized ( classesByClassLoaderAndNameCache )
204 {
205 Map<String, Reference<Class<?>>> map = classesByClassLoaderAndNameCache.get( classLoaderKey );
206
207 if ( map == null )
208 {
209 map = createMap();
210 classesByClassLoaderAndNameCache.put( classLoaderKey, map );
211 }
212
213 final Reference<Class<?>> reference = map.get( this.getJavaTypeName().getClassName() );
214
215 if ( reference != null )
216 {
217 javaClass = reference.get();
218 }
219
220 if ( javaClass == null )
221 {
222 javaClass = super.getJavaClass( classLoader );
223 map.put( this.getJavaTypeName().getClassName(), new WeakReference<Class<?>>( javaClass ) );
224 }
225 }
226 }
227
228 return javaClass;
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 @Override
257 public Class<?>[] getJavaClasses( final ClassLoader classLoader )
258 throws ModelObjectException, ClassNotFoundException
259 {
260 ClassLoader classLoaderKey = classLoader;
261 if ( classLoaderKey == null )
262 {
263 classLoaderKey = BOOTSTRAP_CLASSLOADER_KEY;
264 }
265
266 synchronized ( classesByClassLoaderAndInstanceCache )
267 {
268 Class<?>[] javaClasses = null;
269 Map<String, Reference<Class<?>[]>> map = classesByClassLoaderAndInstanceCache.get( classLoaderKey );
270
271 if ( map == null )
272 {
273 map = createMap();
274 classesByClassLoaderAndInstanceCache.put( classLoaderKey, map );
275 }
276
277 final Reference<Class<?>[]> reference = map.get( this.getIdentifier() );
278
279 if ( reference != null )
280 {
281 javaClasses = reference.get();
282 }
283
284 if ( javaClasses == null && ( reference != null || !map.containsKey( this.getIdentifier() ) ) )
285 {
286 javaClasses = super.getJavaClasses( classLoader );
287 map.put( this.getIdentifier(), new WeakReference<Class<?>[]>( javaClasses ) );
288 }
289
290 return javaClasses;
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 @Override
320 public Constructor<?> getJavaConstructor( final ClassLoader classLoader )
321 throws ModelObjectException, ClassNotFoundException
322 {
323 ClassLoader classLoaderKey = classLoader;
324 if ( classLoaderKey == null )
325 {
326 classLoaderKey = BOOTSTRAP_CLASSLOADER_KEY;
327 }
328
329 synchronized ( constructorsByClassLoaderAndInstanceCache )
330 {
331 Constructor<?> javaClassConstructor = null;
332 Map<String, Reference<Constructor<?>>> map = constructorsByClassLoaderAndInstanceCache.get( classLoaderKey );
333
334 if ( map == null )
335 {
336 map = createMap();
337 constructorsByClassLoaderAndInstanceCache.put( classLoaderKey, map );
338 }
339
340 final Reference<Constructor<?>> reference = map.get( this.getIdentifier() );
341
342 if ( reference != null )
343 {
344 javaClassConstructor = reference.get();
345 }
346
347 if ( javaClassConstructor == null && ( reference != null || !map.containsKey( this.getIdentifier() ) ) )
348 {
349 javaClassConstructor = super.getJavaConstructor( classLoader );
350 map.put( this.getIdentifier(), new WeakReference<Constructor<?>>( javaClassConstructor ) );
351 }
352
353 return javaClassConstructor;
354 }
355 }
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377 @Override
378 public String getJavaFactoryMethodName() throws ModelObjectException
379 {
380 if ( this.javaClassFactoryMethodName == null )
381 {
382 this.javaClassFactoryMethodName = super.getJavaFactoryMethodName();
383 }
384
385 return this.javaClassFactoryMethodName;
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 @Override
415 public Method getJavaFactoryMethod( final ClassLoader classLoader )
416 throws ModelObjectException, ClassNotFoundException
417 {
418 ClassLoader classLoaderKey = classLoader;
419 if ( classLoaderKey == null )
420 {
421 classLoaderKey = BOOTSTRAP_CLASSLOADER_KEY;
422 }
423
424 synchronized ( methodsByClassLoaderAndInstanceCache )
425 {
426 Method javaClassFactoryMethod = null;
427 Map<String, Reference<Method>> map = methodsByClassLoaderAndInstanceCache.get( classLoaderKey );
428
429 if ( map == null )
430 {
431 map = createMap();
432 methodsByClassLoaderAndInstanceCache.put( classLoaderKey, map );
433 }
434
435 final Reference<Method> reference = map.get( this.getIdentifier() );
436
437 if ( reference != null )
438 {
439 javaClassFactoryMethod = reference.get();
440 }
441
442 if ( javaClassFactoryMethod == null && ( reference != null || !map.containsKey( this.getIdentifier() ) ) )
443 {
444 javaClassFactoryMethod = super.getJavaFactoryMethod( classLoader );
445 map.put( this.getIdentifier(), new WeakReference<Method>( javaClassFactoryMethod ) );
446 }
447
448 return javaClassFactoryMethod;
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 @Override
481 public boolean isJavaClassAssignable( final ClassLoader classLoader )
482 throws ModelObjectException, ClassNotFoundException
483 {
484 ClassLoader classLoaderKey = classLoader;
485 if ( classLoaderKey == null )
486 {
487 classLoaderKey = BOOTSTRAP_CLASSLOADER_KEY;
488 }
489
490 synchronized ( assignableFlagsByClassLoaderAndInstanceCache )
491 {
492 Map<String, Boolean> map = assignableFlagsByClassLoaderAndInstanceCache.get( classLoaderKey );
493
494 if ( map == null )
495 {
496 map = createMap();
497 assignableFlagsByClassLoaderAndInstanceCache.put( classLoaderKey, map );
498 }
499
500 Boolean javaClassAssignable = map.get( this.getIdentifier() );
501
502 if ( javaClassAssignable == null && !map.containsKey( this.getIdentifier() ) )
503 {
504 javaClassAssignable = super.isJavaClassAssignable( classLoader );
505 map.put( this.getIdentifier(), javaClassAssignable );
506 }
507
508 return javaClassAssignable == null ? false : javaClassAssignable;
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 @Override
537 public Class<?> getJavaProxyClass( final ClassLoader classLoader )
538 throws ModelObjectException, ClassNotFoundException
539 {
540 ClassLoader classLoaderKey = classLoader;
541 if ( classLoaderKey == null )
542 {
543 classLoaderKey = BOOTSTRAP_CLASSLOADER_KEY;
544 }
545
546 synchronized ( proxyClassesByClassLoaderAndInstanceCache )
547 {
548 Class<?> javaProxyClass = null;
549 Map<String, Reference<Class<?>>> map = proxyClassesByClassLoaderAndInstanceCache.get( classLoaderKey );
550
551 if ( map == null )
552 {
553 map = createMap();
554 proxyClassesByClassLoaderAndInstanceCache.put( classLoaderKey, map );
555 }
556
557 final Reference<Class<?>> reference = map.get( this.getIdentifier() );
558
559 if ( reference != null )
560 {
561 javaProxyClass = reference.get();
562 }
563
564 if ( javaProxyClass == null && ( reference != null || !map.containsKey( this.getIdentifier() ) ) )
565 {
566 javaProxyClass = super.getJavaProxyClass( classLoader );
567 map.put( this.getIdentifier(), new WeakReference<Class<?>>( javaProxyClass ) );
568 }
569
570 return javaProxyClass;
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 @Override
597 public JavaTypeName getJavaTypeName() throws ModelObjectException
598 {
599 if ( this.javaTypeName == null )
600 {
601 this.javaTypeName = super.getJavaTypeName();
602 }
603
604 return this.javaTypeName;
605 }
606
607
608
609 public void gc()
610 {
611 this.gcOrClear( true, false );
612 }
613
614 public void clear()
615 {
616 this.javaClassFactoryMethodName = null;
617 this.javaTypeName = null;
618 this.gcOrClear( false, true );
619 }
620
621 private void gcOrClear( final boolean gc, final boolean clear )
622 {
623 if ( this.getAuthors() instanceof RuntimeModelObject )
624 {
625 if ( gc )
626 {
627 ( (RuntimeModelObject) this.getAuthors() ).gc();
628 }
629 if ( clear )
630 {
631 ( (RuntimeModelObject) this.getAuthors() ).clear();
632 }
633 }
634 if ( this.getDependencies() instanceof RuntimeModelObject )
635 {
636 if ( gc )
637 {
638 ( (RuntimeModelObject) this.getDependencies() ).gc();
639 }
640 if ( clear )
641 {
642 ( (RuntimeModelObject) this.getDependencies() ).clear();
643 }
644 }
645 if ( this.getDocumentation() instanceof RuntimeModelObject )
646 {
647 if ( gc )
648 {
649 ( (RuntimeModelObject) this.getDocumentation() ).gc();
650 }
651 if ( clear )
652 {
653 ( (RuntimeModelObject) this.getDocumentation() ).clear();
654 }
655 }
656 if ( this.getMessages() instanceof RuntimeModelObject )
657 {
658 if ( gc )
659 {
660 ( (RuntimeModelObject) this.getMessages() ).gc();
661 }
662 if ( clear )
663 {
664 ( (RuntimeModelObject) this.getMessages() ).clear();
665 }
666 }
667 if ( this.getProperties() instanceof RuntimeModelObject )
668 {
669 if ( gc )
670 {
671 ( (RuntimeModelObject) this.getProperties() ).gc();
672 }
673 if ( clear )
674 {
675 ( (RuntimeModelObject) this.getProperties() ).clear();
676 }
677 }
678 if ( this.getSpecifications() instanceof RuntimeModelObject )
679 {
680 if ( gc )
681 {
682 ( (RuntimeModelObject) this.getSpecifications() ).gc();
683 }
684 if ( clear )
685 {
686 ( (RuntimeModelObject) this.getSpecifications() ).clear();
687 }
688 }
689 }
690
691
692
693
694
695 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
696 public RuntimeInstance()
697 {
698
699 super();
700
701 }
702
703
704
705
706
707
708
709
710
711 }