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