Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

Commit df330d2

Browse files
committed
Merge pull request #1608 from dodgex/1532_add_option_to_remove_final_modifier_from_generated_classes
1532 add option to remove final modifier from generated classes
2 parents d9d35f3 + 01efc69 commit df330d2

3 files changed

Lines changed: 73 additions & 2 deletions

File tree

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/holder/BaseGeneratedClassHolder.java‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import javax.lang.model.element.TypeElement;
3131

3232
import org.androidannotations.AndroidAnnotationsEnvironment;
33+
import org.androidannotations.Option;
3334
import org.androidannotations.helper.APTCodeModelHelper;
3435
import org.androidannotations.internal.process.ProcessHolder;
3536

@@ -41,6 +42,8 @@
4142

4243
public abstract class BaseGeneratedClassHolder implements GeneratedClassHolder {
4344

45+
public static final Option OPTION_GENERATE_FINAL_CLASSES = new Option("generateFinalClasses", "true");
46+
4447
protected final AndroidAnnotationsEnvironment environment;
4548
protected JDefinedClass generatedClass;
4649
protected AbstractJClass annotatedClass;
@@ -64,10 +67,18 @@ protected void setGeneratedClass() throws Exception {
6467
Element enclosingElement = annotatedElement.getEnclosingElement();
6568
GeneratedClassHolder enclosingHolder = environment.getGeneratedClassHolder(enclosingElement);
6669
String generatedBeanSimpleName = annotatedElement.getSimpleName().toString() + classSuffix();
67-
generatedClass = enclosingHolder.getGeneratedClass()._class(PUBLIC | FINAL | STATIC, generatedBeanSimpleName, EClassType.CLASS);
70+
int modifier = PUBLIC | STATIC;
71+
if (environment.getOptionBooleanValue(OPTION_GENERATE_FINAL_CLASSES)) {
72+
modifier |= FINAL;
73+
}
74+
generatedClass = enclosingHolder.getGeneratedClass()._class(modifier, generatedBeanSimpleName, EClassType.CLASS);
6875
} else {
6976
String generatedClassQualifiedName = annotatedComponentQualifiedName + classSuffix();
70-
generatedClass = getCodeModel()._class(PUBLIC | FINAL, generatedClassQualifiedName, EClassType.CLASS);
77+
int modifier = PUBLIC;
78+
if (environment.getOptionBooleanValue(OPTION_GENERATE_FINAL_CLASSES)) {
79+
modifier |= FINAL;
80+
}
81+
generatedClass = getCodeModel()._class(modifier, generatedClassQualifiedName, EClassType.CLASS);
7182
}
7283
codeModelHelper.generify(generatedClass, annotatedElement);
7384
setExtends();

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/Options.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import org.androidannotations.Option;
2626
import org.androidannotations.helper.ModelConstants;
27+
import org.androidannotations.holder.BaseGeneratedClassHolder;
2728
import org.androidannotations.internal.helper.AndroidManifestFinder;
2829
import org.androidannotations.internal.rclass.ProjectRClassFinder;
2930
import org.androidannotations.logger.LoggerContext;
@@ -43,6 +44,7 @@ public Options(ProcessingEnvironment processingEnvironment) {
4344
addSupportedOption(LoggerContext.OPTION_LOG_LEVEL);
4445
addSupportedOption(LoggerContext.OPTION_LOG_APPENDER_CONSOLE);
4546
addSupportedOption(LoggerContext.OPTION_LOG_APPENDER_FILE);
47+
addSupportedOption(BaseGeneratedClassHolder.OPTION_GENERATE_FINAL_CLASSES);
4648
}
4749

4850
public void addAllSupportedOptions(List<Option> options) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Copyright (C) 2010-2015 eBusiness Information, Excilys Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.generation;
17+
18+
import java.io.File;
19+
import java.io.IOException;
20+
21+
import org.androidannotations.internal.AndroidAnnotationProcessor;
22+
import org.androidannotations.testutils.AAProcessorTestHelper;
23+
import org.junit.Before;
24+
import org.junit.Test;
25+
26+
public class GenerateFinalClassesFlagTest extends AAProcessorTestHelper {
27+
28+
@Before
29+
public void setUp() {
30+
addManifestProcessorParameter(ActivityInManifest.class);
31+
addProcessor(AndroidAnnotationProcessor.class);
32+
ensureOutputDirectoryIsEmpty();
33+
}
34+
35+
@Test
36+
public void generateFinalClass() throws IOException {
37+
addProcessorParameter("generateFinalClasses", "true");
38+
39+
CompileResult result = compileFiles(ActivityInManifest.class);
40+
File generatedFile = toGeneratedFile(ActivityInManifest.class);
41+
42+
assertCompilationSuccessful(result);
43+
44+
assertGeneratedClassMatches(generatedFile, "public final class ActivityInManifest_");
45+
}
46+
47+
@Test
48+
public void generateNonFinalClass() throws IOException {
49+
addProcessorParameter("generateFinalClasses", "false");
50+
51+
CompileResult result = compileFiles(ActivityInManifest.class);
52+
File generatedFile = toGeneratedFile(ActivityInManifest.class);
53+
54+
assertCompilationSuccessful(result);
55+
56+
assertGeneratedClassMatches(generatedFile, "public class ActivityInManifest_");
57+
}
58+
}

0 commit comments

Comments
 (0)