diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml index 4e1d9e34218e..6bde0a908ba4 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml @@ -2214,6 +2214,10 @@ key="serializable.inner.class.with.non.serializable.outer.class.display.name" groupBundle="messages.InspectionsBundle" groupKey="group.names.serialization.issues" enabledByDefault="false" level="WARNING" implementationClass="com.siyeh.ig.serialization.SerializableInnerClassWithNonSerializableOuterClassInspection"/> + + +Checks lambda's, local and anonymous classes and reports references to local variables outside the class or lambda that are not +Serializable. +When a local variable is referenced from an anonymous class, it's value is stored in an implicit field in the anonymous class. +The same happens for local classes and lambda's. +If that variable is of a non-Serializable type, serialization will fail. +

+New in 14 + + \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/serialization/serializable_stores_non_serializable/SerializableStoresNonSerializable.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/serialization/serializable_stores_non_serializable/SerializableStoresNonSerializable.java new file mode 100644 index 000000000000..dd36daacc3ba --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/serialization/serializable_stores_non_serializable/SerializableStoresNonSerializable.java @@ -0,0 +1,28 @@ +class SerializableStoresNonSerializable { + + void f(B b, C c) { + new A() { + @Override + public void m() { + System.out.println(c); + System.out.println(b); + } + }; + A a = () -> { + System.out.println(c); + System.out.println(b); + }; + class D implements A { + @Override + public void m() { + System.out.println(c); + System.out.println(b); + } + } + } +} +interface A { + void m(); +} +class B {} +class C implements java.io.Serializable {} \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/serialization/SerializableStoresNonSerializableInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/serialization/SerializableStoresNonSerializableInspectionTest.java new file mode 100644 index 000000000000..b6d878a2e430 --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/serialization/SerializableStoresNonSerializableInspectionTest.java @@ -0,0 +1,33 @@ +/* + * Copyright 2000-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.siyeh.ig.serialization; + +import com.intellij.codeInspection.InspectionProfileEntry; +import com.siyeh.ig.LightInspectionTestCase; + +/** + * @author Bas Leijdekkers + */ +public class SerializableStoresNonSerializableInspectionTest extends LightInspectionTestCase { + @Override + protected InspectionProfileEntry getInspection() { + return new SerializableStoresNonSerializableInspection(); + } + + public void testSerializableStoresNonSerializable() { + doTest(); + } +}