diff --git a/JMapper Framework/pom.xml b/JMapper Framework/pom.xml index 7a7ab735..3c56e80a 100644 --- a/JMapper Framework/pom.xml +++ b/JMapper Framework/pom.xml @@ -135,7 +135,13 @@ - + + + org.reflections + reflections + 0.9.10 + + com.googlecode.jmapper-framework jmapper-api diff --git a/JMapper Framework/src/main/java/com/googlecode/jmapper/JMapper.java b/JMapper Framework/src/main/java/com/googlecode/jmapper/JMapper.java index 0d2e884a..8efa14d6 100644 --- a/JMapper Framework/src/main/java/com/googlecode/jmapper/JMapper.java +++ b/JMapper Framework/src/main/java/com/googlecode/jmapper/JMapper.java @@ -16,8 +16,8 @@ package com.googlecode.jmapper; import static com.googlecode.jmapper.generation.MapperBuilder.from; -import javassist.NotFoundException; import static com.googlecode.jmapper.util.GeneralUtility.isNull; + import com.googlecode.jmapper.api.IJMapper; import com.googlecode.jmapper.api.enums.MappingType; import com.googlecode.jmapper.api.enums.NullPointerControl; @@ -401,10 +401,10 @@ public JMapper(final Class destination,final Class source,final ChooseCon .analyzing(config) .presentIn(xmlPath)); - }catch (Exception e) { JmapperLog.ERROR(e); } + }catch (Throwable e) { JmapperLog.ERROR(e); } } - private IMapper createMapper(MapperBuilder mapper) throws NotFoundException, Exception{ + private IMapper createMapper(MapperBuilder mapper) throws Throwable{ Class> mapperClass = mapper.exist()?mapper.get() :mapper.generate(); diff --git a/JMapper Framework/src/main/java/com/googlecode/jmapper/config/JmapperLog.java b/JMapper Framework/src/main/java/com/googlecode/jmapper/config/JmapperLog.java index 167d81a1..7a3f020b 100644 --- a/JMapper Framework/src/main/java/com/googlecode/jmapper/config/JmapperLog.java +++ b/JMapper Framework/src/main/java/com/googlecode/jmapper/config/JmapperLog.java @@ -41,7 +41,7 @@ private JmapperLog(){} * * @param e exception to handle */ - public static void ERROR(Exception e) throws JMapperException{ + public static void ERROR(Throwable e) throws JMapperException{ logger.error("{}: {}",e.getClass().getSimpleName(),e.getMessage()); } } diff --git a/JMapper Framework/src/main/java/com/googlecode/jmapper/generation/ICodeGenerator.java b/JMapper Framework/src/main/java/com/googlecode/jmapper/generation/ICodeGenerator.java new file mode 100644 index 00000000..1e03dad5 --- /dev/null +++ b/JMapper Framework/src/main/java/com/googlecode/jmapper/generation/ICodeGenerator.java @@ -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 constructors,List methods) throws Throwable; + +} diff --git a/JMapper Framework/src/main/java/com/googlecode/jmapper/generation/JavassistGenerator.java b/JMapper Framework/src/main/java/com/googlecode/jmapper/generation/JavassistGenerator.java new file mode 100644 index 00000000..76a3342e --- /dev/null +++ b/JMapper Framework/src/main/java/com/googlecode/jmapper/generation/JavassistGenerator.java @@ -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 constructors, + List 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 @@ -75,11 +73,9 @@ public Class> get() { * @param Destination Class * @param Source Class * @return the generated mapper class - * @throws NotFoundException if class doesn't exists - * @throws Exception in other cases such as illegalcode + * @throws Throwable * */ - public Class> generate() throws NotFoundException, - Exception { + public Class> generate() throws Throwable { // if defined the dynamic methods are treated differently // a reference to this list is passed to the MapperConstructor and filled recursively diff --git a/JMapper Framework/src/main/java/com/googlecode/jmapper/generation/MapperGenerator.java b/JMapper Framework/src/main/java/com/googlecode/jmapper/generation/MapperGenerator.java index b2ac9eed..bb3a31ec 100644 --- a/JMapper Framework/src/main/java/com/googlecode/jmapper/generation/MapperGenerator.java +++ b/JMapper Framework/src/main/java/com/googlecode/jmapper/generation/MapperGenerator.java @@ -19,20 +19,15 @@ import static com.googlecode.jmapper.util.GeneralUtility.list; import java.util.ArrayList; -import java.util.List; import java.util.Map; import java.util.Set; -import javassist.CannotCompileException; import javassist.ClassClassPath; import javassist.ClassPool; -import javassist.CtClass; -import javassist.CtConstructor; -import javassist.CtMethod; -import javassist.NotFoundException; + +import org.reflections.Reflections; 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; @@ -52,10 +47,9 @@ public class MapperGenerator { * @param mapping parameter that containts the mappings * @param dynamicMethods dynamic methods to add * @return a new instance of IMapper interface, following the mappingBuilder specifications - * @throws NotFoundException if class to generate doesn't exists - * @throws Exception other cases + * @throws Throwable */ - public static Class generateMapperClass(MapperConstructor mapping, Set dynamicMethods) throws NotFoundException, Exception{ + public static Class generateMapperClass(MapperConstructor mapping, Set dynamicMethods) throws Throwable{ // adds empty constructor ArrayList constructors = list(new Constructor()); @@ -71,69 +65,13 @@ public static Class generateMapperClass(MapperConstructor mapping, Set generateClass(String clazzName,List constructors,List methods) throws Exception { - 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> generators = new Reflections("com.googlecode.jmapper.generation").getSubTypesOf(ICodeGenerator.class); + + ICodeGenerator generator = generators.isEmpty()? new JavassistGenerator():generators.iterator().next().newInstance(); + + return generator.generate(className, constructors, methods); } + + } \ No newline at end of file diff --git a/JMapper Framework/src/main/java/com/googlecode/jmapper/operations/OperationHandler.java b/JMapper Framework/src/main/java/com/googlecode/jmapper/operations/OperationHandler.java index f29bc64d..ae712b08 100644 --- a/JMapper Framework/src/main/java/com/googlecode/jmapper/operations/OperationHandler.java +++ b/JMapper Framework/src/main/java/com/googlecode/jmapper/operations/OperationHandler.java @@ -159,11 +159,8 @@ public void loadStructures(Set dynamicMethodsToWrite) { conversionHandler.load(conversionAnalyzer) .from(sourceMappedField).to(destinationMappedField); - if(conversionHandler.toBeCreated()){ - Method method = conversionHandler.loadMethod(); - System.out.println(method.getBody()); - dynamicMethodsToWrite.add(method); - } + if(conversionHandler.toBeCreated()) + dynamicMethodsToWrite.add(conversionHandler.loadMethod()); operation.setConversionMethod(conversionHandler.getMethod()) .setMemberShip (conversionHandler.getMembership());