-
Notifications
You must be signed in to change notification settings - Fork 2.3k
add option to change GENERATION_SUFFIX #1280
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,9 @@ | |
| import java.lang.annotation.Annotation; | ||
| import java.util.List; | ||
|
|
||
| import javax.annotation.processing.ProcessingEnvironment; | ||
| import javax.lang.model.SourceVersion; | ||
|
|
||
| import org.androidannotations.annotations.EActivity; | ||
| import org.androidannotations.annotations.EApplication; | ||
| import org.androidannotations.annotations.EBean; | ||
|
|
@@ -35,7 +38,8 @@ | |
|
|
||
| public abstract class ModelConstants { | ||
|
|
||
| public static final String GENERATION_SUFFIX = "_"; | ||
| private static String generationSuffix = "_"; | ||
| private static String classSuffix; | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| public static final List<Class<? extends Annotation>> VALID_ENHANCED_VIEW_SUPPORT_ANNOTATIONS = asList(EActivity.class, EViewGroup.class, EView.class, EBean.class, EFragment.class); | ||
|
|
@@ -49,4 +53,24 @@ public abstract class ModelConstants { | |
| private ModelConstants() { | ||
| } | ||
|
|
||
| public static void init(ProcessingEnvironment processingEnv) { | ||
| OptionsHelper optionsHelper = new OptionsHelper(processingEnv); | ||
| classSuffix = optionsHelper.getClassSuffix().trim(); | ||
|
|
||
| if (classSuffix.isEmpty()) { | ||
| throw new IllegalArgumentException("'" + classSuffix + "' may not be an emtpy string."); | ||
| } | ||
|
|
||
| if (!SourceVersion.isName(classSuffix) || classSuffix.contains(".")) { | ||
| throw new IllegalArgumentException("'" + classSuffix + "' is not a valid Java identifier."); | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should also have to be sure the string is not empty (zero-length), because it will result in duplicate class names.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added a |
||
| } | ||
|
|
||
| public static String classSuffix() { | ||
| return classSuffix; | ||
| } | ||
|
|
||
| public static String generationSuffix() { | ||
| return generationSuffix; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,10 +26,10 @@ | |
| import static org.androidannotations.helper.CanonicalNameConstants.HTTP_MESSAGE_CONVERTER; | ||
| import static org.androidannotations.helper.CanonicalNameConstants.INTERNET_PERMISSION; | ||
| import static org.androidannotations.helper.CanonicalNameConstants.WAKELOCK_PERMISSION; | ||
| import static org.androidannotations.helper.ModelConstants.GENERATION_SUFFIX; | ||
| import static org.androidannotations.helper.ModelConstants.VALID_ANDROID_ANNOTATIONS; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should re-add the static imports (you can static import methods, too), because they are improving the readability of the code, and
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the static import is there. but some lines below. |
||
| import static org.androidannotations.helper.ModelConstants.VALID_ENHANCED_COMPONENT_ANNOTATIONS; | ||
| import static org.androidannotations.helper.ModelConstants.VALID_ENHANCED_VIEW_SUPPORT_ANNOTATIONS; | ||
| import static org.androidannotations.helper.ModelConstants.classSuffix; | ||
|
|
||
| import java.lang.annotation.Annotation; | ||
| import java.util.ArrayList; | ||
|
|
@@ -619,7 +619,7 @@ public void applicationRegistered(Element element, AndroidManifest manifest, IsV | |
| TypeElement typeElement = (TypeElement) element; | ||
|
|
||
| String componentQualifiedName = typeElement.getQualifiedName().toString(); | ||
| String generatedComponentQualifiedName = componentQualifiedName + ModelConstants.GENERATION_SUFFIX; | ||
| String generatedComponentQualifiedName = componentQualifiedName + classSuffix(); | ||
|
|
||
| if (!typeElement.getModifiers().contains(Modifier.ABSTRACT) && !applicationClassName.equals(generatedComponentQualifiedName)) { | ||
| if (applicationClassName.equals(componentQualifiedName)) { | ||
|
|
@@ -647,9 +647,9 @@ public void isSharedPreference(Element element, AnnotationElements validatedElem | |
| String elementTypeName = type.toString(); | ||
|
|
||
| boolean sharedPrefValidatedInRound = false; | ||
| if (elementTypeName.endsWith(GENERATION_SUFFIX)) { | ||
| String prefTypeName = elementTypeName.substring(0, elementTypeName.length() - GENERATION_SUFFIX.length()); | ||
| prefTypeName = prefTypeName.replace("_.", "."); | ||
| if (elementTypeName.endsWith(classSuffix())) { | ||
| String prefTypeName = elementTypeName.substring(0, elementTypeName.length() - classSuffix().length()); | ||
| prefTypeName = prefTypeName.replace(classSuffix() + ".", "."); | ||
|
|
||
| Set<? extends Element> sharedPrefElements = validatedElements.getRootAnnotatedElements(SharedPref.class.getName()); | ||
|
|
||
|
|
@@ -1112,12 +1112,12 @@ public void componentRegistered(Element element, AndroidManifest androidManifest | |
| } | ||
|
|
||
| String componentQualifiedName = typeElement.getQualifiedName().toString(); | ||
| String generatedComponentQualifiedName = componentQualifiedName + ModelConstants.GENERATION_SUFFIX; | ||
| String generatedComponentQualifiedName = componentQualifiedName + classSuffix(); | ||
|
|
||
| List<String> componentQualifiedNames = androidManifest.getComponentQualifiedNames(); | ||
| if (!componentQualifiedNames.contains(generatedComponentQualifiedName)) { | ||
| String simpleName = typeElement.getSimpleName().toString(); | ||
| String generatedSimpleName = simpleName + ModelConstants.GENERATION_SUFFIX; | ||
| String generatedSimpleName = simpleName + classSuffix(); | ||
| if (componentQualifiedNames.contains(componentQualifiedName)) { | ||
| valid.invalidate(); | ||
| annotationHelper.printAnnotationError(element, "The AndroidManifest.xml file contains the original component, and not the AndroidAnnotations generated component. Please register " + generatedSimpleName + " instead of " + simpleName); | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have to validate the suffix here, because the user can pass a string which is not valid in Java identifiers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dodgex you can use
SourceVersion.isNamemethod to the the validation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and what SourceVersion value to use?
SourceVersion.RELEASE_6orSourceVersion.RELEASE_7?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/excilys/androidannotations/blob/326fe9666df37284245c4e2706f4c042f0b396c1/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/AndroidAnnotationProcessor.java#L285
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW,
isName()is static, isn't it? The enum value does not matter.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So
if (!SourceVersion.isName(classSuffix) || classSuffix.contains("."))There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah. missed that static... well i'll update the PR when i get the time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated