开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
forked from cxylk/Java-Notes
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
main
分支 (2)
main
gh-pages
main
分支 (2)
main
gh-pages
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
main
分支 (2)
main
gh-pages
Java-Notes
/
JavaSourceLearn
/
src
/
java
/
beans
/
DefaultPersistenceDelegate.java
Java-Notes
/
JavaSourceLearn
/
src
/
java
/
beans
/
DefaultPersistenceDelegate.java
DefaultPersistenceDelegate.java 17.44 KB
一键复制 编辑 原始数据 按行查看 历史
cxylk 提交于 2021年01月21日 16:33 +08:00 . :rainbow: idea构建jdk源码
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
/*
* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package java.beans;
import java.util.*;
import java.lang.reflect.*;
import java.util.Objects;
import sun.reflect.misc.*;
/**
* The <code>DefaultPersistenceDelegate</code> is a concrete implementation of
* the abstract <code>PersistenceDelegate</code> class and
* is the delegate used by default for classes about
* which no information is available. The <code>DefaultPersistenceDelegate</code>
* provides, version resilient, public API-based persistence for
* classes that follow the JavaBeans&trade; conventions without any class specific
* configuration.
* <p>
* The key assumptions are that the class has a nullary constructor
* and that its state is accurately represented by matching pairs
* of "setter" and "getter" methods in the order they are returned
* by the Introspector.
* In addition to providing code-free persistence for JavaBeans,
* the <code>DefaultPersistenceDelegate</code> provides a convenient means
* to effect persistent storage for classes that have a constructor
* that, while not nullary, simply requires some property values
* as arguments.
*
* @see #DefaultPersistenceDelegate(String[])
* @see java.beans.Introspector
*
* @since 1.4
*
* @author Philip Milne
*/
public class DefaultPersistenceDelegate extends PersistenceDelegate {
private static final String[] EMPTY = {};
private final String[] constructor;
private Boolean definesEquals;
/**
* Creates a persistence delegate for a class with a nullary constructor.
*
* @see #DefaultPersistenceDelegate(java.lang.String[])
*/
public DefaultPersistenceDelegate() {
this.constructor = EMPTY;
}
/**
* Creates a default persistence delegate for a class with a
* constructor whose arguments are the values of the property
* names as specified by <code>constructorPropertyNames</code>.
* The constructor arguments are created by
* evaluating the property names in the order they are supplied.
* To use this class to specify a single preferred constructor for use
* in the serialization of a particular type, we state the
* names of the properties that make up the constructor's
* arguments. For example, the <code>Font</code> class which
* does not define a nullary constructor can be handled
* with the following persistence delegate:
*
* <pre>
* new DefaultPersistenceDelegate(new String[]{"name", "style", "size"});
* </pre>
*
* @param constructorPropertyNames The property names for the arguments of this constructor.
*
* @see #instantiate
*/
public DefaultPersistenceDelegate(String[] constructorPropertyNames) {
this.constructor = (constructorPropertyNames == null) ? EMPTY : constructorPropertyNames.clone();
}
private static boolean definesEquals(Class<?> type) {
try {
return type == type.getMethod("equals", Object.class).getDeclaringClass();
}
catch(NoSuchMethodException e) {
return false;
}
}
private boolean definesEquals(Object instance) {
if (definesEquals != null) {
return (definesEquals == Boolean.TRUE);
}
else {
boolean result = definesEquals(instance.getClass());
definesEquals = result ? Boolean.TRUE : Boolean.FALSE;
return result;
}
}
/**
* If the number of arguments in the specified constructor is non-zero and
* the class of <code>oldInstance</code> explicitly declares an "equals" method
* this method returns the value of <code>oldInstance.equals(newInstance)</code>.
* Otherwise, this method uses the superclass's definition which returns true if the
* classes of the two instances are equal.
*
* @param oldInstance The instance to be copied.
* @param newInstance The instance that is to be modified.
* @return True if an equivalent copy of <code>newInstance</code> may be
* created by applying a series of mutations to <code>oldInstance</code>.
*
* @see #DefaultPersistenceDelegate(String[])
*/
protected boolean mutatesTo(Object oldInstance, Object newInstance) {
// Assume the instance is either mutable or a singleton
// if it has a nullary constructor.
return (constructor.length == 0) || !definesEquals(oldInstance) ?
super.mutatesTo(oldInstance, newInstance) :
oldInstance.equals(newInstance);
}
/**
* This default implementation of the <code>instantiate</code> method returns
* an expression containing the predefined method name "new" which denotes a
* call to a constructor with the arguments as specified in
* the <code>DefaultPersistenceDelegate</code>'s constructor.
*
* @param oldInstance The instance to be instantiated.
* @param out The code output stream.
* @return An expression whose value is <code>oldInstance</code>.
*
* @throws NullPointerException if {@code out} is {@code null}
* and this value is used in the method
*
* @see #DefaultPersistenceDelegate(String[])
*/
protected Expression instantiate(Object oldInstance, Encoder out) {
int nArgs = constructor.length;
Class<?> type = oldInstance.getClass();
Object[] constructorArgs = new Object[nArgs];
for(int i = 0; i < nArgs; i++) {
try {
Method method = findMethod(type, this.constructor[i]);
constructorArgs[i] = MethodUtil.invoke(method, oldInstance, new Object[0]);
}
catch (Exception e) {
out.getExceptionListener().exceptionThrown(e);
}
}
return new Expression(oldInstance, oldInstance.getClass(), "new", constructorArgs);
}
private Method findMethod(Class<?> type, String property) {
if (property == null) {
throw new IllegalArgumentException("Property name is null");
}
PropertyDescriptor pd = getPropertyDescriptor(type, property);
if (pd == null) {
throw new IllegalStateException("Could not find property by the name " + property);
}
Method method = pd.getReadMethod();
if (method == null) {
throw new IllegalStateException("Could not find getter for the property " + property);
}
return method;
}
private void doProperty(Class<?> type, PropertyDescriptor pd, Object oldInstance, Object newInstance, Encoder out) throws Exception {
Method getter = pd.getReadMethod();
Method setter = pd.getWriteMethod();
if (getter != null && setter != null) {
Expression oldGetExp = new Expression(oldInstance, getter.getName(), new Object[]{});
Expression newGetExp = new Expression(newInstance, getter.getName(), new Object[]{});
Object oldValue = oldGetExp.getValue();
Object newValue = newGetExp.getValue();
out.writeExpression(oldGetExp);
if (!Objects.equals(newValue, out.get(oldValue))) {
// Search for a static constant with this value;
Object e = (Object[])pd.getValue("enumerationValues");
if (e instanceof Object[] && Array.getLength(e) % 3 == 0) {
Object[] a = (Object[])e;
for(int i = 0; i < a.length; i = i + 3) {
try {
Field f = type.getField((String)a[i]);
if (f.get(null).equals(oldValue)) {
out.remove(oldValue);
out.writeExpression(new Expression(oldValue, f, "get", new Object[]{null}));
}
}
catch (Exception ex) {}
}
}
invokeStatement(oldInstance, setter.getName(), new Object[]{oldValue}, out);
}
}
}
static void invokeStatement(Object instance, String methodName, Object[] args, Encoder out) {
out.writeStatement(new Statement(instance, methodName, args));
}
// Write out the properties of this instance.
private void initBean(Class<?> type, Object oldInstance, Object newInstance, Encoder out) {
for (Field field : type.getFields()) {
if (!ReflectUtil.isPackageAccessible(field.getDeclaringClass())) {
continue;
}
int mod = field.getModifiers();
if (Modifier.isFinal(mod) || Modifier.isStatic(mod) || Modifier.isTransient(mod)) {
continue;
}
try {
Expression oldGetExp = new Expression(field, "get", new Object[] { oldInstance });
Expression newGetExp = new Expression(field, "get", new Object[] { newInstance });
Object oldValue = oldGetExp.getValue();
Object newValue = newGetExp.getValue();
out.writeExpression(oldGetExp);
if (!Objects.equals(newValue, out.get(oldValue))) {
out.writeStatement(new Statement(field, "set", new Object[] { oldInstance, oldValue }));
}
}
catch (Exception exception) {
out.getExceptionListener().exceptionThrown(exception);
}
}
BeanInfo info;
try {
info = Introspector.getBeanInfo(type);
} catch (IntrospectionException exception) {
return;
}
// Properties
for (PropertyDescriptor d : info.getPropertyDescriptors()) {
if (d.isTransient()) {
continue;
}
try {
doProperty(type, d, oldInstance, newInstance, out);
}
catch (Exception e) {
out.getExceptionListener().exceptionThrown(e);
}
}
// Listeners
/*
Pending(milne). There is a general problem with the archival of
listeners which is unresolved as of 1.4. Many of the methods
which install one object inside another (typically "add" methods
or setters) automatically install a listener on the "child" object
so that its "parent" may respond to changes that are made to it.
For example the JTable:setModel() method automatically adds a
TableModelListener (the JTable itself in this case) to the supplied
table model.
We do not need to explicitly add these listeners to the model in an
archive as they will be added automatically by, in the above case,
the JTable's "setModel" method. In some cases, we must specifically
avoid trying to do this since the listener may be an inner class
that cannot be instantiated using public API.
No general mechanism currently
exists for differentiating between these kind of listeners and
those which were added explicitly by the user. A mechanism must
be created to provide a general means to differentiate these
special cases so as to provide reliable persistence of listeners
for the general case.
*/
if (!java.awt.Component.class.isAssignableFrom(type)) {
return; // Just handle the listeners of Components for now.
}
for (EventSetDescriptor d : info.getEventSetDescriptors()) {
if (d.isTransient()) {
continue;
}
Class<?> listenerType = d.getListenerType();
// The ComponentListener is added automatically, when
// Contatiner:add is called on the parent.
if (listenerType == java.awt.event.ComponentListener.class) {
continue;
}
// JMenuItems have a change listener added to them in
// their "add" methods to enable accessibility support -
// see the add method in JMenuItem for details. We cannot
// instantiate this instance as it is a private inner class
// and do not need to do this anyway since it will be created
// and installed by the "add" method. Special case this for now,
// ignoring all change listeners on JMenuItems.
if (listenerType == javax.swing.event.ChangeListener.class &&
type == javax.swing.JMenuItem.class) {
continue;
}
EventListener[] oldL = new EventListener[0];
EventListener[] newL = new EventListener[0];
try {
Method m = d.getGetListenerMethod();
oldL = (EventListener[])MethodUtil.invoke(m, oldInstance, new Object[]{});
newL = (EventListener[])MethodUtil.invoke(m, newInstance, new Object[]{});
}
catch (Exception e2) {
try {
Method m = type.getMethod("getListeners", new Class<?>[]{Class.class});
oldL = (EventListener[])MethodUtil.invoke(m, oldInstance, new Object[]{listenerType});
newL = (EventListener[])MethodUtil.invoke(m, newInstance, new Object[]{listenerType});
}
catch (Exception e3) {
return;
}
}
// Asssume the listeners are in the same order and that there are no gaps.
// Eventually, this may need to do true differencing.
String addListenerMethodName = d.getAddListenerMethod().getName();
for (int i = newL.length; i < oldL.length; i++) {
// System.out.println("Adding listener: " + addListenerMethodName + oldL[i]);
invokeStatement(oldInstance, addListenerMethodName, new Object[]{oldL[i]}, out);
}
String removeListenerMethodName = d.getRemoveListenerMethod().getName();
for (int i = oldL.length; i < newL.length; i++) {
invokeStatement(oldInstance, removeListenerMethodName, new Object[]{newL[i]}, out);
}
}
}
/**
* This default implementation of the <code>initialize</code> method assumes
* all state held in objects of this type is exposed via the
* matching pairs of "setter" and "getter" methods in the order
* they are returned by the Introspector. If a property descriptor
* defines a "transient" attribute with a value equal to
* <code>Boolean.TRUE</code> the property is ignored by this
* default implementation. Note that this use of the word
* "transient" is quite independent of the field modifier
* that is used by the <code>ObjectOutputStream</code>.
* <p>
* For each non-transient property, an expression is created
* in which the nullary "getter" method is applied
* to the <code>oldInstance</code>. The value of this
* expression is the value of the property in the instance that is
* being serialized. If the value of this expression
* in the cloned environment <code>mutatesTo</code> the
* target value, the new value is initialized to make it
* equivalent to the old value. In this case, because
* the property value has not changed there is no need to
* call the corresponding "setter" method and no statement
* is emitted. If not however, the expression for this value
* is replaced with another expression (normally a constructor)
* and the corresponding "setter" method is called to install
* the new property value in the object. This scheme removes
* default information from the output produced by streams
* using this delegate.
* <p>
* In passing these statements to the output stream, where they
* will be executed, side effects are made to the <code>newInstance</code>.
* In most cases this allows the problem of properties
* whose values depend on each other to actually help the
* serialization process by making the number of statements
* that need to be written to the output smaller. In general,
* the problem of handling interdependent properties is reduced to
* that of finding an order for the properties in
* a class such that no property value depends on the value of
* a subsequent property.
*
* @param type the type of the instances
* @param oldInstance The instance to be copied.
* @param newInstance The instance that is to be modified.
* @param out The stream to which any initialization statements should be written.
*
* @throws NullPointerException if {@code out} is {@code null}
*
* @see java.beans.Introspector#getBeanInfo
* @see java.beans.PropertyDescriptor
*/
protected void initialize(Class<?> type,
Object oldInstance, Object newInstance,
Encoder out)
{
// System.out.println("DefulatPD:initialize" + type);
super.initialize(type, oldInstance, newInstance, out);
if (oldInstance.getClass() == type) { // !type.isInterface()) {
initBean(type, oldInstance, newInstance, out);
}
}
private static PropertyDescriptor getPropertyDescriptor(Class<?> type, String property) {
try {
for (PropertyDescriptor pd : Introspector.getBeanInfo(type).getPropertyDescriptors()) {
if (property.equals(pd.getName()))
return pd;
}
} catch (IntrospectionException exception) {
}
return null;
}
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

Java学习过程中遇到的知识点总结,复习笔记
暂无标签
Apache-2.0
使用 Apache-2.0 开源许可协议
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/machunpeng/Java-Notes.git
git@gitee.com:machunpeng/Java-Notes.git
machunpeng
Java-Notes
Java-Notes
main
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /