-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
160 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
JMapper Framework/src/main/java/com/googlecode/jmapper/generation/ICodeGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* Copyright (C) 2012 - 2015 Alessandro Vurro. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.googlecode.jmapper.generation; | ||
|
||
import java.util.List; | ||
|
||
import com.googlecode.jmapper.generation.beans.Constructor; | ||
import com.googlecode.jmapper.generation.beans.Method; | ||
|
||
/** | ||
* Implements this interface if you want define a custom code generation. | ||
* | ||
* @author Alessandro Vurro | ||
* | ||
*/ | ||
public interface ICodeGenerator { | ||
|
||
/** | ||
* Generates a class with this parameters. | ||
* @param clazzName class name | ||
* @param constructors list of costructors | ||
* @param methods list of methods | ||
* @return the generated class | ||
* @throws Throwable | ||
*/ | ||
Class<?> generate(String clazzName,List<Constructor> constructors,List<Method> methods) throws Throwable; | ||
|
||
} |
92 changes: 92 additions & 0 deletions
92
JMapper Framework/src/main/java/com/googlecode/jmapper/generation/JavassistGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/** | ||
* Copyright (C) 2012 - 2015 Alessandro Vurro. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.googlecode.jmapper.generation; | ||
|
||
import java.util.List; | ||
|
||
import javassist.CannotCompileException; | ||
import javassist.ClassPool; | ||
import javassist.CtClass; | ||
import javassist.CtConstructor; | ||
import javassist.CtMethod; | ||
import javassist.NotFoundException; | ||
|
||
import com.googlecode.jmapper.IMapper; | ||
import com.googlecode.jmapper.config.Error; | ||
import com.googlecode.jmapper.generation.beans.Constructor; | ||
import com.googlecode.jmapper.generation.beans.Method; | ||
|
||
/** | ||
* Javassist implementation. | ||
* | ||
* @author Alessandro Vurro | ||
* | ||
*/ | ||
public class JavassistGenerator implements ICodeGenerator { | ||
|
||
public Class<?> generate(String clazzName, List<Constructor> constructors, | ||
List<Method> methods) throws Throwable { | ||
try{ | ||
ClassPool cp = ClassPool.getDefault(); | ||
// create the class | ||
CtClass cc = cp.makeClass(clazzName); | ||
|
||
// adds the interface | ||
cc.addInterface(cp.get(IMapper.class.getName())); | ||
|
||
// adds constructor | ||
for (Constructor constructor : constructors) { | ||
// create constructor | ||
CtConstructor ctConstructor = new CtConstructor(toCtClass(constructor.getParameters()), cc); | ||
// set body constructor | ||
ctConstructor.setBody(constructor.getBody()); | ||
// add constructor to CtClass | ||
cc.addConstructor(ctConstructor); | ||
} | ||
|
||
// adds methods | ||
for (Method method : methods) { | ||
try{// create method | ||
CtMethod ctMethod = new CtMethod(toCtClass(method.getReturnType())[0],method.getName(), toCtClass(method.getParameters()), cc); | ||
// set body method | ||
ctMethod.setBody(method.getBody()); | ||
// add method to CtClass | ||
cc.addMethod(ctMethod); } | ||
catch (CannotCompileException e) { Error.bodyContainsIllegalCode(method,e); } | ||
} | ||
|
||
Class<?> generetedClass = cc.toClass(); | ||
cc.defrost(); | ||
return generetedClass; | ||
}catch (NotFoundException e) { Error.notFoundException(e); } | ||
return null; | ||
} | ||
|
||
/** | ||
* This method transforms classes in CtClass[] | ||
* @param classes | ||
* @return CtClass[] version of classes parameter | ||
* @throws Exception in case of not found class | ||
*/ | ||
private static CtClass[] toCtClass(Class<?>... classes) throws Exception{ | ||
ClassPool cp = ClassPool.getDefault(); | ||
CtClass[] parameters = new CtClass[classes.length]; | ||
for(int i=0;i<classes.length;i++) | ||
parameters[i]=cp.get(classes[i].getName()); | ||
|
||
return parameters; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters