From 241b91b90165ff1aefa7d7e20e650f045ebfd927 Mon Sep 17 00:00:00 2001 From: Eli Hart Date: Wed, 17 Aug 2016 11:48:27 -0700 Subject: [PATCH] Add error check for inner classes in processor --- .../com/airbnb/epoxy/BoundViewHolders.java | 16 +----------- .../java/com/airbnb/epoxy/DiffHelper.java | 16 +----------- .../java/com/airbnb/epoxy/EpoxyAdapter.java | 16 +----------- .../com/airbnb/epoxy/EpoxyItemAnimator.java | 16 +----------- .../com/airbnb/epoxy/EpoxyViewHolder.java | 16 +----------- .../com/airbnb/epoxy/HiddenEpoxyModel.java | 16 +----------- .../java/com/airbnb/epoxy/ModelState.java | 15 ----------- .../com/airbnb/epoxy/SimpleEpoxyModel.java | 15 ----------- .../main/java/com/airbnb/epoxy/UpdateOp.java | 16 +----------- .../com/airbnb/epoxy/ViewHolderState.java | 16 +----------- .../java/com/airbnb/epoxy/EpoxyAttribute.java | 16 +----------- .../java/com/airbnb/epoxy/EpoxyModel.java | 15 ----------- .../java/com/airbnb/epoxy/AttributeInfo.java | 15 ----------- .../com/airbnb/epoxy/ClassToGenerateInfo.java | 16 +----------- .../java/com/airbnb/epoxy/EpoxyProcessor.java | 25 +++++++------------ .../airbnb/epoxy/EpoxyProcessorException.java | 16 +----------- .../com/airbnb/epoxy/EpoxyProcessorTest.java | 12 +++++++++ .../src/test/resources/ModelAsInnerClass.java | 13 ++++++++++ .../resources/ModelWithPrivateInnerClass.java | 2 +- 19 files changed, 46 insertions(+), 242 deletions(-) create mode 100644 epoxy-processor/src/test/resources/ModelAsInnerClass.java diff --git a/epoxy-adapter/src/main/java/com/airbnb/epoxy/BoundViewHolders.java b/epoxy-adapter/src/main/java/com/airbnb/epoxy/BoundViewHolders.java index d0b980ff52..570e49a242 100755 --- a/epoxy-adapter/src/main/java/com/airbnb/epoxy/BoundViewHolders.java +++ b/epoxy-adapter/src/main/java/com/airbnb/epoxy/BoundViewHolders.java @@ -1,18 +1,4 @@ -/* - * Copyright (C) 2016 Airbnb, Inc. - * - * 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.airbnb.epoxy; import android.support.annotation.Nullable; diff --git a/epoxy-adapter/src/main/java/com/airbnb/epoxy/DiffHelper.java b/epoxy-adapter/src/main/java/com/airbnb/epoxy/DiffHelper.java index 59f8481a90..b331244468 100755 --- a/epoxy-adapter/src/main/java/com/airbnb/epoxy/DiffHelper.java +++ b/epoxy-adapter/src/main/java/com/airbnb/epoxy/DiffHelper.java @@ -1,18 +1,4 @@ -/* - * Copyright (C) 2016 Airbnb, Inc. - * - * 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.airbnb.epoxy; import android.support.annotation.Nullable; diff --git a/epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyAdapter.java b/epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyAdapter.java index c97d078f25..9db40aaf2f 100755 --- a/epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyAdapter.java +++ b/epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyAdapter.java @@ -1,18 +1,4 @@ -/* - * Copyright (C) 2016 Airbnb, Inc. - * - * 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.airbnb.epoxy; import android.os.Bundle; diff --git a/epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyItemAnimator.java b/epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyItemAnimator.java index 5ebd615b38..a0907d91a0 100755 --- a/epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyItemAnimator.java +++ b/epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyItemAnimator.java @@ -1,18 +1,4 @@ -/* - * Copyright (C) 2016 Airbnb, Inc. - * - * 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.airbnb.epoxy; import android.support.annotation.NonNull; diff --git a/epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyViewHolder.java b/epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyViewHolder.java index 2a8b1b04e6..2de5a8db94 100755 --- a/epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyViewHolder.java +++ b/epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyViewHolder.java @@ -1,18 +1,4 @@ -/* - * Copyright (C) 2016 Airbnb, Inc. - * - * 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.airbnb.epoxy; import android.support.annotation.LayoutRes; diff --git a/epoxy-adapter/src/main/java/com/airbnb/epoxy/HiddenEpoxyModel.java b/epoxy-adapter/src/main/java/com/airbnb/epoxy/HiddenEpoxyModel.java index 0f97190778..a222db17bc 100755 --- a/epoxy-adapter/src/main/java/com/airbnb/epoxy/HiddenEpoxyModel.java +++ b/epoxy-adapter/src/main/java/com/airbnb/epoxy/HiddenEpoxyModel.java @@ -1,18 +1,4 @@ -/* - * Copyright (C) 2016 Airbnb, Inc. - * - * 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.airbnb.epoxy; import android.support.v4.widget.Space; diff --git a/epoxy-adapter/src/main/java/com/airbnb/epoxy/ModelState.java b/epoxy-adapter/src/main/java/com/airbnb/epoxy/ModelState.java index 98a76c9d2e..bed188b7e2 100755 --- a/epoxy-adapter/src/main/java/com/airbnb/epoxy/ModelState.java +++ b/epoxy-adapter/src/main/java/com/airbnb/epoxy/ModelState.java @@ -1,18 +1,3 @@ -/* - * Copyright (C) 2016 Airbnb, Inc. - * - * 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.airbnb.epoxy; import android.support.v4.util.Pools; diff --git a/epoxy-adapter/src/main/java/com/airbnb/epoxy/SimpleEpoxyModel.java b/epoxy-adapter/src/main/java/com/airbnb/epoxy/SimpleEpoxyModel.java index bd6bf28dc8..baf8aa42ee 100755 --- a/epoxy-adapter/src/main/java/com/airbnb/epoxy/SimpleEpoxyModel.java +++ b/epoxy-adapter/src/main/java/com/airbnb/epoxy/SimpleEpoxyModel.java @@ -1,18 +1,3 @@ -/* - * Copyright (C) 2016 Airbnb, Inc. - * - * 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.airbnb.epoxy; import android.support.annotation.LayoutRes; diff --git a/epoxy-adapter/src/main/java/com/airbnb/epoxy/UpdateOp.java b/epoxy-adapter/src/main/java/com/airbnb/epoxy/UpdateOp.java index ef3d20ab86..2f48f16d60 100755 --- a/epoxy-adapter/src/main/java/com/airbnb/epoxy/UpdateOp.java +++ b/epoxy-adapter/src/main/java/com/airbnb/epoxy/UpdateOp.java @@ -1,18 +1,4 @@ -/* - * Copyright (C) 2016 Airbnb, Inc. - * - * 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.airbnb.epoxy; import android.support.annotation.IntDef; diff --git a/epoxy-adapter/src/main/java/com/airbnb/epoxy/ViewHolderState.java b/epoxy-adapter/src/main/java/com/airbnb/epoxy/ViewHolderState.java index 2a0e6e47ad..801b1d9278 100755 --- a/epoxy-adapter/src/main/java/com/airbnb/epoxy/ViewHolderState.java +++ b/epoxy-adapter/src/main/java/com/airbnb/epoxy/ViewHolderState.java @@ -1,18 +1,4 @@ -/* - * Copyright (C) 2016 Airbnb, Inc. - * - * 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.airbnb.epoxy; import android.os.Parcel; diff --git a/epoxy-model/src/main/java/com/airbnb/epoxy/EpoxyAttribute.java b/epoxy-model/src/main/java/com/airbnb/epoxy/EpoxyAttribute.java index 4d4f1649d2..b5d8858c34 100755 --- a/epoxy-model/src/main/java/com/airbnb/epoxy/EpoxyAttribute.java +++ b/epoxy-model/src/main/java/com/airbnb/epoxy/EpoxyAttribute.java @@ -1,18 +1,4 @@ -/* - * Copyright (C) 2016 Airbnb, Inc. - * - * 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.airbnb.epoxy; import java.lang.annotation.ElementType; diff --git a/epoxy-model/src/main/java/com/airbnb/epoxy/EpoxyModel.java b/epoxy-model/src/main/java/com/airbnb/epoxy/EpoxyModel.java index 80385b6651..df516c3c26 100755 --- a/epoxy-model/src/main/java/com/airbnb/epoxy/EpoxyModel.java +++ b/epoxy-model/src/main/java/com/airbnb/epoxy/EpoxyModel.java @@ -1,18 +1,3 @@ -/* - * Copyright (C) 2016 Airbnb, Inc. - * - * 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.airbnb.epoxy; import android.support.annotation.LayoutRes; diff --git a/epoxy-processor/src/main/java/com/airbnb/epoxy/AttributeInfo.java b/epoxy-processor/src/main/java/com/airbnb/epoxy/AttributeInfo.java index b315347bbc..7d43ef0b83 100755 --- a/epoxy-processor/src/main/java/com/airbnb/epoxy/AttributeInfo.java +++ b/epoxy-processor/src/main/java/com/airbnb/epoxy/AttributeInfo.java @@ -1,18 +1,3 @@ -/* - * Copyright (C) 2016 Airbnb, Inc. - * - * 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.airbnb.epoxy; import com.squareup.javapoet.AnnotationSpec; diff --git a/epoxy-processor/src/main/java/com/airbnb/epoxy/ClassToGenerateInfo.java b/epoxy-processor/src/main/java/com/airbnb/epoxy/ClassToGenerateInfo.java index 252d744348..d61fa1e56d 100755 --- a/epoxy-processor/src/main/java/com/airbnb/epoxy/ClassToGenerateInfo.java +++ b/epoxy-processor/src/main/java/com/airbnb/epoxy/ClassToGenerateInfo.java @@ -1,18 +1,4 @@ -/* - * Copyright (C) 2016 Airbnb, Inc. - * - * 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.airbnb.epoxy; import com.squareup.javapoet.ClassName; diff --git a/epoxy-processor/src/main/java/com/airbnb/epoxy/EpoxyProcessor.java b/epoxy-processor/src/main/java/com/airbnb/epoxy/EpoxyProcessor.java index 18010e6080..1b8f7dc40e 100755 --- a/epoxy-processor/src/main/java/com/airbnb/epoxy/EpoxyProcessor.java +++ b/epoxy-processor/src/main/java/com/airbnb/epoxy/EpoxyProcessor.java @@ -1,18 +1,3 @@ -/* - * Copyright (C) 2016 Airbnb, Inc. - * - * 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.airbnb.epoxy; import android.support.annotation.LayoutRes; @@ -178,7 +163,15 @@ private void validateAccessibleViaGeneratedCode(Element attribute) throws enclosingElement.getSimpleName(), attribute.getSimpleName()); } -// enclosingElement.getNestingKind() + // Nested classes must be static + if (enclosingElement.getNestingKind().isNested()) { + if (!enclosingElement.getModifiers().contains(STATIC)) { + throwError( + "Nested classes with %s annotations must be static. (class: %s, field: %s)", + EpoxyAttribute.class.getSimpleName(), + enclosingElement.getSimpleName(), attribute.getSimpleName()); + } + } // Verify containing type. if (enclosingElement.getKind() != CLASS) { diff --git a/epoxy-processor/src/main/java/com/airbnb/epoxy/EpoxyProcessorException.java b/epoxy-processor/src/main/java/com/airbnb/epoxy/EpoxyProcessorException.java index 77322095b7..44fa851ff2 100755 --- a/epoxy-processor/src/main/java/com/airbnb/epoxy/EpoxyProcessorException.java +++ b/epoxy-processor/src/main/java/com/airbnb/epoxy/EpoxyProcessorException.java @@ -1,18 +1,4 @@ -/* - * Copyright (C) 2016 Airbnb, Inc. - * - * 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.airbnb.epoxy; class EpoxyProcessorException extends RuntimeException { diff --git a/epoxy-processor/src/test/java/com/airbnb/epoxy/EpoxyProcessorTest.java b/epoxy-processor/src/test/java/com/airbnb/epoxy/EpoxyProcessorTest.java index 08d5957ae1..bef9a021a9 100755 --- a/epoxy-processor/src/test/java/com/airbnb/epoxy/EpoxyProcessorTest.java +++ b/epoxy-processor/src/test/java/com/airbnb/epoxy/EpoxyProcessorTest.java @@ -188,4 +188,16 @@ public void testModelThatDoesNotExtendEpoxyModelFails() { .failsToCompile() .withErrorContaining("must extend"); } + + @Test + public void testModelAsInnerClassFails() { + JavaFileObject model = JavaFileObjects + .forResource("ModelAsInnerClass.java"); + + assert_().about(javaSource()) + .that(model) + .processedWith(new EpoxyProcessor()) + .failsToCompile() + .withErrorContaining("Nested classes"); + } } \ No newline at end of file diff --git a/epoxy-processor/src/test/resources/ModelAsInnerClass.java b/epoxy-processor/src/test/resources/ModelAsInnerClass.java new file mode 100644 index 0000000000..342d2cec8d --- /dev/null +++ b/epoxy-processor/src/test/resources/ModelAsInnerClass.java @@ -0,0 +1,13 @@ +package com.airbnb.epoxy; + +public class ModelAsInnerClass { + + class InnerClass extends EpoxyModel { + @EpoxyAttribute int valueInt; + + @Override + protected int getDefaultLayout() { + return 0; + } + } +} \ No newline at end of file diff --git a/epoxy-processor/src/test/resources/ModelWithPrivateInnerClass.java b/epoxy-processor/src/test/resources/ModelWithPrivateInnerClass.java index 5dbe7a09ae..71e0fb70d7 100755 --- a/epoxy-processor/src/test/resources/ModelWithPrivateInnerClass.java +++ b/epoxy-processor/src/test/resources/ModelWithPrivateInnerClass.java @@ -7,7 +7,7 @@ protected int getDefaultLayout() { return 0; } - private class Test extends EpoxyModel { + private static class Test extends EpoxyModel { @EpoxyAttribute int value; @Override