Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.androidannotations.helper.AndroidManifest;
import org.androidannotations.helper.AndroidManifestFinder;
import org.androidannotations.helper.ErrorHelper;
import org.androidannotations.helper.ModelConstants;
import org.androidannotations.helper.Option;
import org.androidannotations.helper.OptionsHelper;
import org.androidannotations.logger.Level;
Expand Down Expand Up @@ -69,6 +70,8 @@ public class AndroidAnnotationProcessor extends AbstractProcessor {
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);

ModelConstants.init(processingEnv);

// Configure Logger
LoggerContext loggerContext = LoggerContext.getInstance();
loggerContext.setProcessingEnv(processingEnv);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
package org.androidannotations.handler;

import static com.sun.codemodel.JExpr.ref;
import static org.androidannotations.helper.ModelConstants.classSuffix;

import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;

import org.androidannotations.annotations.App;
import org.androidannotations.annotations.EApplication;
import org.androidannotations.helper.ModelConstants;
import org.androidannotations.holder.EApplicationHolder;
import org.androidannotations.holder.EComponentHolder;
import org.androidannotations.model.AnnotationElements;
Expand All @@ -49,7 +49,7 @@ public void validate(Element element, AnnotationElements validatedElements, IsVa
public void process(Element element, EComponentHolder holder) {
String fieldName = element.getSimpleName().toString();
String applicationQualifiedName = element.asType().toString();
JClass applicationClass = refClass(applicationQualifiedName + ModelConstants.GENERATION_SUFFIX);
JClass applicationClass = refClass(applicationQualifiedName + classSuffix());

holder.getInitBody().assign(ref(fieldName), applicationClass.staticInvoke(EApplicationHolder.GET_APPLICATION_INSTANCE));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import static com.sun.codemodel.JExpr._this;
import static com.sun.codemodel.JExpr.cast;
import static com.sun.codemodel.JExpr.ref;
import static org.androidannotations.helper.ModelConstants.GENERATION_SUFFIX;
import static org.androidannotations.helper.ModelConstants.classSuffix;

import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
Expand Down Expand Up @@ -91,7 +91,7 @@ private void rebindContextIfBean(Element element, JBlock initIfNonConfigurationN
elementType = element.asType();
}
String typeQualifiedName = elementType.toString();
JClass fieldGeneratedBeanClass = refClass(typeQualifiedName + GENERATION_SUFFIX);
JClass fieldGeneratedBeanClass = refClass(typeQualifiedName + classSuffix());

initIfNonConfigurationNotNullBlock.invoke(cast(fieldGeneratedBeanClass, field), "rebind").arg(_this());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static com.sun.codemodel.JMod.FINAL;
import static com.sun.codemodel.JMod.PRIVATE;
import static com.sun.codemodel.JMod.PUBLIC;
import static org.androidannotations.helper.ModelConstants.generationSuffix;

import java.util.List;

Expand All @@ -30,7 +31,6 @@

import org.androidannotations.annotations.Receiver;
import org.androidannotations.helper.APTCodeModelHelper;
import org.androidannotations.helper.ModelConstants;
import org.androidannotations.holder.HasReceiverRegistration;
import org.androidannotations.holder.ReceiverRegistrationHolder.IntentFilterData;
import org.androidannotations.model.AnnotationElements;
Expand Down Expand Up @@ -82,7 +82,7 @@ protected void validate(Element element, AnnotationElements validatedElements, I
public void process(Element element, HasReceiverRegistration holder) throws Exception {

String methodName = element.getSimpleName().toString();
String receiverName = methodName + "Receiver" + ModelConstants.GENERATION_SUFFIX;
String receiverName = methodName + "Receiver" + generationSuffix();

Receiver annotation = element.getAnnotation(Receiver.class);
String[] actions = annotation.actions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.sun.codemodel.JExpr._super;
import static com.sun.codemodel.JExpr._this;
import static com.sun.codemodel.JExpr.invoke;
import static org.androidannotations.helper.ModelConstants.generationSuffix;

import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -96,7 +97,7 @@ private void listenerFields(Element element, EActivityHolder holder) {
int i = 1;
for (TypeMirror listenterTypeMirror : listenerTypeMirrors) {
JClass listenerClass = codeModelHelper.typeMirrorToJClass(listenterTypeMirror, holder);
JFieldVar listener = holder.getGeneratedClass().field(JMod.PRIVATE, listenerClass, "listener" + i + "_");
JFieldVar listener = holder.getGeneratedClass().field(JMod.PRIVATE, listenerClass, "listener" + i + generationSuffix());
codeModelHelper.addSuppressWarnings(listener, "unused");
listener.annotate(classes().INJECT);
i++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
*/
package org.androidannotations.handler.rest;

import static org.androidannotations.helper.ModelConstants.classSuffix;

import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.type.TypeMirror;

import org.androidannotations.annotations.rest.Rest;
import org.androidannotations.annotations.rest.RestService;
import org.androidannotations.handler.BaseAnnotationHandler;
import org.androidannotations.helper.ModelConstants;
import org.androidannotations.holder.EComponentHolder;
import org.androidannotations.model.AnnotationElements;
import org.androidannotations.process.IsValid;
Expand Down Expand Up @@ -53,7 +54,7 @@ public void process(Element element, EComponentHolder holder) {
TypeMirror fieldTypeMirror = element.asType();
String interfaceName = fieldTypeMirror.toString();

String generatedClassName = interfaceName + ModelConstants.GENERATION_SUFFIX;
String generatedClassName = interfaceName + classSuffix();

JBlock methodBody = holder.getInitBody();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import static com.sun.codemodel.JExpr._new;
import static com.sun.codemodel.JExpr.lit;
import static org.androidannotations.helper.ModelConstants.GENERATION_SUFFIX;
import static org.androidannotations.helper.ModelConstants.classSuffix;

import java.io.StringWriter;
import java.lang.annotation.Annotation;
Expand Down Expand Up @@ -498,7 +498,7 @@ private ExecutableElement getMethod(List<ExecutableElement> methods, String meth
public JInvocation newBeanOrEBean(GeneratedClassHolder holder, DeclaredType beanType, JVar contextVar) {
if (beanType.asElement().getAnnotation(EBean.class) != null) {
String typeQualifiedName = beanType.toString();
JClass injectedClass = holder.refClass(typeQualifiedName + GENERATION_SUFFIX);
JClass injectedClass = holder.refClass(typeQualifiedName + classSuffix());
return injectedClass.staticInvoke(EBeanHolder.GET_INSTANCE_METHOD_NAME).arg(contextVar);
} else {
return _new(holder.refClass(beanType.toString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static com.sun.codemodel.JMod.PRIVATE;
import static com.sun.codemodel.JMod.PUBLIC;
import static com.sun.codemodel.JMod.STATIC;
import static org.androidannotations.helper.ModelConstants.generationSuffix;

import java.util.List;

Expand Down Expand Up @@ -93,10 +94,10 @@ protected JClass getSuperClass() {

private void createAdditionalConstructor() {
if (hasFragmentInClasspath()) {
fragmentField = addFragmentConstructor(holder.classes().FRAGMENT, "fragment_");
fragmentField = addFragmentConstructor(holder.classes().FRAGMENT, "fragment" + generationSuffix());
}
if (hasFragmentSupportInClasspath()) {
fragmentSupportField = addFragmentConstructor(holder.classes().SUPPORT_V4_FRAGMENT, "fragmentSupport_");
fragmentSupportField = addFragmentConstructor(holder.classes().SUPPORT_V4_FRAGMENT, "fragmentSupport" + generationSuffix());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.androidannotations.helper;

import static org.androidannotations.helper.ModelConstants.classSuffix;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
Expand Down Expand Up @@ -282,8 +284,8 @@ private String manifestNameToValidQualifiedName(String applicationPackage, Node
private boolean classOrModelClassExists(String className) {
Elements elementUtils = processingEnv.getElementUtils();

if (className.endsWith("_")) {
className = className.substring(0, className.length() - 1);
if (className.endsWith(classSuffix())) {
className = className.substring(0, className.length() - classSuffix().length());
}
return elementUtils.getTypeElement(className) != null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
package org.androidannotations.helper;

import static org.androidannotations.helper.ModelConstants.GENERATION_SUFFIX;
import static org.androidannotations.helper.ModelConstants.VALID_ENHANCED_COMPONENT_ANNOTATIONS;
import static org.androidannotations.helper.ModelConstants.classSuffix;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -92,9 +92,9 @@ public String generatedClassQualifiedNameFromQualifiedName(String qualifiedName)
TypeElement type = typeElementFromQualifiedName(qualifiedName);
if (type.getNestingKind() == NestingKind.MEMBER) {
String parentGeneratedClass = generatedClassQualifiedNameFromQualifiedName(type.getEnclosingElement().asType().toString());
return parentGeneratedClass + "." + type.getSimpleName().toString() + GENERATION_SUFFIX;
return parentGeneratedClass + "." + type.getSimpleName().toString() + classSuffix();
} else {
return qualifiedName + GENERATION_SUFFIX;
return qualifiedName + classSuffix();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* License for the specific language governing permissions and limitations under
* the License.
*/

package org.androidannotations.helper;

import static com.sun.codemodel.JExpr._new;
Expand All @@ -24,6 +25,7 @@
import static org.androidannotations.helper.CanonicalNameConstants.PARCELABLE;
import static org.androidannotations.helper.CanonicalNameConstants.SERIALIZABLE;
import static org.androidannotations.helper.CanonicalNameConstants.STRING;
import static org.androidannotations.helper.ModelConstants.generationSuffix;

import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -77,7 +79,7 @@ public void build() throws JClassAlreadyExistsException {
}

private void createClass() throws JClassAlreadyExistsException {
builderClass = holder.getGeneratedClass()._class(PUBLIC | STATIC, "IntentBuilder_");
builderClass = holder.getGeneratedClass()._class(PUBLIC | STATIC, "IntentBuilder" + generationSuffix());
builderClass._extends(getSuperClass());
holder.setIntentBuilderClass(builderClass);
contextField = ref("context");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -49,4 +53,24 @@ public abstract class ModelConstants {
private ModelConstants() {
}

public static void init(ProcessingEnvironment processingEnv) {
OptionsHelper optionsHelper = new OptionsHelper(processingEnv);

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Member

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.isName method to the the validation.

Copy link
Copy Markdown
Member Author

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_6 or SourceVersion.RELEASE_7?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Member

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("."))

Copy link
Copy Markdown
Member Author

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

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.");
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a trim() and also checked the length.

}

public static String classSuffix() {
return classSuffix;
}

public static String generationSuffix() {
return generationSuffix;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ enum Option {
LOG_FILE("logFile", null), //
LOG_LEVEL("logLevel", "DEBUG"), //
LOG_APPENDER_CONSOLE("logAppenderConsole", "false"), //
LOG_APPENDER_FILE("logAppenderFile", "true");
LOG_APPENDER_FILE("logAppenderFile", "true"), //
CLASS_SUFFIX("classSuffix", "_");

private String key;
private String defaultValue;
Expand Down Expand Up @@ -82,6 +83,10 @@ public String getResourcePackageName() {
return getString(Option.RESOURCE_PACKAGE_NAME);
}

public String getClassSuffix() {
return getString(Option.CLASS_SUFFIX);
}

public String getLogFile() {
return getString(Option.LOG_FILE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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 generationSuffix() will be a unique name in the whole codebase anyway.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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;
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static com.sun.codemodel.JExpr._this;
import static com.sun.codemodel.JMod.FINAL;
import static com.sun.codemodel.JMod.PRIVATE;
import static org.androidannotations.helper.ModelConstants.generationSuffix;

import org.androidannotations.api.view.HasViews;
import org.androidannotations.api.view.OnViewChangedNotifier;
Expand Down Expand Up @@ -47,7 +48,7 @@ public void invokeViewChanged(JBlock block) {
public JVar replacePreviousNotifier(JBlock block) {
JClass notifierClass = holder.refClass(OnViewChangedNotifier.class);
if (notifier == null) {
notifier = holder.getGeneratedClass().field(PRIVATE | FINAL, notifierClass, "onViewChangedNotifier_", _new(notifierClass));
notifier = holder.getGeneratedClass().field(PRIVATE | FINAL, notifierClass, "onViewChangedNotifier" + generationSuffix(), _new(notifierClass));
holder.getGeneratedClass()._implements(HasViews.class);
}
return block.decl(notifierClass, "previousNotifier", notifierClass.staticInvoke("replaceNotifier").arg(notifier));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import static com.sun.codemodel.JMod.FINAL;
import static com.sun.codemodel.JMod.PUBLIC;
import static com.sun.codemodel.JMod.STATIC;
import static org.androidannotations.helper.ModelConstants.GENERATION_SUFFIX;
import static org.androidannotations.helper.ModelConstants.classSuffix;

import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
Expand Down Expand Up @@ -55,10 +55,10 @@ protected void setGeneratedClass() throws Exception {
if (annotatedElement.getNestingKind().isNested()) {
Element enclosingElement = annotatedElement.getEnclosingElement();
GeneratedClassHolder enclosingHolder = processHolder.getGeneratedClassHolder(enclosingElement);
String generatedBeanSimpleName = annotatedElement.getSimpleName().toString() + GENERATION_SUFFIX;
String generatedBeanSimpleName = annotatedElement.getSimpleName().toString() + classSuffix();
generatedClass = enclosingHolder.getGeneratedClass()._class(PUBLIC | FINAL | STATIC, generatedBeanSimpleName, ClassType.CLASS);
} else {
String generatedClassQualifiedName = annotatedComponentQualifiedName + GENERATION_SUFFIX;
String generatedClassQualifiedName = annotatedComponentQualifiedName + classSuffix();
generatedClass = codeModel()._class(PUBLIC | FINAL, generatedClassQualifiedName, ClassType.CLASS);
}
for (TypeParameterElement typeParam : annotatedElement.getTypeParameters()) {
Expand All @@ -68,7 +68,7 @@ protected void setGeneratedClass() throws Exception {
setExtends();
codeModelHelper.addNonAAAnotations(generatedClass, annotatedElement.getAnnotationMirrors(), this);
}

public JClass getAnnotatedClass() {
return annotatedClass;
}
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.