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.8", comments = "See http://www.jomc.org/jomc/1.8/jomc-tools-1.8" )
76
77
78 public class RuntimeInstance extends Instance implements RuntimeModelObject
79 {
80
81
82
83 @XmlTransient
84 static final Map<ClassLoader, Map<String, Reference<Class<?>[]>>> classesByClassLoaderAndInstanceCache =
85 new WeakIdentityHashMap<ClassLoader, Map<String, Reference<Class<?>[]>>>();
86
87
88 @XmlTransient
89 static final Map<ClassLoader, Map<String, Reference<Constructor<?>>>> constructorsByClassLoaderAndInstanceCache =
90 new WeakIdentityHashMap<ClassLoader, Map<String, Reference<Constructor<?>>>>();
91
92
93 @XmlTransient
94 static final Map<ClassLoader, Map<String, Reference<Method>>> methodsByClassLoaderAndInstanceCache =
95 new WeakIdentityHashMap<ClassLoader, Map<String, Reference<Method>>>();
96
97
98 @XmlTransient
99 static final Map<ClassLoader, Map<String, Boolean>> assignableFlagsByClassLoaderAndInstanceCache =
100 new WeakIdentityHashMap<ClassLoader, Map<String, Boolean>>();
101
102
103 @XmlTransient
104 static final Map<ClassLoader, Map<String, Reference<Class<?>>>> proxyClassesByClassLoaderAndInstanceCache =
105 new WeakIdentityHashMap<ClassLoader, Map<String, Reference<Class<?>>>>();
106
107
108 @XmlTransient
109 private volatile String javaClassFactoryMethodName;
110
111
112 @XmlTransient
113 private volatile JavaTypeName javaTypeName;
114
115
116
117
118
119
120
121
122 public RuntimeInstance( final Instance instance )
123 {
124 super( instance );
125
126 if ( this.getAuthors() != null )
127 {
128 this.setAuthors( RuntimeModelObjects.getInstance().copyOf( this.getAuthors() ) );
129 }
130 if ( this.getDependencies() != null )
131 {
132 this.setDependencies( RuntimeModelObjects.getInstance().copyOf( this.getDependencies() ) );
133 }
134 if ( this.getDocumentation() != null )
135 {
136 this.setDocumentation( RuntimeModelObjects.getInstance().copyOf( this.getDocumentation() ) );
137 }
138 if ( this.getMessages() != null )
139 {
140 this.setMessages( RuntimeModelObjects.getInstance().copyOf( this.getMessages() ) );
141 }
142 if ( this.getProperties() != null )
143 {
144 this.setProperties( RuntimeModelObjects.getInstance().copyOf( this.getProperties() ) );
145 }
146 if ( this.getSpecifications() != null )
147 {
148 this.setSpecifications( RuntimeModelObjects.getInstance().copyOf( this.getSpecifications() ) );
149 }
150 }
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171 @Override
172 public Class<?> getJavaClass( final ClassLoader classLoader )
173 throws ModelObjectException, ClassNotFoundException
174 {
175 Class<?> javaClass = null;
176
177 if ( this.getJavaTypeName() != null )
178 {
179 ClassLoader classLoaderKey = classLoader;
180 if ( classLoaderKey == null )
181 {
182 classLoaderKey = BOOTSTRAP_CLASSLOADER_KEY;
183 }
184
185 synchronized ( classesByClassLoaderAndNameCache )
186 {
187 Map<String, Reference<Class<?>>> map = classesByClassLoaderAndNameCache.get( classLoaderKey );
188
189 if ( map == null )
190 {
191 map = createMap();
192 classesByClassLoaderAndNameCache.put( classLoaderKey, map );
193 }
194
195 final Reference<Class<?>> reference = map.get( this.getJavaTypeName().getClassName() );
196
197 if ( reference != null )
198 {
199 javaClass = reference.get();
200 }
201
202 if ( javaClass == null )
203 {
204 javaClass = super.getJavaClass( classLoader );
205 map.put( this.getJavaTypeName().getClassName(), new WeakReference<Class<?>>( javaClass ) );
206 }
207 }
208 }
209
210 return javaClass;
211 }
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234 @Override
235 public Class<?>[] getJavaClasses( final ClassLoader classLoader )
236 throws ModelObjectException, ClassNotFoundException
237 {
238 ClassLoader classLoaderKey = classLoader;
239 if ( classLoaderKey == null )
240 {
241 classLoaderKey = BOOTSTRAP_CLASSLOADER_KEY;
242 }
243
244 synchronized ( classesByClassLoaderAndInstanceCache )
245 {
246 Class<?>[] javaClasses = null;
247 Map<String, Reference<Class<?>[]>> map = classesByClassLoaderAndInstanceCache.get( classLoaderKey );
248
249 if ( map == null )
250 {
251 map = createMap();
252 classesByClassLoaderAndInstanceCache.put( classLoaderKey, map );
253 }
254
255 final Reference<Class<?>[]> reference = map.get( this.getIdentifier() );
256
257 if ( reference != null )
258 {
259 javaClasses = reference.get();
260 }
261
262 if ( javaClasses == null && ( reference != null || !map.containsKey( this.getIdentifier() ) ) )
263 {
264 javaClasses = super.getJavaClasses( classLoader );
265 map.put( this.getIdentifier(), new WeakReference<Class<?>[]>( javaClasses ) );
266 }
267
268 return javaClasses;
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 @Override
294 public Constructor<?> getJavaConstructor( final ClassLoader classLoader )
295 throws ModelObjectException, ClassNotFoundException
296 {
297 ClassLoader classLoaderKey = classLoader;
298 if ( classLoaderKey == null )
299 {
300 classLoaderKey = BOOTSTRAP_CLASSLOADER_KEY;
301 }
302
303 synchronized ( constructorsByClassLoaderAndInstanceCache )
304 {
305 Constructor<?> javaClassConstructor = null;
306 Map<String, Reference<Constructor<?>>> map = constructorsByClassLoaderAndInstanceCache.get( classLoaderKey );
307
308 if ( map == null )
309 {
310 map = createMap();
311 constructorsByClassLoaderAndInstanceCache.put( classLoaderKey, map );
312 }
313
314 final Reference<Constructor<?>> reference = map.get( this.getIdentifier() );
315
316 if ( reference != null )
317 {
318 javaClassConstructor = reference.get();
319 }
320
321 if ( javaClassConstructor == null && ( reference != null || !map.containsKey( this.getIdentifier() ) ) )
322 {
323 javaClassConstructor = super.getJavaConstructor( classLoader );
324 map.put( this.getIdentifier(), new WeakReference<Constructor<?>>( javaClassConstructor ) );
325 }
326
327 return javaClassConstructor;
328 }
329 }
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347 @Override
348 public String getJavaFactoryMethodName() throws ModelObjectException
349 {
350 if ( this.javaClassFactoryMethodName == null )
351 {
352 this.javaClassFactoryMethodName = super.getJavaFactoryMethodName();
353 }
354
355 return this.javaClassFactoryMethodName;
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 @Override
381 public Method getJavaFactoryMethod( final ClassLoader classLoader )
382 throws ModelObjectException, ClassNotFoundException
383 {
384 ClassLoader classLoaderKey = classLoader;
385 if ( classLoaderKey == null )
386 {
387 classLoaderKey = BOOTSTRAP_CLASSLOADER_KEY;
388 }
389
390 synchronized ( methodsByClassLoaderAndInstanceCache )
391 {
392 Method javaClassFactoryMethod = null;
393 Map<String, Reference<Method>> map = methodsByClassLoaderAndInstanceCache.get( classLoaderKey );
394
395 if ( map == null )
396 {
397 map = createMap();
398 methodsByClassLoaderAndInstanceCache.put( classLoaderKey, map );
399 }
400
401 final Reference<Method> reference = map.get( this.getIdentifier() );
402
403 if ( reference != null )
404 {
405 javaClassFactoryMethod = reference.get();
406 }
407
408 if ( javaClassFactoryMethod == null && ( reference != null || !map.containsKey( this.getIdentifier() ) ) )
409 {
410 javaClassFactoryMethod = super.getJavaFactoryMethod( classLoader );
411 map.put( this.getIdentifier(), new WeakReference<Method>( javaClassFactoryMethod ) );
412 }
413
414 return javaClassFactoryMethod;
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 @Override
443 public boolean isJavaClassAssignable( final ClassLoader classLoader )
444 throws ModelObjectException, ClassNotFoundException
445 {
446 ClassLoader classLoaderKey = classLoader;
447 if ( classLoaderKey == null )
448 {
449 classLoaderKey = BOOTSTRAP_CLASSLOADER_KEY;
450 }
451
452 synchronized ( assignableFlagsByClassLoaderAndInstanceCache )
453 {
454 Map<String, Boolean> map = assignableFlagsByClassLoaderAndInstanceCache.get( classLoaderKey );
455
456 if ( map == null )
457 {
458 map = createMap();
459 assignableFlagsByClassLoaderAndInstanceCache.put( classLoaderKey, map );
460 }
461
462 Boolean javaClassAssignable = map.get( this.getIdentifier() );
463
464 if ( javaClassAssignable == null && !map.containsKey( this.getIdentifier() ) )
465 {
466 javaClassAssignable = super.isJavaClassAssignable( classLoader );
467 map.put( this.getIdentifier(), javaClassAssignable );
468 }
469
470 return javaClassAssignable == null ? false : javaClassAssignable;
471 }
472 }
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494 @Override
495 public Class<?> getJavaProxyClass( final ClassLoader classLoader )
496 throws ModelObjectException, ClassNotFoundException
497 {
498 ClassLoader classLoaderKey = classLoader;
499 if ( classLoaderKey == null )
500 {
501 classLoaderKey = BOOTSTRAP_CLASSLOADER_KEY;
502 }
503
504 synchronized ( proxyClassesByClassLoaderAndInstanceCache )
505 {
506 Class<?> javaProxyClass = null;
507 Map<String, Reference<Class<?>>> map = proxyClassesByClassLoaderAndInstanceCache.get( classLoaderKey );
508
509 if ( map == null )
510 {
511 map = createMap();
512 proxyClassesByClassLoaderAndInstanceCache.put( classLoaderKey, map );
513 }
514
515 final Reference<Class<?>> reference = map.get( this.getIdentifier() );
516
517 if ( reference != null )
518 {
519 javaProxyClass = reference.get();
520 }
521
522 if ( javaProxyClass == null && ( reference != null || !map.containsKey( this.getIdentifier() ) ) )
523 {
524 javaProxyClass = super.getJavaProxyClass( classLoader );
525 map.put( this.getIdentifier(), new WeakReference<Class<?>>( javaProxyClass ) );
526 }
527
528 return javaProxyClass;
529 }
530 }
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550 @Override
551 public JavaTypeName getJavaTypeName() throws ModelObjectException
552 {
553 if ( this.javaTypeName == null )
554 {
555 this.javaTypeName = super.getJavaTypeName();
556 }
557
558 return this.javaTypeName;
559 }
560
561
562
563 public void gc()
564 {
565 this.gcOrClear( true, false );
566 }
567
568 public void clear()
569 {
570 this.javaClassFactoryMethodName = null;
571 this.javaTypeName = null;
572 this.gcOrClear( false, true );
573 }
574
575 private void gcOrClear( final boolean gc, final boolean clear )
576 {
577 if ( this.getAuthors() instanceof RuntimeModelObject )
578 {
579 if ( gc )
580 {
581 ( (RuntimeModelObject) this.getAuthors() ).gc();
582 }
583 if ( clear )
584 {
585 ( (RuntimeModelObject) this.getAuthors() ).clear();
586 }
587 }
588 if ( this.getDependencies() instanceof RuntimeModelObject )
589 {
590 if ( gc )
591 {
592 ( (RuntimeModelObject) this.getDependencies() ).gc();
593 }
594 if ( clear )
595 {
596 ( (RuntimeModelObject) this.getDependencies() ).clear();
597 }
598 }
599 if ( this.getDocumentation() instanceof RuntimeModelObject )
600 {
601 if ( gc )
602 {
603 ( (RuntimeModelObject) this.getDocumentation() ).gc();
604 }
605 if ( clear )
606 {
607 ( (RuntimeModelObject) this.getDocumentation() ).clear();
608 }
609 }
610 if ( this.getMessages() instanceof RuntimeModelObject )
611 {
612 if ( gc )
613 {
614 ( (RuntimeModelObject) this.getMessages() ).gc();
615 }
616 if ( clear )
617 {
618 ( (RuntimeModelObject) this.getMessages() ).clear();
619 }
620 }
621 if ( this.getProperties() instanceof RuntimeModelObject )
622 {
623 if ( gc )
624 {
625 ( (RuntimeModelObject) this.getProperties() ).gc();
626 }
627 if ( clear )
628 {
629 ( (RuntimeModelObject) this.getProperties() ).clear();
630 }
631 }
632 if ( this.getSpecifications() instanceof RuntimeModelObject )
633 {
634 if ( gc )
635 {
636 ( (RuntimeModelObject) this.getSpecifications() ).gc();
637 }
638 if ( clear )
639 {
640 ( (RuntimeModelObject) this.getSpecifications() ).clear();
641 }
642 }
643 }
644
645
646
647
648
649 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.8", comments = "See http://www.jomc.org/jomc/1.8/jomc-tools-1.8" )
650 public RuntimeInstance()
651 {
652
653 super();
654
655 }
656
657
658
659
660
661
662
663
664 }