Fields in a “Serializable” class should either be transient or serializable
嗨,我在声纳线头中看到这个错误:
"可序列化"类中的字段应该是临时的或可序列化
我的代码是:
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 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 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 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 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 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 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 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 | package com.cgi.atom.common.priorityexec; /** * Created by nageswararao.vesepog on 8/24/2016. */ import java.util.*; import java.util.concurrent.BlockingDeque; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; public class PriorityBlockingDeque<E> extends AbstractQueue<E> implements BlockingDeque<E>, java.io.Serializable { /* * Implemented as a navigable set protected by a * single lock and using conditions to manage blocking. */ private final int capacity; private final LinkedList<E> list; /** * Main lock guarding all access */ private final ReentrantLock lock = new ReentrantLock(); /** * Condition for waiting takes */ private final Condition notEmpty = lock.newCondition(); /** * Condition for waiting puts */ private final Condition notFull = lock.newCondition(); private Comparator<E> comparator; /** * Creates a <tt>PriorityBlockingDeque</tt> with a capacity of * {@link Integer#MAX_VALUE}. */ public PriorityBlockingDeque() { this(null, Integer.MAX_VALUE); } /** * Creates a <tt>PriorityBlockingDeque</tt> with the given (fixed) capacity. * * @param capacity the capacity of this deque * @throws IllegalArgumentException if <tt>capacity</tt> is less than 1 */ public PriorityBlockingDeque(int capacity) { this(null, capacity); } public PriorityBlockingDeque(Comparator<E> comparator, int capacity) { if (capacity <= 0) throw new IllegalArgumentException(); this.capacity = capacity; this.list = new LinkedList<E>(); this.comparator = comparator; } // Basic adding and removing operations, called only while holding lock /** * Adds e or returns false if full. * * @param e The element to add. * @return Whether adding was successful. */ private boolean innerAdd(E e) { if (list.size() >= capacity) return false; int insertionPoint = Collections.binarySearch(list, e, comparator); if (insertionPoint < 0) { // this means the key didn't exist, so the insertion point is negative minus 1. insertionPoint = -insertionPoint - 1; } list.add(insertionPoint, e); notEmpty.signal(); return true; } /** * Removes and returns first element, or null if empty. * * @return The removed element. */ private E innerRemoveFirst() { E f = list.pollFirst(); if (f == null) return null; notFull.signal(); return f; } /** * Removes and returns last element, or null if empty. * * @return The removed element. */ private E innerRemoveLast() { E l = list.pollLast(); if (l == null) return null; notFull.signal(); return l; } // BlockingDeque methods /** * @throws IllegalStateException {@inheritDoc} * @throws NullPointerException {@inheritDoc} */ public void addFirst(E e) { if (!offerFirst(e)) throw new IllegalStateException("Deque full"); } /** * @throws IllegalStateException {@inheritDoc} * @throws NullPointerException {@inheritDoc} */ public void addLast(E e) { if (!offerLast(e)) throw new IllegalStateException("Deque full"); } /** * @throws NullPointerException {@inheritDoc} */ public boolean offerFirst(E e) { if (e == null) throw new NullPointerException(); lock.lock(); try { return innerAdd(e); } finally { lock.unlock(); } } /** * @throws NullPointerException {@inheritDoc} */ public boolean offerLast(E e) { if (e == null) throw new NullPointerException(); lock.lock(); try { return innerAdd(e); } finally { lock.unlock(); } } /** * @throws NullPointerException {@inheritDoc} * @throws InterruptedException {@inheritDoc} */ public void putFirst(E e) throws InterruptedException { if (e == null) throw new NullPointerException(); lock.lock(); try { while (!innerAdd(e)) notFull.await(); } finally { lock.unlock(); } } /** * @throws NullPointerException {@inheritDoc} * @throws InterruptedException {@inheritDoc} */ public void putLast(E e) throws InterruptedException { if (e == null) throw new NullPointerException(); lock.lock(); try { while (!innerAdd(e)) notFull.await(); } finally { lock.unlock(); } } /** * @throws NullPointerException {@inheritDoc} * @throws InterruptedException {@inheritDoc} */ public boolean offerFirst(E e, long timeout, TimeUnit unit) throws InterruptedException { if (e == null) throw new NullPointerException(); long nanos = unit.toNanos(timeout); lock.lockInterruptibly(); try { for (; ;) { if (innerAdd(e)) return true; if (nanos <= 0) return false; nanos = notFull.awaitNanos(nanos); } } finally { lock.unlock(); } } /** * @throws NullPointerException {@inheritDoc} * @throws InterruptedException {@inheritDoc} */ public boolean offerLast(E e, long timeout, TimeUnit unit) throws InterruptedException { if (e == null) throw new NullPointerException(); long nanos = unit.toNanos(timeout); lock.lockInterruptibly(); try { for (; ;) { if (innerAdd(e)) return true; if (nanos <= 0) return false; nanos = notFull.awaitNanos(nanos); } } finally { lock.unlock(); } } /** * @throws NoSuchElementException {@inheritDoc} */ public E removeFirst() { E x = pollFirst(); if (x == null) throw new NoSuchElementException(); return x; } /** * @throws NoSuchElementException {@inheritDoc} */ public E removeLast() { E x = pollLast(); if (x == null) throw new NoSuchElementException(); return x; } public E pollFirst() { lock.lock(); try { return innerRemoveFirst(); } finally { lock.unlock(); } } public E pollLast() { lock.lock(); try { return innerRemoveLast(); } finally { lock.unlock(); } } public E takeFirst() throws InterruptedException { lock.lock(); try { E x; while ((x = innerRemoveFirst()) == null) notEmpty.await(); return x; } finally { lock.unlock(); } } public E takeLast() throws InterruptedException { lock.lock(); try { E x; while ((x = innerRemoveLast()) == null) notEmpty.await(); return x; } finally { lock.unlock(); } } public E pollFirst(long timeout, TimeUnit unit) throws InterruptedException { long nanos = unit.toNanos(timeout); lock.lockInterruptibly(); try { for (; ;) { E x = innerRemoveFirst(); if (x != null) return x; if (nanos <= 0) return null; nanos = notEmpty.awaitNanos(nanos); } } finally { lock.unlock(); } } public E pollLast(long timeout, TimeUnit unit) throws InterruptedException { long nanos = unit.toNanos(timeout); lock.lockInterruptibly(); try { for (; ;) { E x = innerRemoveLast(); if (x != null) return x; if (nanos <= 0) return null; nanos = notEmpty.awaitNanos(nanos); } } finally { lock.unlock(); } } /** * @throws NoSuchElementException {@inheritDoc} */ public E getFirst() { E x = peekFirst(); if (x == null) throw new NoSuchElementException(); return x; } /** * @throws NoSuchElementException {@inheritDoc} */ public E getLast() { E x = peekLast(); if (x == null) throw new NoSuchElementException(); return x; } public E peekFirst() { lock.lock(); try { return list.size() == 0 ? null : list.peekFirst(); } finally { lock.unlock(); } } public E peekLast() { lock.lock(); try { return list.size() == 0 ? null : list.peekLast(); } finally { lock.unlock(); } } public boolean removeFirstOccurrence(Object o) { if (o == null) return false; lock.lock(); try { for (Iterator<E> it = list.iterator(); it.hasNext();) { E e = it.next(); if (o.equals(e)) { it.remove(); return true; } } return false; } finally { lock.unlock(); } } public boolean removeLastOccurrence(Object o) { if (o == null) return false; lock.lock(); try { for (Iterator<E> it = list.descendingIterator(); it.hasNext();) { E e = it.next(); if (o.equals(e)) { it.remove(); return true; } } return false; } finally { lock.unlock(); } } // BlockingQueue methods /** * Inserts the specified element to the deque unless it would * violate capacity restrictions. When using a capacity-restricted deque, * it is generally preferable to use method {@link #offer(Object) offer}. * <p/> * <p> This method is equivalent to {@link #addLast}. * * @throws IllegalStateException if the element cannot be added at this * time due to capacity restrictions * @throws NullPointerException if the specified element is null */ @Override public boolean add(E e) { addLast(e); return true; } /** * @throws NullPointerException if the specified element is null */ public boolean offer(E e) { return offerLast(e); } /** * @throws NullPointerException {@inheritDoc} * @throws InterruptedException {@inheritDoc} */ public void put(E e) throws InterruptedException { putLast(e); } /** * @throws NullPointerException {@inheritDoc} * @throws InterruptedException {@inheritDoc} */ public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException { return offerLast(e, timeout, unit); } /** * Retrieves and removes the head of the queue represented by this deque. * This method differs from {@link #poll poll} only in that it throws an * exception if this deque is empty. * <p/> * <p> This method is equivalent to {@link #removeFirst() removeFirst}. * * @return the head of the queue represented by this deque * @throws NoSuchElementException if this deque is empty */ @Override public E remove() { return removeFirst(); } public E poll() { return pollFirst(); } public E take() throws InterruptedException { return takeFirst(); } public E poll(long timeout, TimeUnit unit) throws InterruptedException { return pollFirst(timeout, unit); } /** * Retrieves, but does not remove, the head of the queue represented by * this deque. This method differs from {@link #peek peek} only in that * it throws an exception if this deque is empty. * <p/> * <p> This method is equivalent to {@link #getFirst() getFirst}. * * @return the head of the queue represented by this deque * @throws NoSuchElementException if this deque is empty */ @Override public E element() { return getFirst(); } public E peek() { return peekFirst(); } /** * Returns the number of additional elements that this deque can ideally * (in the absence of memory or resource constraints) accept without * blocking. This is always equal to the initial capacity of this deque * less the current <tt>size</tt> of this deque. * <p/> * <p> Note that you cannot always tell if an attempt to insert * an element will succeed by inspecting <tt>remainingCapacity</tt> * because it may be the case that another thread is about to * insert or remove an element. */ public int remainingCapacity() { lock.lock(); try { return capacity - list.size(); } finally { lock.unlock(); } } /** * @throws UnsupportedOperationException {@inheritDoc} * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} */ public int drainTo(Collection<? super E> c) { if (c==null) throw new NullPointerException(); if (c.equals(this)) throw new IllegalArgumentException(); lock.lock(); try { for (E e : list) { c.add(e); } int n = list.size(); list.clear(); notFull.signalAll(); return n; } finally { lock.unlock(); } } /** * @throws UnsupportedOperationException {@inheritDoc} * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} */ public int drainTo(Collection<? super E> c, int maxElements) { if (c ==null) throw new NullPointerException(); if (c.equals(this)) throw new IllegalArgumentException(); lock.lock(); try { int n = 0; for (Iterator<E> it = list.iterator(); n < maxElements && it.hasNext();) { E e = it.next(); c.add(e); it.remove(); ++n; } notFull.signalAll(); return n; } finally { lock.unlock(); } } // Stack methods /** * @throws IllegalStateException {@inheritDoc} * @throws NullPointerException {@inheritDoc} */ public void push(E e) { addFirst(e); } /** * @throws NoSuchElementException {@inheritDoc} */ public E pop() { return removeFirst(); } // Collection methods /** * Removes the first occurrence of the specified element from this deque. * If the deque does not contain the element, it is unchanged. * More formally, removes the first element <tt>e</tt> such that * <tt>o.equals(e)</tt> (if such an element exists). * Returns <tt>true</tt> if this deque contained the specified element * (or equivalently, if this deque changed as a result of the call). * <p/> * <p> This method is equivalent to * {@link #removeFirstOccurrence(Object) removeFirstOccurrence}. * * @param o element to be removed from this deque, if present * @return <tt>true</tt> if this deque changed as a result of the call */ @Override public boolean remove(Object o) { return removeFirstOccurrence(o); } /** * Returns the number of elements in this deque. * * @return the number of elements in this deque */ @Override public int size() { lock.lock(); try { return list.size(); } finally { lock.unlock(); } } /** * Returns <tt>true</tt> if this deque contains the specified element. * More formally, returns <tt>true</tt> if and only if this deque contains * at least one element <tt>e</tt> such that <tt>o.equals(e)</tt>. * * @param o object to be checked for containment in this deque * @return <tt>true</tt> if this deque contains the specified element */ @Override public boolean contains(Object o) { if (o == null) return false; lock.lock(); try { return list.contains(o); } finally { lock.unlock(); } } /** * Returns an array containing all of the elements in this deque, in * proper sequence (from first to last element). * <p/> * <p> The returned array will be"safe" in that no references to it are * maintained by this deque. (In other words, this method must allocate * a new array). The caller is thus free to modify the returned array. * <p/> * <p> This method acts as bridge between array-based and collection-based * APIs. * * @return an array containing all of the elements in this deque */ @Override public Object[] toArray() { lock.lock(); try { return list.toArray(); } finally { lock.unlock(); } } /** * Returns an array containing all of the elements in this deque, in * proper sequence; the runtime type of the returned array is that of * the specified array. If the deque fits in the specified array, it * is returned therein. Otherwise, a new array is allocated with the * runtime type of the specified array and the size of this deque. * <p/> * <p> If this deque fits in the specified array with room to spare * (i.e., the array has more elements than this deque), the element in * the array immediately following the end of the deque is set to * <tt>null</tt>. * <p/> * <p> Like the {@link #toArray()} method, this method acts as bridge between * array-based and collection-based APIs. Further, this method allows * precise control over the runtime type of the output array, and may, * under certain circumstances, be used to save allocation costs. * <p/> * <p> Suppose <tt>x</tt> is a deque known to contain only strings. * The following code can be used to dump the deque into a newly * allocated array of <tt>String</tt>: * <p/> * [cc lang="java"] * String[] y = x.toArray(new String[0]); |
*P/>*注意,toarray(new object[0])在函数to中是相同的*toarray().。**@参数a deque元素要放入的数组*如果足够大,则存储;否则,将*为此目的分配了相同的运行时类型*@返回包含此deque中所有元素的数组*@如果指定数组的运行时类型为*不是中每个元素的运行时类型的父类型*这个德克*@如果指定的数组为空,则引发NullPointerException*/@覆盖公共
1 2 3 4 5 6 7 8 9 10 | <P>能否有人提供解决方案,这样声纳就不会显示所有三个变量的这些误差?</P><div class="suo-content">[collapse title=""]<ul><li>"serializable"类中的sonarlint v3:fields的可能重复项对于列表接口应该是瞬态的或可序列化的</li></ul>[/collapse]</div><p><center>[wp_ad_camp_1]</center></p><hr><P>声纳已经给了你两个解决方案。</P><li>使它们可序列化</li><li>使它们暂时</li><P>你不能做前者,因为它们不是你写的课程,所以你需要让它们暂时消失。在Java中关键字"瞬变"是什么意思?</P><P>或者,如果您不需要序列化任何<wyn>PriorityBlockingDeque</wyn>,那么只需删除该接口。</P><P>这是一个警告的原因是,应该如何序列化包含不可序列化组件的类?</P><div class="suo-content">[collapse title=""]<ul><li>为什么我不能选择选项1?你能解释更多吗</li><li>@用户3766619当然。因为<wyn>Condition</wyn>是一个接口,您不是它的作者。它是JDK的一部分。你不能改变它。</li></ul>[/collapse]</div><hr><P>可序列化类中的字段本身必须是可序列化的或瞬时的,即使该类从未显式序列化或反序列化。例如,在负载下,大多数J2EE应用程序框架将对象刷新到磁盘上,而一个据称具有非瞬时、不可序列化数据成员的可序列化对象可能导致程序崩溃,并为攻击者打开大门。通常,可序列化类应该满足其约定,并且在序列化实例时不具有意外行为。</P><P>此规则在不可序列化字段上以及在集合字段不是私有字段(因为它们可以从外部分配不可序列化的值)以及在类中分配不可序列化的类型时引发问题。</P><P>不符合代码示例</P>[cc lang="java"]public class Address { //... } public class Person implements Serializable { private static final long serialVersionUID = 1905122041950251207L; private String name; private Address address; // Noncompliant; Address isn't serializable } |
例外
使所有成员都可序列化或瞬态的替代方法是实现特殊方法,这些方法负责正确序列化和反序列化对象。此规则忽略实现以下方法的类:
1 2 3 4 | private void writeObject(java.io.ObjectOutputStream out) throws IOException private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException; |
号
引用:"serializable"类中的字段应该是瞬态的或可序列化的(squid:s1948)