Skip to content

Commit

Permalink
Improved field sorter to support multiple source folders (issue #55).
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Sep 5, 2012
1 parent f892cba commit f41ddff
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 31 deletions.
40 changes: 33 additions & 7 deletions src/main/java/com/tightdb/generator/CodeGenProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand All @@ -31,6 +32,8 @@

public class CodeGenProcessor extends AbstractAnnotationProcessor {

private static final String SOURCE_FOLDERS = "source_folders";

private static final String MSG_INCORRECT_TYPE = "Incorrect data type was specified! Expected primitive or wrapper type, byte[], java.lang.Object, java.util.Date or java.nio.ByteBuffer!";

private static final String MSG_NO_COLUMNS = "The table specification must have at least one valid field/column specified!";
Expand All @@ -57,7 +60,7 @@ public class CodeGenProcessor extends AbstractAnnotationProcessor {
@Override
public void processAnnotations(Set<? extends TypeElement> annotations,
RoundEnvironment env) throws Exception {
fieldSorter = new FieldSorter(logger);
fieldSorter = new FieldSorter(logger, getSourceFolders());

for (TypeElement annotation : annotations) {
String annotationName = annotation.getQualifiedName().toString();
Expand All @@ -82,8 +85,23 @@ private void processAnnotatedElements(Set<? extends Element> elements)
"The path of the Java source and generated files must be configured as source output! (see -s option of javac)");
}

List<File> sourcesPath = new LinkedList<File>();

File file = new File(uri);
File sourcesPath = file.getParentFile();
File generatedSourcesPath = file.getParentFile();
sourcesPath.add(generatedSourcesPath);

String[] sourceFolders = getSourceFolders();
while (generatedSourcesPath != null) {
for (String sourceFolder : sourceFolders) {
File potentialPath = new File(generatedSourcesPath, sourceFolder);
if (potentialPath.exists()) {
sourcesPath.add(potentialPath);
logger.info("Configured source folder: " + potentialPath);
}
}
generatedSourcesPath = generatedSourcesPath.getParentFile();
}

prepareTables(elements);

Expand All @@ -93,14 +111,13 @@ private void processAnnotatedElements(Set<? extends Element> elements)
setupModelInfo(model);
}
}

for (Element element : elements) {
if (element instanceof TypeElement) {
TypeElement model = (TypeElement) element;
processModel(sourcesPath, model);
}
}

}

private void setupModelInfo(TypeElement model) {
Expand All @@ -124,13 +141,13 @@ private void setupModelInfo(TypeElement model) {
queryName));
}

private void processModel(File sourcesPath, TypeElement model) {
private void processModel(List<File> sourceFolders, TypeElement model) {
String modelType = model.getQualifiedName().toString();

List<VariableElement> fields = getFields(model);

// sort the fields, due to unresolved bug in Eclipse APT
fieldSorter.sortFields(fields, model, sourcesPath);
fieldSorter.sortFields(fields, model, sourceFolders);

// get the capitalized model name
String entity = StringUtils
Expand Down Expand Up @@ -192,7 +209,7 @@ private List<Model> getColumns(List<VariableElement> fields) {
column.put("paramType", paramType);
column.put("index", index++);
column.put("isSubtable", isSubtable);

if (isSubtable) {
ModelInfo subModelInfo = modelsInfo.get(subtype);
column.put("subTableName", subModelInfo.getTableName());
Expand Down Expand Up @@ -445,4 +462,13 @@ private static String getAttribute(AnnotationMirror annotationMirror,
}
return null;
}

private String[] getSourceFolders() {
String sourceFolders = options.get(SOURCE_FOLDERS);
if (sourceFolders != null) {
return sourceFolders.split("[\\:\\,\\;]");
} else {
return new String[] { "src", "src/main/java", "src/test/java" };
}
}
}
8 changes: 4 additions & 4 deletions src/main/java/com/tightdb/generator/FieldSorter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public class FieldSorter {
private AnnotationProcessingLogger logger;
private TableSpecReader specReader;

public FieldSorter(AnnotationProcessingLogger logger) {
public FieldSorter(AnnotationProcessingLogger logger, String[] sourceFolders) {
this.logger = logger;
this.specReader = new TableSpecReader(logger);
this.specReader = new TableSpecReader(logger, sourceFolders);
}

public void sortFields(List<VariableElement> fields, TypeElement model, File sourcePath) {
String specSource = specReader.getSpecFields(model, sourcePath);
public void sortFields(List<VariableElement> fields, TypeElement model, List<File> sourcesPath) {
String specSource = specReader.getSpecFields(model, sourcesPath);
if (specSource == null) {
logger.warn("Field sorting failed, couldn't find table spec: " + model.getSimpleName());
return;
Expand Down
55 changes: 35 additions & 20 deletions src/main/java/com/tightdb/generator/TableSpecReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import javax.lang.model.element.TypeElement;

Expand All @@ -18,15 +19,16 @@ public class TableSpecReader {

private SpecMatcher specMatcher;

public TableSpecReader(AnnotationProcessingLogger logger) {
public TableSpecReader(AnnotationProcessingLogger logger,
String[] sourceFolders) {
this.logger = logger;
this.specMatcher = new SpecMatcher(logger);
}

public String getSpecFields(TypeElement model, File sourcePath) {
public String getSpecFields(TypeElement model, List<File> sourcesPath) {
String modelName = model.toString();

File sourceFile = findSourceFile(sourcePath, modelName);
File sourceFile = findSourceFile(sourcesPath, modelName);
if (sourceFile == null) {
logger.warn("Table spec retrieval failed!");
return null;
Expand All @@ -37,32 +39,41 @@ public String getSpecFields(TypeElement model, File sourcePath) {
try {
source = FileUtils.readFileToString(sourceFile);
} catch (IOException e) {
logger.warn("Table spec retrieval failed, couldn't read file: " + sourceFile);
logger.warn("Table spec retrieval failed, couldn't read file: "
+ sourceFile);
return null;
}

String spec = specMatcher.matchSpec(model.getSimpleName().toString(), source);
String spec = specMatcher.matchSpec(model.getSimpleName().toString(),
source);
if (spec == null) {
logger.warn("Table spec retrieval failed, couldn't find table spec: " + modelName);
logger.warn("Table spec retrieval failed, couldn't find table spec: "
+ modelName);
}
return spec;

}

private File findSourceFile(File sourcePath, String modelName) {
private File findSourceFile(List<File> sourceFolders, String modelName) {
String[] modelNameParts = modelName.split("\\.");

for (String part : modelNameParts) {
File path = new File(sourcePath.getAbsolutePath() + File.separator + part);
if (path.isDirectory()) {
sourcePath = path;
} else {
File sourceFile = new File(path.getAbsolutePath() + ".java");
if (sourceFile.exists() && sourceFile.isFile()) {
return sourceFile;
for (File sourceFolder : sourceFolders) {
File folder = sourceFolder;
for (String part : modelNameParts) {
File path = new File(folder, part);
if (path.isDirectory()) {
folder = path;
} else {
logger.warn("The file doesn't exist: " + sourceFile);
return scanSourcePath(sourcePath, modelNameParts[modelNameParts.length - 1]);
File sourceFile = new File(path.getAbsolutePath() + ".java");
if (sourceFile.exists() && sourceFile.isFile()) {
return sourceFile;
} else {
File sf = scanSourcePath(folder,
modelNameParts[modelNameParts.length - 1]);
if (sf != null) {
return sf;
}
}
}
}
}
Expand All @@ -72,10 +83,14 @@ private File findSourceFile(File sourcePath, String modelName) {
}

private File scanSourcePath(File sourcePath, String modelName) {
logger.debug("Scanning source path '" + sourcePath + "' for table spec '" + modelName + "'");
IOFileFilter fileFilter = new AndFileFilter(new SuffixFileFilter(".java"), new SpecSourceFileFilter(specMatcher, modelName, logger));
logger.debug("Scanning source path '" + sourcePath
+ "' for table spec '" + modelName + "'");
IOFileFilter fileFilter = new AndFileFilter(new SuffixFileFilter(
".java"), new SpecSourceFileFilter(specMatcher, modelName,
logger));
IOFileFilter dirFilter = FalseFileFilter.FALSE;
Collection<File> files = FileUtils.listFiles(sourcePath, fileFilter, dirFilter);
Collection<File> files = FileUtils.listFiles(sourcePath, fileFilter,
dirFilter);

switch (files.size()) {
case 0:
Expand Down
Binary file modified tightdb-android/libs/tightdb/tightdb-devkit.jar
Binary file not shown.
Binary file modified tightdb-example/lib/tightdb-devkit.jar
Binary file not shown.

0 comments on commit f41ddff

Please sign in to comment.