Permalink
Browse files

Moving projects, eclipse did bad shit with project sharing

  • Loading branch information...
1 parent 9b80143 commit df8981f2e224bb0e85f0c299c03945ceaf9e2ec7 @pyricau pyricau committed Dec 22, 2010
Showing with 702 additions and 0 deletions.
  1. +12 −0 AndroidAnnotationProcessingExample/.classpath
  2. +4 −0 AndroidAnnotationProcessingExample/.factorypath
  3. +33 −0 AndroidAnnotationProcessingExample/.project
  4. +5 −0 AndroidAnnotationProcessingExample/.settings/org.eclipse.jdt.apt.core.prefs
  5. +13 −0 AndroidAnnotationProcessingExample/.settings/org.eclipse.jdt.core.prefs
  6. +18 −0 AndroidAnnotationProcessingExample/AndroidManifest.xml
  7. +11 −0 AndroidAnnotationProcessingExample/default.properties
  8. BIN AndroidAnnotationProcessingExample/res/drawable/icon.png
  9. +21 −0 AndroidAnnotationProcessingExample/res/layout/main.xml
  10. +5 −0 AndroidAnnotationProcessingExample/res/values/strings.xml
  11. +26 −0 ...notationProcessingExample/src/com/googlecode/androidannotations/example/AnnotationProcessing.java
  12. +10 −0 AndroidAnnotationProcessingExample/src/com/googlecode/androidannotations/example/Test.java
  13. +6 −0 AndroidAnnotations/.classpath
  14. +17 −0 AndroidAnnotations/.project
  15. +12 −0 AndroidAnnotations/.settings/org.eclipse.jdt.core.prefs
  16. BIN AndroidAnnotations/AndroidAnnotations.jar
  17. +1 −0 AndroidAnnotations/META-INF/services/javax.annotation.processing.Processor
  18. +371 −0 AndroidAnnotations/src/com/googlecode/androidannotations/AndroidAnnotationProcessor.java
  19. +7 −0 AndroidAnnotations/src/com/googlecode/androidannotations/CompilationFailedException.java
  20. +76 −0 AndroidAnnotations/src/com/googlecode/androidannotations/GeneratedActivity.java
  21. +30 −0 AndroidAnnotations/src/com/googlecode/androidannotations/GeneratedField.java
  22. +12 −0 AndroidAnnotations/src/com/googlecode/androidannotations/Layout.java
  23. +12 −0 AndroidAnnotations/src/com/googlecode/androidannotations/UiField.java
View
12 AndroidAnnotationProcessingExample/.classpath
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
+ <classpathentry kind="src" path="gen">
+ <attributes>
+ <attribute name="optional" value="true"/>
+ </attributes>
+ </classpathentry>
+ <classpathentry kind="lib" path="/AndroidAnnotations/AndroidAnnotations.jar"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
View
4 AndroidAnnotationProcessingExample/.factorypath
@@ -0,0 +1,4 @@
+<factorypath>
+ <factorypathentry kind="PLUGIN" id="org.eclipse.jst.ws.annotations.core" enabled="true" runInBatchMode="false"/>
+ <factorypathentry kind="WKSPJAR" id="/AndroidAnnotations/AndroidAnnotations.jar" enabled="true" runInBatchMode="false"/>
+</factorypath>
View
33 AndroidAnnotationProcessingExample/.project
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>AndroidAnnotationProcessingExample</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>com.android.ide.eclipse.adt.ApkBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>com.android.ide.eclipse.adt.AndroidNature</nature>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
View
5 AndroidAnnotationProcessingExample/.settings/org.eclipse.jdt.apt.core.prefs
@@ -0,0 +1,5 @@
+#Wed Dec 22 17:25:15 CET 2010
+eclipse.preferences.version=1
+org.eclipse.jdt.apt.aptEnabled=true
+org.eclipse.jdt.apt.genSrcDir=gen
+org.eclipse.jdt.apt.reconcileEnabled=true
View
13 AndroidAnnotationProcessingExample/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,13 @@
+#Wed Dec 22 10:18:54 CET 2010
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=1.6
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.processAnnotations=enabled
+org.eclipse.jdt.core.compiler.source=1.6
View
18 AndroidAnnotationProcessingExample/AndroidManifest.xml
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="com.googlecode.androidannotations.example"
+ android:versionCode="1"
+ android:versionName="1.0">
+ <application android:icon="@drawable/icon" android:label="@string/app_name">
+ <activity android:name=".gen.AnnotationProcessing"
+ android:label="@string/app_name">
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+ <category android:name="android.intent.category.LAUNCHER" />
+ </intent-filter>
+ </activity>
+
+ </application>
+ <uses-sdk android:minSdkVersion="3" />
+
+</manifest>
View
11 AndroidAnnotationProcessingExample/default.properties
@@ -0,0 +1,11 @@
+# This file is automatically generated by Android Tools.
+# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
+#
+# This file must be checked in Version Control Systems.
+#
+# To customize properties used by the Ant build system use,
+# "build.properties", and override values to adapt the script to your
+# project structure.
+
+# Project target.
+target=android-3
View
BIN AndroidAnnotationProcessingExample/res/drawable/icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
21 AndroidAnnotationProcessingExample/res/layout/main.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:orientation="vertical"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent"
+ >
+ <TextView
+ android:id="@+id/myTv"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:text="@string/hello"
+ />
+
+ <EditText
+ android:id="@+id/edit"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:text="@string/hello"
+ />
+
+</LinearLayout>
View
5 AndroidAnnotationProcessingExample/res/values/strings.xml
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+ <string name="hello">Hello World, AnnotationProcessing!</string>
+ <string name="app_name">AnnotationProcessing</string>
+</resources>
View
26 ...ProcessingExample/src/com/googlecode/androidannotations/example/AnnotationProcessing.java
@@ -0,0 +1,26 @@
+package com.googlecode.androidannotations.example;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.widget.EditText;
+import android.widget.TextView;
+
+import com.googlecode.androidannotations.Layout;
+import com.googlecode.androidannotations.UiField;
+
+@Layout(R.layout.main)
+public class AnnotationProcessing extends Activity {
+
+ @UiField
+ EditText edit;
+
+ @UiField(R.id.myTv)
+ TextView text;
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ text.setText("Yahooo !");
+ }
+}
View
10 AndroidAnnotationProcessingExample/src/com/googlecode/androidannotations/example/Test.java
@@ -0,0 +1,10 @@
+package com.googlecode.androidannotations.example;
+
+import android.app.Activity;
+
+import com.googlecode.androidannotations.Layout;
+
+@Layout(R.layout.main)
+public class Test extends Activity{
+
+}
View
6 AndroidAnnotations/.classpath
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
View
17 AndroidAnnotations/.project
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>AndroidAnnotations</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
View
12 AndroidAnnotations/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,12 @@
+#Wed Dec 22 10:21:42 CET 2010
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=1.6
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.6
View
BIN AndroidAnnotations/AndroidAnnotations.jar
Binary file not shown.
View
1 AndroidAnnotations/META-INF/services/javax.annotation.processing.Processor
@@ -0,0 +1 @@
+com.googlecode.androidannotations.AndroidAnnotationProcessor
View
371 AndroidAnnotations/src/com/googlecode/androidannotations/AndroidAnnotationProcessor.java
@@ -0,0 +1,371 @@
+package com.googlecode.androidannotations;
+
+import java.lang.annotation.Annotation;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import javax.annotation.processing.AbstractProcessor;
+import javax.annotation.processing.Filer;
+import javax.annotation.processing.RoundEnvironment;
+import javax.annotation.processing.SupportedAnnotationTypes;
+import javax.annotation.processing.SupportedSourceVersion;
+import javax.lang.model.SourceVersion;
+import javax.lang.model.element.AnnotationMirror;
+import javax.lang.model.element.Element;
+import javax.lang.model.element.PackageElement;
+import javax.lang.model.element.TypeElement;
+import javax.lang.model.element.VariableElement;
+import javax.lang.model.type.DeclaredType;
+import javax.lang.model.type.TypeKind;
+import javax.lang.model.type.TypeMirror;
+import javax.lang.model.util.ElementFilter;
+import javax.lang.model.util.Elements;
+import javax.tools.Diagnostic;
+
+@SupportedAnnotationTypes({ "com.googlecode.androidannotations.Layout", "com.googlecode.androidannotations.UiField" })
+@SupportedSourceVersion(SourceVersion.RELEASE_6)
+public class AndroidAnnotationProcessor extends AbstractProcessor {
+
+ private static final String LAYOUT_INNER_CLASS_NAME = "layout";
+
+ private static final String ID_INNER_CLASS_NAME = "id";
+
+ List<TypeElement> layoutAnnotatedElements = new ArrayList<TypeElement>();
+
+ List<VariableElement> uiFieldAnnotatedElements = new ArrayList<VariableElement>();
+
+ Map<Element, GeneratedActivity> activitiesByElement = new HashMap<Element, GeneratedActivity>();
+
+ Map<Integer, String> layoutFieldQualifiedNamesByIds = new HashMap<Integer, String>();
+
+ Map<Integer, String> idFieldQualifiedNamesByIds = new HashMap<Integer, String>();
+
+ @Override
+ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
+
+ try {
+ clear();
+
+ parseElements(annotations, roundEnv);
+
+ if (foundLayoutAnnotatedElements()) {
+
+ TypeElement rClassElement = findRClassElement();
+
+ List<TypeElement> rInnerTypes = extractRInnerTypes(rClassElement);
+
+ extractLayoutFieldIds(rInnerTypes);
+
+ extractIdFieldIds(rInnerTypes);
+
+ for (TypeElement layoutAnnotatedElement : layoutAnnotatedElements) {
+ Layout layoutAnnotation = layoutAnnotatedElement.getAnnotation(Layout.class);
+ int layoutId = layoutAnnotation.value();
+
+ if (!layoutFieldQualifiedNamesByIds.containsKey(layoutId)) {
+ AnnotationMirror annotationMirror = findAnnotationMirror(layoutAnnotatedElement, Layout.class);
+ processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Layout id value not found in R.layout.*: " + layoutId,
+ layoutAnnotatedElement, annotationMirror);
+ } else {
+
+ String layoutFieldQualifiedName = layoutFieldQualifiedNamesByIds.get(layoutId);
+
+ String superClassQualifiedName = layoutAnnotatedElement.getQualifiedName().toString();
+
+ int packageSeparatorIndex = superClassQualifiedName.lastIndexOf('.');
+
+ String packageName = superClassQualifiedName.substring(0, packageSeparatorIndex);
+
+ String classSimpleName = superClassQualifiedName.substring(packageSeparatorIndex + 1)+"_";
+
+ GeneratedActivity activity = new GeneratedActivity();
+
+ activity.setLayoutQualifiedName(layoutFieldQualifiedName);
+ activity.setClassSimpleName(classSimpleName);
+ activity.setPackageName(packageName);
+
+ activity.setSuperClassQualifiedName(superClassQualifiedName);
+
+ activitiesByElement.put(layoutAnnotatedElement, activity);
+
+ }
+ }
+
+ for (VariableElement uiFieldAnnotatedElement : uiFieldAnnotatedElements) {
+
+ Element enclosingElement = uiFieldAnnotatedElement.getEnclosingElement();
+
+ GeneratedActivity generatedActivity = activitiesByElement.get(enclosingElement);
+
+ if (generatedActivity != null) {
+ List<GeneratedField> generatedFields = generatedActivity.getGeneratedFields();
+
+ String name = uiFieldAnnotatedElement.getSimpleName().toString();
+
+ TypeMirror uiFieldTypeMirror = uiFieldAnnotatedElement.asType();
+
+ if (uiFieldTypeMirror instanceof DeclaredType) {
+
+ DeclaredType uiFieldDeclaredType = (DeclaredType) uiFieldTypeMirror;
+ String typeQualifiedName = uiFieldDeclaredType.toString();
+
+ UiField uiFieldAnnotation = uiFieldAnnotatedElement.getAnnotation(UiField.class);
+
+ int id = uiFieldAnnotation.value();
+
+ String viewQualifiedId = null;
+ if (id == -1) {
+ String fieldName = uiFieldAnnotatedElement.getSimpleName().toString();
+
+ TypeElement idType = extractIdInnerType(rInnerTypes);
+
+ String idQualifiedName = idType.getQualifiedName().toString();
+
+ viewQualifiedId = idQualifiedName + "." + fieldName;
+
+ if (!idFieldQualifiedNamesByIds.containsValue(viewQualifiedId)) {
+ AnnotationMirror annotationMirror = findAnnotationMirror(uiFieldAnnotatedElement, UiField.class);
+ processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
+ UiField.class + " field name not found in R.id.* " + Layout.class+ " "+viewQualifiedId, uiFieldAnnotatedElement, annotationMirror);
+ viewQualifiedId = null;
+ }
+
+ } else {
+ if (!idFieldQualifiedNamesByIds.containsKey(id)) {
+ AnnotationMirror annotationMirror = findAnnotationMirror(uiFieldAnnotatedElement, UiField.class);
+ processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
+ UiField.class + " value not found in R.id.* " + Layout.class, uiFieldAnnotatedElement, annotationMirror);
+ } else {
+ viewQualifiedId = idFieldQualifiedNamesByIds.get(id);
+ }
+ }
+
+ if (viewQualifiedId != null) {
+ GeneratedField generatedField = new GeneratedField();
+
+ generatedField.setName(name);
+ generatedField.setTypeQualifiedName(typeQualifiedName);
+ generatedField.setViewQualifiedId(viewQualifiedId);
+
+ generatedFields.add(generatedField);
+ }
+ } else {
+ AnnotationMirror annotationMirror = findAnnotationMirror(uiFieldAnnotatedElement, UiField.class);
+ processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
+ UiField.class + " should only be used on a field which is a declared type " + Layout.class, uiFieldAnnotatedElement,
+ annotationMirror);
+ }
+
+ }
+
+ }
+
+ Filer filer = processingEnv.getFiler();
+
+ for (GeneratedActivity activity : activitiesByElement.values()) {
+ activity.generateSource(filer);
+ }
+ }
+
+ clear();
+ } catch (CompilationFailedException e) {
+ return false;
+ } catch (Exception e) {
+ Element element = roundEnv.getElementsAnnotatedWith(annotations.iterator().next()).iterator().next();
+ processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Annotation processing exception: " + e.toString(), element);
+ }
+
+ return false;
+ }
+
+ private TypeElement findRClassElement() {
+
+ Elements elementUtils = processingEnv.getElementUtils();
+
+ TypeElement firstActivity = layoutAnnotatedElements.get(0);
+
+ PackageElement firstActivityPackage = elementUtils.getPackageOf(firstActivity);
+
+ TypeElement rType = elementUtils.getTypeElement(firstActivityPackage.getQualifiedName() + "." + "R");
+
+ // TODO better handling at finding R class
+ if (rType == null) {
+ processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
+ "All Activities should belong to the same package as the R class, which is not the case for: " + firstActivity, firstActivity);
+ }
+
+ return rType;
+ }
+
+ private AnnotationMirror findAnnotationMirror(Element annotatedElement, Class<? extends Annotation> annotationClass) {
+ List<? extends AnnotationMirror> annotationMirrors = annotatedElement.getAnnotationMirrors();
+
+ for (AnnotationMirror annotationMirror : annotationMirrors) {
+ TypeElement annotationElement = (TypeElement) annotationMirror.getAnnotationType().asElement();
+ if (isAnnotation(annotationElement, annotationClass)) {
+ return annotationMirror;
+ }
+ }
+ throw new CompilationFailedException();
+ }
+
+ private void extractLayoutFieldIds(List<TypeElement> rInnerTypes) {
+
+ TypeElement layoutType = extractLayoutInnerType(rInnerTypes);
+
+ if (layoutType == null) {
+ return;
+ }
+
+ String layoutQualifiedName = layoutType.getQualifiedName().toString();
+
+ List<? extends Element> layoutEnclosedElements = layoutType.getEnclosedElements();
+
+ List<VariableElement> layoutFields = ElementFilter.fieldsIn(layoutEnclosedElements);
+
+ for (VariableElement layoutField : layoutFields) {
+ TypeKind fieldType = layoutField.asType().getKind();
+ if (fieldType.isPrimitive() && fieldType.equals(TypeKind.INT)) {
+ Integer layoutFieldId = (Integer) layoutField.getConstantValue();
+ layoutFieldQualifiedNamesByIds.put(layoutFieldId, layoutQualifiedName + "." + layoutField.getSimpleName());
+ }
+ }
+ }
+
+ private void extractIdFieldIds(List<TypeElement> rInnerTypes) {
+
+ TypeElement idType = extractIdInnerType(rInnerTypes);
+
+ if (idType == null) {
+ return;
+ }
+
+ String idQualifiedName = idType.getQualifiedName().toString();
+
+ List<? extends Element> idEnclosedElements = idType.getEnclosedElements();
+
+ List<VariableElement> idFields = ElementFilter.fieldsIn(idEnclosedElements);
+
+ for (VariableElement idField : idFields) {
+ TypeKind fieldType = idField.asType().getKind();
+ if (fieldType.isPrimitive() && fieldType.equals(TypeKind.INT)) {
+ Integer idFieldId = (Integer) idField.getConstantValue();
+ idFieldQualifiedNamesByIds.put(idFieldId, idQualifiedName + "." + idField.getSimpleName());
+ }
+ }
+ }
+
+ private TypeElement extractIdInnerType(List<TypeElement> rInnerTypes) {
+
+ for (TypeElement rInnerType : rInnerTypes) {
+ if (rInnerType.getSimpleName().toString().equals(ID_INNER_CLASS_NAME)) {
+ return rInnerType;
+ }
+ }
+
+ return null;
+ }
+
+ private TypeElement extractLayoutInnerType(List<TypeElement> rInnerTypes) {
+
+ for (TypeElement rInnerType : rInnerTypes) {
+ if (rInnerType.getSimpleName().toString().equals(LAYOUT_INNER_CLASS_NAME)) {
+ return rInnerType;
+ }
+ }
+
+ return null;
+ }
+
+ private List<TypeElement> extractRInnerTypes(TypeElement rElement) {
+
+ List<? extends Element> rEnclosedElements = rElement.getEnclosedElements();
+
+ List<TypeElement> rInnerTypes = ElementFilter.typesIn(rEnclosedElements);
+ return rInnerTypes;
+ }
+
+ private boolean foundLayoutAnnotatedElements() {
+ return layoutAnnotatedElements.size() != 0;
+ }
+
+ // private TypeElement extractRClassElement(Element rLocationElement) {
+ // RClass rLocationAnnotation =
+ // rLocationElement.getAnnotation(RClass.class);
+ // try {
+ // rLocationAnnotation.value();
+ // } catch (MirroredTypeException mte) {
+ // DeclaredType typeMirror = (DeclaredType) mte.getTypeMirror();
+ // return (TypeElement) typeMirror.asElement();
+ // }
+ // processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
+ // "Annotation processing error : could not extract MirrorType from class value",
+ // rLocationElement);
+ // throw new IllegalArgumentException();
+ // }
+
+ private void clear() {
+ layoutAnnotatedElements.clear();
+ layoutFieldQualifiedNamesByIds.clear();
+ activitiesByElement.clear();
+ uiFieldAnnotatedElements.clear();
+ }
+
+ private void parseElements(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
+
+ TypeMirror activityTypeMirror = processingEnv.getElementUtils().getTypeElement("android.app.Activity").asType();
+
+ for (TypeElement annotation : annotations) {
+ for (Element element : roundEnv.getElementsAnnotatedWith(annotation)) {
+
+ if (isAnnotation(annotation, Layout.class)) {
+ TypeElement typeElement = (TypeElement) element;
+
+ TypeMirror layoutTypeMirror = typeElement.asType();
+
+ if (processingEnv.getTypeUtils().isSubtype(layoutTypeMirror, activityTypeMirror)) {
+ layoutAnnotatedElements.add(typeElement);
+ } else {
+ AnnotationMirror annotationMirror = findAnnotationMirror(typeElement, Layout.class);
+ processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, Layout.class + " should only be used on Activity subclasses",
+ typeElement, annotationMirror);
+ }
+ } else if (isAnnotation(annotation, UiField.class)) {
+ VariableElement variableElement = (VariableElement) element;
+
+ Element enclosingElement = variableElement.getEnclosingElement();
+
+ if (enclosingElement instanceof TypeElement) {
+ TypeElement enclosingTypeElement = (TypeElement) enclosingElement;
+
+ Layout layoutAnnotation = enclosingTypeElement.getAnnotation(Layout.class);
+
+ if (layoutAnnotation != null) {
+ uiFieldAnnotatedElements.add(variableElement);
+ } else {
+ AnnotationMirror annotationMirror = findAnnotationMirror(variableElement, UiField.class);
+ processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
+ UiField.class + " should only be used on a field from a class annotated with " + Layout.class, variableElement,
+ annotationMirror);
+
+ }
+
+ } else {
+ AnnotationMirror annotationMirror = findAnnotationMirror(variableElement, UiField.class);
+ processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, UiField.class + " should only be used on a field from a class",
+ variableElement, annotationMirror);
+
+ }
+ }
+ }
+ }
+ }
+
+ private boolean isAnnotation(TypeElement annotation, Class<? extends Annotation> annotationClass) {
+ return annotation.getQualifiedName().toString().equals(annotationClass.getName());
+ }
+
+}
View
7 AndroidAnnotations/src/com/googlecode/androidannotations/CompilationFailedException.java
@@ -0,0 +1,7 @@
+package com.googlecode.androidannotations;
+
+public class CompilationFailedException extends RuntimeException {
+
+ private static final long serialVersionUID = 1L;
+
+}
View
76 AndroidAnnotations/src/com/googlecode/androidannotations/GeneratedActivity.java
@@ -0,0 +1,76 @@
+package com.googlecode.androidannotations;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.annotation.processing.Filer;
+import javax.tools.FileObject;
+
+public class GeneratedActivity {
+
+ private static final String CLASS_FORMAT = //
+ "package %s;\n" + //
+ "\n" + //
+ "public class %s extends %s {\n" + //
+ " @Override\n" + //
+ " public void onCreate(android.os.Bundle savedInstanceState) {\n" + //
+ " setContentView(%s);\n" + //
+ "\n" + //
+ "%s" + //
+ "\n" + //
+ " super.onCreate(savedInstanceState);\n" + //
+ " }\n" + //
+ "}\n";
+
+
+
+ private String packageName;
+
+ private String classSimpleName;
+
+ private String superClassQualifiedName;
+
+ private String layoutQualifiedName;
+
+ private final List<GeneratedField> generatedFields = new ArrayList<GeneratedField>();
+
+ public void generateSource(Filer filer) throws IOException {
+
+ FileObject classFile = filer.createSourceFile(packageName+"."+classSimpleName);
+
+ StringBuilder generatedFieldsSb = new StringBuilder();
+
+ for(GeneratedField generatedField : generatedFields) {
+ generatedFieldsSb.append(generatedField.writeField());
+ }
+
+ String generatedClass = String.format(CLASS_FORMAT, packageName, classSimpleName, superClassQualifiedName, layoutQualifiedName, generatedFieldsSb.toString());
+
+ Writer writer = classFile.openWriter();
+ writer.append(generatedClass);
+ writer.close();
+ }
+
+ public void setPackageName(String packageName) {
+ this.packageName = packageName;
+ }
+
+ public void setSuperClassQualifiedName(String superClassQualifiedName) {
+ this.superClassQualifiedName = superClassQualifiedName;
+ }
+
+ public void setLayoutQualifiedName(String layoutQualifiedName) {
+ this.layoutQualifiedName = layoutQualifiedName;
+ }
+
+ public List<GeneratedField> getGeneratedFields() {
+ return generatedFields;
+ }
+
+ public void setClassSimpleName(String classSimpleName) {
+ this.classSimpleName = classSimpleName;
+ }
+
+}
View
30 AndroidAnnotations/src/com/googlecode/androidannotations/GeneratedField.java
@@ -0,0 +1,30 @@
+package com.googlecode.androidannotations;
+
+
+public class GeneratedField {
+
+ private static final String FIELD_FORMAT = " %s = (%s) findViewById(%s);\n";
+
+ private String name;
+
+ private String typeQualifiedName;
+
+ private String viewQualifiedId;
+
+ public String writeField() {
+ return String.format(FIELD_FORMAT, name, typeQualifiedName, viewQualifiedId);
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public void setTypeQualifiedName(String typeQualifiedName) {
+ this.typeQualifiedName = typeQualifiedName;
+ }
+
+ public void setViewQualifiedId(String viewQualifiedId) {
+ this.viewQualifiedId = viewQualifiedId;
+ }
+
+}
View
12 AndroidAnnotations/src/com/googlecode/androidannotations/Layout.java
@@ -0,0 +1,12 @@
+package com.googlecode.androidannotations;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.TYPE)
+public @interface Layout {
+ int value();
+}
View
12 AndroidAnnotations/src/com/googlecode/androidannotations/UiField.java
@@ -0,0 +1,12 @@
+package com.googlecode.androidannotations;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.FIELD)
+public @interface UiField {
+ int value() default -1;
+}

0 comments on commit df8981f

Please sign in to comment.