Skip to content

Commit

Permalink
writing the beginnings of the compiler. will begin full run of it. go…
Browse files Browse the repository at this point in the history
…od things coming out of it
  • Loading branch information
Andrew Grosner committed Oct 28, 2014
1 parent 1b702ed commit 5b17ba3
Show file tree
Hide file tree
Showing 39 changed files with 790 additions and 43 deletions.
47 changes: 47 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
apply plugin: 'com.android.application'
apply plugin: 'android-apt'
repositories {
mavenCentral()
}

android {
compileSdkVersion 21
buildToolsVersion "21.0.2"

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}

defaultConfig {
minSdkVersion 8
targetSdkVersion 21
versionCode 1
versionName "1.0"
}

lintOptions {
abortOnError false
}

packagingOptions {
exclude 'META-INF/services/javax.annotation.processing.Processor'
}
}

apt {
arguments {
schematicOutPackage 'com.grosner.dbflow.app.provider'
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
apt project(':compiler')
compile project(':library')
compile 'com.android.support:appcompat-v7:21.0.0'
}

android.applicationVariants.all { variant ->
variant.javaCompile.dependsOn ':compiler:jar'
}
18 changes: 18 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.grosner.dbflow.app" >

<application
android:allowBackup="true"
android:label="DB Flow Compiler Test" >
<activity
android:name=".DemoActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
20 changes: 20 additions & 0 deletions app/src/main/java/com/grosner/dbflow/app/AModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.grosner.dbflow.app;

import com.grosner.dbflow.annotation.Column;
import com.grosner.dbflow.annotation.Table;
import com.grosner.dbflow.structure.BaseModel;

/**
* Author: andrewgrosner
* Contributors: { }
* Description:
*/
@Table(name = "AModel")
public class AModel extends BaseModel {

@Column(columnType = Column.PRIMARY_KEY, name = "name")
String name;

@Column(name = "time")
long time;
}
39 changes: 39 additions & 0 deletions app/src/main/java/com/grosner/dbflow/app/DemoActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.grosner.dbflow.app;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class DemoActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_demo, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}
16 changes: 16 additions & 0 deletions app/src/main/res/layout/activity_demo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.grosner.dbflow.app.DemoActivity">

<TextView
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</RelativeLayout>
9 changes: 9 additions & 0 deletions app/src/main/res/menu/menu_demo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.grosner.dbflow.app.DemoActivity">
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never"/>
</menu>
6 changes: 6 additions & 0 deletions app/src/main/res/values-w820dp/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<resources>
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
(such as screen margins) for screens with more than 820dp of available width. This
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
<dimen name="activity_horizontal_margin">64dp</dimen>
</resources>
5 changes: 5 additions & 0 deletions app/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
7 changes: 7 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<resources>
<string name="title_activity_demo">DemoActivity</string>

<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>

</resources>
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.13.3'
classpath 'com.android.tools.build:gradle:0.13.2'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.+'
}
}

Expand Down
10 changes: 10 additions & 0 deletions compiler/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apply plugin: 'java'

targetCompatibility = JavaVersion.VERSION_1_7
sourceCompatibility = JavaVersion.VERSION_1_7

dependencies {
compile project(':flowcore')
compile 'com.squareup:javawriter:2.5.0'
compile 'com.google.auto.service:auto-service:1.0+'
}
100 changes: 100 additions & 0 deletions compiler/src/main/java/com/grosner/processor/DBFlowProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.grosner.processor;

import com.google.auto.service.AutoService;
import com.grosner.dbflow.annotation.Column;
import com.grosner.dbflow.annotation.Table;
import com.grosner.processor.model.TableDefinition;
import com.squareup.javawriter.JavaWriter;

import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;

/**
* Author: andrewgrosner
* Contributors: { }
* Description:
*/
@AutoService(Processor.class)
public class DBFlowProcessor extends AbstractProcessor {

/**
* If the processor class is annotated with {@link
* javax.annotation.processing.SupportedAnnotationTypes}, return an unmodifiable set with the
* same set of strings as the annotation. If the class is not so
* annotated, an empty set is returned.
*
* @return the names of the annotation types supported by this
* processor, or an empty set if none
*/
@Override
public Set<String> getSupportedAnnotationTypes() {
Set<String> supportedTypes = new LinkedHashSet<String>();
supportedTypes.add(Table.class.getName());
supportedTypes.add(Column.class.getName());
return supportedTypes;
}

/**
* If the processor class is annotated with {@link
* javax.annotation.processing.SupportedSourceVersion}, return the source version in the
* annotation. If the class is not so annotated, {@link
* javax.lang.model.SourceVersion#RELEASE_6} is returned.
*
* @return the latest source version supported by this processor
*/
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
}

/**
* {@inheritDoc}
*
* @param annotations
* @param roundEnv
*/
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
final Set<? extends Element> annotatedElements = roundEnv
.getElementsAnnotatedWith(Table.class);

//process() gets called more than once, annotatedElements might be empty an empty Set in one of those calls( i.e. when there are no annotations to process this round).
if (annotatedElements.size() > 0) {

Iterator<? extends Element> iterator = annotatedElements.iterator();
while(iterator.hasNext()) {
Element element = iterator.next();
System.out.println(element.asType());

try {
final String packageName = processingEnv.getElementUtils()
.getPackageOf(element).toString();
TableDefinition tableDefinition = new TableDefinition(packageName, element);
JavaWriter javaWriter = new JavaWriter(processingEnv.getFiler().createSourceFile(tableDefinition.getFQCN()).openWriter());
tableDefinition.write(javaWriter);
javaWriter.close();

tableDefinition.writeAdapter(processingEnv);
} catch (IOException e) {
e.printStackTrace();
}
}

}
// return true if we successfully processed the Annotation.
return true;
}

private void createHeader(JavaWriter javaWriter, String packageName) throws IOException {
javaWriter.emitPackage(packageName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.grosner.processor.model;

import com.google.common.collect.Sets;
import com.grosner.dbflow.annotation.Column;
import com.squareup.javawriter.JavaWriter;

import javax.lang.model.element.Element;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.VariableElement;
import java.io.IOException;

/**
* Author: andrewgrosner
* Contributors: { }
* Description:
*/
public class ColumnDefinition implements FlowWriter{

String columnName;

String columnFieldName;

String columnFieldType;

int columnType;

Element element;

public ColumnDefinition(VariableElement element) {
this.element = element;

Column column = element.getAnnotation(Column.class);
this.columnName = column.name();
this.columnFieldName = element.getSimpleName().toString();
this.columnFieldType = element.asType().toString();
columnType = column.columnType();
}


@Override
public String getFQCN() {
return null;
}

@Override
public void write(JavaWriter javaWriter) throws IOException {
javaWriter.emitField("String", columnName.toUpperCase(),
Sets.newHashSet(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL),
"\""+columnName+"\"");
javaWriter.emitEmptyLine();
}

public void writeContentValue(JavaWriter javaWriter) throws IOException {
javaWriter.emitStatement("contentValues.put(%1s, %1s)");
}
}
Loading

0 comments on commit 5b17ba3

Please sign in to comment.