Skip to content

Commit

Permalink
generating reverse
Browse files Browse the repository at this point in the history
  • Loading branch information
daolena-a committed Feb 14, 2012
1 parent 9851d9c commit 8b8facc
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 85 deletions.
84 changes: 0 additions & 84 deletions pom.xml
Expand Up @@ -12,89 +12,5 @@
<module>processor</module>
<module>testApp</module>
</modules>
<dependencies><dependency>
<groupId>com.google.code.javaparser</groupId>
<artifactId>javaparser</artifactId>
<version>1.0.1</version>
</dependency>

</dependencies>
<pluginRepositories>

<!-- THIS REPOSITORY IS NOT LONGER VALID
<pluginRepository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2/</url>
<layout>default</layout>
</pluginRepository>
-->

<!-- IF YOU WANT STAY TUNED ON UPDATE REMOVE COMMENT -->

<pluginRepository>
<id>sonatype-repo</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>

</pluginRepositories>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<filesets>
<fileset>
<directory>src/main/java/org/mapper/test/gen</directory>
<followSymlinks>false</followSymlinks>
<excludes>
<exclude>**/.git/**</exclude>
</excludes>
</fileset>
</filesets>
</configuration>
</plugin>

<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<!-- source output directory -->
<outputDirectory>src/main/generated</outputDirectory>
<processors>
<!-- list of processors to use -->
<processor>org.mapper.processor.MappedByProcessor</processor>
</processors>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<!--<source>1.7</source>-->
<!--<target>1.7</target>-->
<compilerArgument>-proc:none</compilerArgument>
</configuration>
</plugin>
</plugins>
</build>
</project>
Expand Up @@ -29,6 +29,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
MappedBy anno;
ClassWriter classWriter;
System.out.println("processor");

for(Element elem : roundEnv.getElementsAnnotatedWith(MappedBy.class)){

Class<?> clazz = null;
Expand Down
Expand Up @@ -30,23 +30,60 @@ public void generateSourceCode(Class<T> clazz){
source.addLine("package org.mapper.test.gen;");
source.addLine("");
source.addLine("import "+clazz.getCanonicalName()+";");
source.addLine("import org.mapper.test.gen.*;");
source.addLine("import "+anno.targetedClass().getCanonicalName()+";");
source.addLine("public class "+clazz.getSimpleName()+"To"+anno.targetedClass().getSimpleName()+"{");
source.addLine("public static "+clazz.getSimpleName()+" convert("+anno.targetedClass().getSimpleName()+" value){");
source.addLine(clazz.getSimpleName()+" result = new "+clazz.getSimpleName()+"();");
MappedByField annoField;
for (Field f : clazz.getDeclaredFields()){
annoField = (MappedByField) f.getAnnotation(MappedByField.class);
if (annoField == null) break;
if(f.getClass().getAnnotation(MappedBy.class) != null){
new SourceBuilder().generateSourceCode(f.getClass());
if(annoField.fieldName() != null && annoField.fieldName().length() > 0)
source.addLine("result.set"+camelCase(f.getName())+"("+f.getClass().getSimpleName()+"To"+f.getClass().getAnnotation(MappedBy.class).targetedClass().getSimpleName() +".convert(value.get"+camelCase(annoField.fieldName())+"()));");
else source.addLine("result.set"+camelCase(f.getName())+"("+f.getClass().getSimpleName()+"To"+f.getClass().getAnnotation(MappedBy.class).targetedClass().getSimpleName() +".convert(value.get"+camelCase(f.getName())+"()));");
continue;
}
if (annoField == null) continue;

if(annoField.fieldName() != null && annoField.fieldName().length() > 0)
source.addLine("result.set"+camelCase(f.getName())+"(value.get"+camelCase(annoField.fieldName())+"());");
else{
source.addLine("result.set"+camelCase(f.getName())+"(value.get"+camelCase(f.getName())+"());");
}

}
source.addLine("return result;}");

source.addLine("");
//reverse
source.addLine("public static "+anno.targetedClass().getSimpleName()+" convertReverse("+clazz.getSimpleName()+" value){");
source.addLine(anno.targetedClass().getSimpleName()+" result = new "+anno.targetedClass().getSimpleName()+"();");
for (Field f : clazz.getDeclaredFields()){
annoField = (MappedByField) f.getAnnotation(MappedByField.class);
if(f.getClass().getAnnotation(MappedBy.class) != null){
new SourceBuilder().generateSourceCode(f.getClass());
if(annoField.fieldName() != null && annoField.fieldName().length() > 0)
source.addLine("result.set"+camelCase(f.getName())+"("+f.getClass().getSimpleName()+"To"+f.getClass()
.getAnnotation(MappedBy.class).targetedClass().getSimpleName() +".convert(value.get"+
camelCase(annoField.fieldName())+"()));");
else source.addLine("result.set"+camelCase(f.getName())+"("+f.getClass().getSimpleName()+"To"+f.getClass().getAnnotation(MappedBy.class).targetedClass().getSimpleName() +".convert(value.get"+camelCase(f.getName())+"()));");
continue;
}
if (annoField == null) continue;

if(annoField.fieldName() != null && annoField.fieldName().length() > 0)
source.addLine("result.set"+camelCase(annoField.fieldName())+"(value.get"+camelCase(f.getName())+"());");
else{
source.addLine("result.set"+camelCase(f.getName())+"(value.get"+camelCase(f.getName())+"());");
}

}
source.addLine("return result;}");
source.addLine("}");


ClassWriter classWriter = new ClassWriter("./testApp/src/main/java/org/mapper/test/gen/"+clazz.getSimpleName()+"To"+anno.targetedClass().getSimpleName()+".java");
classWriter.writeClass(source);
//package
Expand Down
78 changes: 78 additions & 0 deletions testApp/pom.xml
Expand Up @@ -19,4 +19,82 @@
</dependency>
</dependencies>

<pluginRepositories>

<!-- THIS REPOSITORY IS NOT LONGER VALID
<pluginRepository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2/</url>
<layout>default</layout>
</pluginRepository>
-->

<!-- IF YOU WANT STAY TUNED ON UPDATE REMOVE COMMENT -->

<pluginRepository>
<id>sonatype-repo</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>

</pluginRepositories>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<filesets>
<fileset>
<directory>src/main/java/org/mapper/test/gen</directory>
<followSymlinks>false</followSymlinks>
<excludes>
<exclude>**/.git/**</exclude>
</excludes>
</fileset>
</filesets>
</configuration>
</plugin>

<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<!-- source output directory -->
<outputDirectory>src/main/generated</outputDirectory>
<processors>
<!-- list of processors to use -->
<processor>org.mapper.processor.MappedByProcessor</processor>
</processors>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<!--<source>1.7</source>-->
<!--<target>1.7</target>-->
<compilerArgument>-proc:none</compilerArgument>
</configuration>
</plugin>
</plugins>
</build>
</project>
11 changes: 11 additions & 0 deletions testApp/src/main/java/org/mapper/test/TestComplex.java
@@ -0,0 +1,11 @@
package org.mapper.test;

/**
* Created by IntelliJ IDEA.
* User: adrien.daolena
* Date: 14/02/12
* Time: 16:06
* To change this template use File | Settings | File Templates.
*/
public class TestComplex {
}
11 changes: 11 additions & 0 deletions testApp/src/main/java/org/mapper/test/TestMappedComplex.java
@@ -0,0 +1,11 @@
package org.mapper.test;

/**
* Created by IntelliJ IDEA.
* User: adrien.daolena
* Date: 14/02/12
* Time: 16:06
* To change this template use File | Settings | File Templates.
*/
public class TestMappedComplex {
}

0 comments on commit 8b8facc

Please sign in to comment.