Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/beans/Beans.java
38829 views
1
/*
2
* Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package java.beans;
27
28
import com.sun.beans.finder.ClassFinder;
29
30
import java.applet.Applet;
31
import java.applet.AppletContext;
32
import java.applet.AppletStub;
33
import java.applet.AudioClip;
34
35
import java.awt.Image;
36
37
import java.beans.beancontext.BeanContext;
38
39
import java.io.IOException;
40
import java.io.InputStream;
41
import java.io.ObjectInputStream;
42
import java.io.ObjectStreamClass;
43
import java.io.StreamCorruptedException;
44
45
import java.lang.reflect.Modifier;
46
47
import java.net.URL;
48
49
import java.util.Enumeration;
50
import java.util.Hashtable;
51
import java.util.Iterator;
52
import java.util.Vector;
53
54
/**
55
* This class provides some general purpose beans control methods.
56
*/
57
58
public class Beans {
59
60
/**
61
* <p>
62
* Instantiate a JavaBean.
63
* </p>
64
* @return a JavaBean
65
* @param cls the class-loader from which we should create
66
* the bean. If this is null, then the system
67
* class-loader is used.
68
* @param beanName the name of the bean within the class-loader.
69
* For example "sun.beanbox.foobah"
70
*
71
* @exception ClassNotFoundException if the class of a serialized
72
* object could not be found.
73
* @exception IOException if an I/O error occurs.
74
*/
75
76
public static Object instantiate(ClassLoader cls, String beanName) throws IOException, ClassNotFoundException {
77
return Beans.instantiate(cls, beanName, null, null);
78
}
79
80
/**
81
* <p>
82
* Instantiate a JavaBean.
83
* </p>
84
* @return a JavaBean
85
*
86
* @param cls the class-loader from which we should create
87
* the bean. If this is null, then the system
88
* class-loader is used.
89
* @param beanName the name of the bean within the class-loader.
90
* For example "sun.beanbox.foobah"
91
* @param beanContext The BeanContext in which to nest the new bean
92
*
93
* @exception ClassNotFoundException if the class of a serialized
94
* object could not be found.
95
* @exception IOException if an I/O error occurs.
96
*/
97
98
public static Object instantiate(ClassLoader cls, String beanName, BeanContext beanContext) throws IOException, ClassNotFoundException {
99
return Beans.instantiate(cls, beanName, beanContext, null);
100
}
101
102
/**
103
* Instantiate a bean.
104
* <p>
105
* The bean is created based on a name relative to a class-loader.
106
* This name should be a dot-separated name such as "a.b.c".
107
* <p>
108
* In Beans 1.0 the given name can indicate either a serialized object
109
* or a class. Other mechanisms may be added in the future. In
110
* beans 1.0 we first try to treat the beanName as a serialized object
111
* name then as a class name.
112
* <p>
113
* When using the beanName as a serialized object name we convert the
114
* given beanName to a resource pathname and add a trailing ".ser" suffix.
115
* We then try to load a serialized object from that resource.
116
* <p>
117
* For example, given a beanName of "x.y", Beans.instantiate would first
118
* try to read a serialized object from the resource "x/y.ser" and if
119
* that failed it would try to load the class "x.y" and create an
120
* instance of that class.
121
* <p>
122
* If the bean is a subtype of java.applet.Applet, then it is given
123
* some special initialization. First, it is supplied with a default
124
* AppletStub and AppletContext. Second, if it was instantiated from
125
* a classname the applet's "init" method is called. (If the bean was
126
* deserialized this step is skipped.)
127
* <p>
128
* Note that for beans which are applets, it is the caller's responsiblity
129
* to call "start" on the applet. For correct behaviour, this should be done
130
* after the applet has been added into a visible AWT container.
131
* <p>
132
* Note that applets created via beans.instantiate run in a slightly
133
* different environment than applets running inside browsers. In
134
* particular, bean applets have no access to "parameters", so they may
135
* wish to provide property get/set methods to set parameter values. We
136
* advise bean-applet developers to test their bean-applets against both
137
* the JDK appletviewer (for a reference browser environment) and the
138
* BDK BeanBox (for a reference bean container).
139
*
140
* @return a JavaBean
141
* @param cls the class-loader from which we should create
142
* the bean. If this is null, then the system
143
* class-loader is used.
144
* @param beanName the name of the bean within the class-loader.
145
* For example "sun.beanbox.foobah"
146
* @param beanContext The BeanContext in which to nest the new bean
147
* @param initializer The AppletInitializer for the new bean
148
*
149
* @exception ClassNotFoundException if the class of a serialized
150
* object could not be found.
151
* @exception IOException if an I/O error occurs.
152
*/
153
154
public static Object instantiate(ClassLoader cls, String beanName, BeanContext beanContext, AppletInitializer initializer)
155
throws IOException, ClassNotFoundException {
156
157
InputStream ins;
158
ObjectInputStream oins = null;
159
Object result = null;
160
boolean serialized = false;
161
IOException serex = null;
162
163
// If the given classloader is null, we check if an
164
// system classloader is available and (if so)
165
// use that instead.
166
// Note that calls on the system class loader will
167
// look in the bootstrap class loader first.
168
if (cls == null) {
169
try {
170
cls = ClassLoader.getSystemClassLoader();
171
} catch (SecurityException ex) {
172
// We're not allowed to access the system class loader.
173
// Drop through.
174
}
175
}
176
177
// Try to find a serialized object with this name
178
final String serName = beanName.replace('.','/').concat(".ser");
179
if (cls == null)
180
ins = ClassLoader.getSystemResourceAsStream(serName);
181
else
182
ins = cls.getResourceAsStream(serName);
183
if (ins != null) {
184
try {
185
if (cls == null) {
186
oins = new ObjectInputStream(ins);
187
} else {
188
oins = new ObjectInputStreamWithLoader(ins, cls);
189
}
190
result = oins.readObject();
191
serialized = true;
192
oins.close();
193
} catch (IOException ex) {
194
ins.close();
195
// Drop through and try opening the class. But remember
196
// the exception in case we can't find the class either.
197
serex = ex;
198
} catch (ClassNotFoundException ex) {
199
ins.close();
200
throw ex;
201
}
202
}
203
204
if (result == null) {
205
// No serialized object, try just instantiating the class
206
Class<?> cl;
207
208
try {
209
cl = ClassFinder.findClass(beanName, cls);
210
} catch (ClassNotFoundException ex) {
211
// There is no appropriate class. If we earlier tried to
212
// deserialize an object and got an IO exception, throw that,
213
// otherwise rethrow the ClassNotFoundException.
214
if (serex != null) {
215
throw serex;
216
}
217
throw ex;
218
}
219
220
if (!Modifier.isPublic(cl.getModifiers())) {
221
throw new ClassNotFoundException("" + cl + " : no public access");
222
}
223
224
/*
225
* Try to instantiate the class.
226
*/
227
228
try {
229
result = cl.newInstance();
230
} catch (Exception ex) {
231
// We have to remap the exception to one in our signature.
232
// But we pass extra information in the detail message.
233
throw new ClassNotFoundException("" + cl + " : " + ex, ex);
234
}
235
}
236
237
if (result != null) {
238
239
// Ok, if the result is an applet initialize it.
240
241
AppletStub stub = null;
242
243
if (result instanceof Applet) {
244
Applet applet = (Applet) result;
245
boolean needDummies = initializer == null;
246
247
if (needDummies) {
248
249
// Figure our the codebase and docbase URLs. We do this
250
// by locating the URL for a known resource, and then
251
// massaging the URL.
252
253
// First find the "resource name" corresponding to the bean
254
// itself. So a serialzied bean "a.b.c" would imply a
255
// resource name of "a/b/c.ser" and a classname of "x.y"
256
// would imply a resource name of "x/y.class".
257
258
final String resourceName;
259
260
if (serialized) {
261
// Serialized bean
262
resourceName = beanName.replace('.','/').concat(".ser");
263
} else {
264
// Regular class
265
resourceName = beanName.replace('.','/').concat(".class");
266
}
267
268
URL objectUrl = null;
269
URL codeBase = null;
270
URL docBase = null;
271
272
// Now get the URL correponding to the resource name.
273
if (cls == null) {
274
objectUrl = ClassLoader.getSystemResource(resourceName);
275
} else
276
objectUrl = cls.getResource(resourceName);
277
278
// If we found a URL, we try to locate the docbase by taking
279
// of the final path name component, and the code base by taking
280
// of the complete resourceName.
281
// So if we had a resourceName of "a/b/c.class" and we got an
282
// objectURL of "file://bert/classes/a/b/c.class" then we would
283
// want to set the codebase to "file://bert/classes/" and the
284
// docbase to "file://bert/classes/a/b/"
285
286
if (objectUrl != null) {
287
String s = objectUrl.toExternalForm();
288
289
if (s.endsWith(resourceName)) {
290
int ix = s.length() - resourceName.length();
291
codeBase = new URL(s.substring(0,ix));
292
docBase = codeBase;
293
294
ix = s.lastIndexOf('/');
295
296
if (ix >= 0) {
297
docBase = new URL(s.substring(0,ix+1));
298
}
299
}
300
}
301
302
// Setup a default context and stub.
303
BeansAppletContext context = new BeansAppletContext(applet);
304
305
stub = (AppletStub)new BeansAppletStub(applet, context, codeBase, docBase);
306
applet.setStub(stub);
307
} else {
308
initializer.initialize(applet, beanContext);
309
}
310
311
// now, if there is a BeanContext, add the bean, if applicable.
312
313
if (beanContext != null) {
314
unsafeBeanContextAdd(beanContext, result);
315
}
316
317
// If it was deserialized then it was already init-ed.
318
// Otherwise we need to initialize it.
319
320
if (!serialized) {
321
// We need to set a reasonable initial size, as many
322
// applets are unhappy if they are started without
323
// having been explicitly sized.
324
applet.setSize(100,100);
325
applet.init();
326
}
327
328
if (needDummies) {
329
((BeansAppletStub)stub).active = true;
330
} else initializer.activate(applet);
331
332
} else if (beanContext != null) unsafeBeanContextAdd(beanContext, result);
333
}
334
335
return result;
336
}
337
338
@SuppressWarnings("unchecked")
339
private static void unsafeBeanContextAdd(BeanContext beanContext, Object res) {
340
beanContext.add(res);
341
}
342
343
/**
344
* From a given bean, obtain an object representing a specified
345
* type view of that source object.
346
* <p>
347
* The result may be the same object or a different object. If
348
* the requested target view isn't available then the given
349
* bean is returned.
350
* <p>
351
* This method is provided in Beans 1.0 as a hook to allow the
352
* addition of more flexible bean behaviour in the future.
353
*
354
* @return an object representing a specified type view of the
355
* source object
356
* @param bean Object from which we want to obtain a view.
357
* @param targetType The type of view we'd like to get.
358
*
359
*/
360
public static Object getInstanceOf(Object bean, Class<?> targetType) {
361
return bean;
362
}
363
364
/**
365
* Check if a bean can be viewed as a given target type.
366
* The result will be true if the Beans.getInstanceof method
367
* can be used on the given bean to obtain an object that
368
* represents the specified targetType type view.
369
*
370
* @param bean Bean from which we want to obtain a view.
371
* @param targetType The type of view we'd like to get.
372
* @return "true" if the given bean supports the given targetType.
373
*
374
*/
375
public static boolean isInstanceOf(Object bean, Class<?> targetType) {
376
return Introspector.isSubclass(bean.getClass(), targetType);
377
}
378
379
/**
380
* Test if we are in design-mode.
381
*
382
* @return True if we are running in an application construction
383
* environment.
384
*
385
* @see DesignMode
386
*/
387
public static boolean isDesignTime() {
388
return ThreadGroupContext.getContext().isDesignTime();
389
}
390
391
/**
392
* Determines whether beans can assume a GUI is available.
393
*
394
* @return True if we are running in an environment where beans
395
* can assume that an interactive GUI is available, so they
396
* can pop up dialog boxes, etc. This will normally return
397
* true in a windowing environment, and will normally return
398
* false in a server environment or if an application is
399
* running as part of a batch job.
400
*
401
* @see Visibility
402
*
403
*/
404
public static boolean isGuiAvailable() {
405
return ThreadGroupContext.getContext().isGuiAvailable();
406
}
407
408
/**
409
* Used to indicate whether of not we are running in an application
410
* builder environment.
411
*
412
* <p>Note that this method is security checked
413
* and is not available to (for example) untrusted applets.
414
* More specifically, if there is a security manager,
415
* its <code>checkPropertiesAccess</code>
416
* method is called. This could result in a SecurityException.
417
*
418
* @param isDesignTime True if we're in an application builder tool.
419
* @exception SecurityException if a security manager exists and its
420
* <code>checkPropertiesAccess</code> method doesn't allow setting
421
* of system properties.
422
* @see SecurityManager#checkPropertiesAccess
423
*/
424
425
public static void setDesignTime(boolean isDesignTime)
426
throws SecurityException {
427
SecurityManager sm = System.getSecurityManager();
428
if (sm != null) {
429
sm.checkPropertiesAccess();
430
}
431
ThreadGroupContext.getContext().setDesignTime(isDesignTime);
432
}
433
434
/**
435
* Used to indicate whether of not we are running in an environment
436
* where GUI interaction is available.
437
*
438
* <p>Note that this method is security checked
439
* and is not available to (for example) untrusted applets.
440
* More specifically, if there is a security manager,
441
* its <code>checkPropertiesAccess</code>
442
* method is called. This could result in a SecurityException.
443
*
444
* @param isGuiAvailable True if GUI interaction is available.
445
* @exception SecurityException if a security manager exists and its
446
* <code>checkPropertiesAccess</code> method doesn't allow setting
447
* of system properties.
448
* @see SecurityManager#checkPropertiesAccess
449
*/
450
451
public static void setGuiAvailable(boolean isGuiAvailable)
452
throws SecurityException {
453
SecurityManager sm = System.getSecurityManager();
454
if (sm != null) {
455
sm.checkPropertiesAccess();
456
}
457
ThreadGroupContext.getContext().setGuiAvailable(isGuiAvailable);
458
}
459
}
460
461
/**
462
* This subclass of ObjectInputStream delegates loading of classes to
463
* an existing ClassLoader.
464
*/
465
466
class ObjectInputStreamWithLoader extends ObjectInputStream
467
{
468
private ClassLoader loader;
469
470
/**
471
* Loader must be non-null;
472
*/
473
474
public ObjectInputStreamWithLoader(InputStream in, ClassLoader loader)
475
throws IOException, StreamCorruptedException {
476
477
super(in);
478
if (loader == null) {
479
throw new IllegalArgumentException("Illegal null argument to ObjectInputStreamWithLoader");
480
}
481
this.loader = loader;
482
}
483
484
/**
485
* Use the given ClassLoader rather than using the system class
486
*/
487
@SuppressWarnings("rawtypes")
488
protected Class resolveClass(ObjectStreamClass classDesc)
489
throws IOException, ClassNotFoundException {
490
491
String cname = classDesc.getName();
492
return ClassFinder.resolveClass(cname, this.loader);
493
}
494
}
495
496
/**
497
* Package private support class. This provides a default AppletContext
498
* for beans which are applets.
499
*/
500
501
class BeansAppletContext implements AppletContext {
502
Applet target;
503
Hashtable<URL,Object> imageCache = new Hashtable<>();
504
505
BeansAppletContext(Applet target) {
506
this.target = target;
507
}
508
509
public AudioClip getAudioClip(URL url) {
510
// We don't currently support audio clips in the Beans.instantiate
511
// applet context, unless by some luck there exists a URL content
512
// class that can generate an AudioClip from the audio URL.
513
try {
514
return (AudioClip) url.getContent();
515
} catch (Exception ex) {
516
return null;
517
}
518
}
519
520
public synchronized Image getImage(URL url) {
521
Object o = imageCache.get(url);
522
if (o != null) {
523
return (Image)o;
524
}
525
try {
526
o = url.getContent();
527
if (o == null) {
528
return null;
529
}
530
if (o instanceof Image) {
531
imageCache.put(url, o);
532
return (Image) o;
533
}
534
// Otherwise it must be an ImageProducer.
535
Image img = target.createImage((java.awt.image.ImageProducer)o);
536
imageCache.put(url, img);
537
return img;
538
539
} catch (Exception ex) {
540
return null;
541
}
542
}
543
544
public Applet getApplet(String name) {
545
return null;
546
}
547
548
public Enumeration<Applet> getApplets() {
549
Vector<Applet> applets = new Vector<>();
550
applets.addElement(target);
551
return applets.elements();
552
}
553
554
public void showDocument(URL url) {
555
// We do nothing.
556
}
557
558
public void showDocument(URL url, String target) {
559
// We do nothing.
560
}
561
562
public void showStatus(String status) {
563
// We do nothing.
564
}
565
566
public void setStream(String key, InputStream stream)throws IOException{
567
// We do nothing.
568
}
569
570
public InputStream getStream(String key){
571
// We do nothing.
572
return null;
573
}
574
575
public Iterator<String> getStreamKeys(){
576
// We do nothing.
577
return null;
578
}
579
}
580
581
/**
582
* Package private support class. This provides an AppletStub
583
* for beans which are applets.
584
*/
585
class BeansAppletStub implements AppletStub {
586
transient boolean active;
587
transient Applet target;
588
transient AppletContext context;
589
transient URL codeBase;
590
transient URL docBase;
591
592
BeansAppletStub(Applet target,
593
AppletContext context, URL codeBase,
594
URL docBase) {
595
this.target = target;
596
this.context = context;
597
this.codeBase = codeBase;
598
this.docBase = docBase;
599
}
600
601
public boolean isActive() {
602
return active;
603
}
604
605
public URL getDocumentBase() {
606
// use the root directory of the applet's class-loader
607
return docBase;
608
}
609
610
public URL getCodeBase() {
611
// use the directory where we found the class or serialized object.
612
return codeBase;
613
}
614
615
public String getParameter(String name) {
616
return null;
617
}
618
619
public AppletContext getAppletContext() {
620
return context;
621
}
622
623
public void appletResize(int width, int height) {
624
// we do nothing.
625
}
626
}
627
628