Skip to content

Commit c9611d5

Browse files
committed
Initial Commit
0 parents  commit c9611d5

File tree

73 files changed

+1667
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+1667
-0
lines changed

.gitignore

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#built application files
2+
*.apk
3+
*.ap_
4+
5+
# files for the dex VM
6+
*.dex
7+
8+
# Java class files
9+
*.class
10+
11+
# generated files
12+
bin/
13+
gen/
14+
15+
# Local configuration file (sdk path, etc)
16+
local.properties
17+
18+
# Windows thumbnail db
19+
Thumbs.db
20+
21+
# OSX files
22+
.DS_Store
23+
24+
# Eclipse project files
25+
.classpath
26+
.project
27+
28+
# Android Studio
29+
*.iml
30+
.idea
31+
#.idea/workspace.xml - remove # and delete .idea if it better suit your needs.
32+
.gradle
33+
build/
34+
35+
#NDK
36+
obj/

annotationprocessor/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

annotationprocessor/build.gradle

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apply plugin: 'java'
2+
3+
dependencies {
4+
compile fileTree(dir: 'libs', include: ['*.jar'])
5+
// compile 'com.google.auto.service:auto-service:1.0-rc2' // this includes guava (huge) and could be uses it to auto-generate META-INF.service.Processor
6+
compile 'com.squareup:javapoet:1.7.0'
7+
compile 'io.realm:realm-annotations:1.1.1'
8+
compile project(':annotations')
9+
}
10+
11+
sourceCompatibility = 1.7
12+
targetCompatibility = 1.7
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.quarkworks.android.realmtypesafequery;
2+
3+
import com.quarkworks.android.realmtypesafequery.annotations.GenerateRealmStringFields;
4+
import com.quarkworks.android.realmtypesafequery.annotations.GenerateRealmFields;
5+
import com.squareup.javapoet.CodeBlock;
6+
import com.squareup.javapoet.FieldSpec;
7+
import com.squareup.javapoet.JavaFile;
8+
import com.squareup.javapoet.TypeSpec;
9+
10+
import java.io.IOException;
11+
import java.lang.reflect.Type;
12+
import java.util.LinkedHashSet;
13+
import java.util.LinkedList;
14+
import java.util.List;
15+
import java.util.Set;
16+
17+
import javax.annotation.processing.AbstractProcessor;
18+
import javax.annotation.processing.RoundEnvironment;
19+
import javax.lang.model.element.Element;
20+
import javax.lang.model.element.Modifier;
21+
import javax.lang.model.element.TypeElement;
22+
import javax.lang.model.element.VariableElement;
23+
import javax.lang.model.util.ElementFilter;
24+
import javax.tools.Diagnostic;
25+
26+
import io.realm.annotations.Ignore;
27+
28+
//@AutoService(Processor.class) this causes some compiler error so we are now doing this by hand
29+
public class AnnotationProcessor extends AbstractProcessor {
30+
31+
@Override
32+
public Set<String> getSupportedAnnotationTypes() {
33+
Set<String> set = new LinkedHashSet<>();
34+
set.add(GenerateRealmStringFields.class.getCanonicalName());
35+
set.add(GenerateRealmFields.class.getCanonicalName());
36+
37+
return set;
38+
}
39+
40+
@Override
41+
public synchronized boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
42+
for (Element element : roundEnv.getElementsAnnotatedWith(GenerateRealmStringFields.class)) {
43+
if (!(element instanceof TypeElement)) continue;
44+
45+
TypeElement typeElement = (TypeElement) element;
46+
List<VariableElement> variableElements = ElementFilter.fieldsIn(typeElement.getEnclosedElements());
47+
List<FieldSpec> fieldSpecs = new LinkedList<>();
48+
49+
for (VariableElement variableElement : variableElements) {
50+
51+
boolean isIgnored = variableElement.getAnnotation(Ignore.class) != null;
52+
if (isIgnored) continue;
53+
54+
Type type = String.class;
55+
56+
57+
String name = variableElement.getSimpleName().toString();
58+
name = name.replaceAll("([A-Z])", "_$1").toUpperCase();
59+
Modifier[] modifiers = {Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL};
60+
CodeBlock codeBlock = CodeBlock.of("$S", variableElement.getSimpleName());
61+
62+
FieldSpec fieldSpec = FieldSpec.builder(type, name, modifiers)
63+
.initializer(codeBlock)
64+
.build();
65+
66+
fieldSpecs.add(fieldSpec);
67+
}
68+
69+
String packageName = "com.quarkworks.android.realmtypesafequery.generated";
70+
String className = typeElement.getSimpleName() + "StringFields";
71+
72+
TypeSpec typeSpec = TypeSpec.classBuilder(className)
73+
.addFields(fieldSpecs)
74+
.addModifiers(Modifier.PUBLIC)
75+
.build();
76+
77+
JavaFile javaFile = JavaFile.builder(packageName, typeSpec).build();
78+
79+
try {
80+
javaFile.writeTo(this.processingEnv.getFiler());
81+
} catch (IOException e) {
82+
this.reportError(element, e.toString());
83+
}
84+
}
85+
86+
return true;
87+
}
88+
89+
private void reportError(Element element, CharSequence message) {
90+
this.processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, message, element);
91+
}
92+
93+
private void reportWarning(Element element, CharSequence message) {
94+
this.processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, message, element);
95+
}
96+
97+
private void log(CharSequence message) {
98+
this.processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, message);
99+
}
100+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.quarkworks.android.realmtypesafequery.AnnotationProcessor

annotations/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

annotations/build.gradle

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
apply plugin: 'java'
2+
3+
dependencies {
4+
compile fileTree(dir: 'libs', include: ['*.jar'])
5+
}
6+
7+
sourceCompatibility = 1.7
8+
targetCompatibility = 1.7
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.quarkworks.android.realmtypesafequery.annotations;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Target(ElementType.TYPE)
9+
@Retention(RetentionPolicy.SOURCE)
10+
public @interface GenerateRealmFields {}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.quarkworks.android.realmtypesafequery.annotations;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Target(ElementType.TYPE)
9+
@Retention(RetentionPolicy.SOURCE)
10+
public @interface GenerateRealmStringFields {}

build.gradle

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Top-level build file where you can add configuration options common to all sub-projects/modules.
2+
3+
buildscript {
4+
repositories {
5+
jcenter()
6+
}
7+
dependencies {
8+
classpath 'com.android.tools.build:gradle:2.1.2'
9+
classpath "io.realm:realm-gradle-plugin:1.1.1"
10+
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3' // jitpack.io
11+
12+
// NOTE: Do not place your application dependencies here; they belong
13+
// in the individual module build.gradle files
14+
}
15+
}
16+
17+
allprojects {
18+
repositories {
19+
jcenter()
20+
}
21+
}
22+
23+
task clean(type: Delete) {
24+
delete rootProject.buildDir
25+
}

0 commit comments

Comments
 (0)