com.github.ludorival.dao.gwt.rebind.EntityManagerGenerator.java Source code

Java tutorial

Introduction

Here is the source code for com.github.ludorival.dao.gwt.rebind.EntityManagerGenerator.java

Source

package com.github.ludorival.dao.gwt.rebind;

import static com.github.ludorival.dao.entity.adapter.AdapterEntityManager.CANNOT_PROCESS_PARAMETERIZED_TYPE;
import static com.github.ludorival.dao.entity.adapter.AdapterEntityManager.EXPLICITELY_IGNORED;
import static com.github.ludorival.dao.entity.adapter.AdapterEntityManager.IGNORE_METHOD;
import static com.github.ludorival.dao.entity.adapter.AdapterEntityManager.NO_PARAMETER_GETTER;
import static com.github.ludorival.dao.entity.adapter.AdapterEntityManager.ONLY_ENTITY_FOR_INDEX;
import static com.github.ludorival.dao.entity.adapter.AdapterEntityManager.SUCCESSFUL_ADD_PROPERTY;
import static com.github.ludorival.dao.entity.adapter.AdapterEntityManager.VOID_GETTER;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.github.ludorival.dao.entity.Bean;
import com.github.ludorival.dao.entity.Entity;
import com.github.ludorival.dao.entity.Index;
import com.github.ludorival.dao.entity.Property;
import com.github.ludorival.dao.entity.Property.Kind;
import com.github.ludorival.dao.entity.adapter.AdapterBean;
import com.github.ludorival.dao.entity.adapter.AdapterEntity;
import com.github.ludorival.dao.entity.adapter.AdapterEntityManager;
import com.github.ludorival.dao.entity.annotation.IsBean;
import com.github.ludorival.dao.entity.annotation.IsEntity;
import com.github.ludorival.dao.entity.annotation.IsIgnored;
import com.github.ludorival.dao.entity.annotation.IsIndexable;
import com.github.ludorival.dao.entity.annotation.IsMemo;
import com.github.ludorival.dao.entity.annotation.KeyOf;
import com.github.ludorival.dao.entity.annotation.OldName;
import com.google.gwt.core.ext.Generator;
import com.google.gwt.core.ext.GeneratorContext;
import com.google.gwt.core.ext.TreeLogger;
import com.google.gwt.core.ext.TreeLogger.Type;
import com.google.gwt.core.ext.UnableToCompleteException;
import com.google.gwt.core.ext.typeinfo.JClassType;
import com.google.gwt.core.ext.typeinfo.JConstructor;
import com.google.gwt.core.ext.typeinfo.JMethod;
import com.google.gwt.core.ext.typeinfo.JParameterizedType;
import com.google.gwt.core.ext.typeinfo.JPrimitiveType;
import com.google.gwt.core.ext.typeinfo.JType;
import com.google.gwt.core.ext.typeinfo.TypeOracle;
import com.google.gwt.core.ext.typeinfo.TypeOracleException;
import com.google.gwt.user.rebind.ClassSourceFileComposerFactory;
import com.google.gwt.user.rebind.SourceWriter;

/*
 * #%L
 * DAO
 * %%
 * Copyright (C) 2015 ludorival
 * %%
 * 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.
 * #L%
 */

public class EntityManagerGenerator extends Generator {

    private class Source {
        private final String packageName;

        private final String className;

        public Source(String packageName, String className) {
            this.packageName = packageName;
            this.className = className;
        }

        @Override
        public String toString() {
            return packageName + "." + className;
        }

    }

    private class BeanMetadata {

        private final String name;

        private final JClassType type;

        private final Source implementation;

        private final boolean entity;

        public BeanMetadata(JClassType type, String name, Source implementation, boolean entity) {
            this.type = type;
            this.name = name;
            this.implementation = implementation;
            this.entity = entity;
        }

        @Override
        public String toString() {
            return name + "(" + type + ") - " + implementation;
        }

    }

    private final boolean parseOnlyInterface;

    public EntityManagerGenerator() {
        this(false);
    }

    public EntityManagerGenerator(boolean parseOnlyInterface) {
        this.parseOnlyInterface = parseOnlyInterface;
    }

    @Override
    public String generate(TreeLogger logger, GeneratorContext context, String typeName)
            throws UnableToCompleteException {
        final TypeOracle typeOracle = context.getTypeOracle();
        JClassType mainType = typeOracle.findType(typeName);

        String packageName = mainType.getPackage().getName();
        String className = "Gwt" + mainType.getName();
        if (parseOnlyInterface)
            className += "Light";
        PrintWriter writer = context.tryCreate(logger, packageName, className);
        if (writer == null) {
            return packageName + "." + className;
        }

        ClassSourceFileComposerFactory factory = new ClassSourceFileComposerFactory(packageName, className);
        logger.log(Type.DEBUG, "Create EntityManager " + factory.getCreatedClassName());
        factory.setSuperclass(AdapterEntityManager.class.getSimpleName());
        factory.addImport(AdapterEntityManager.class.getName());
        factory.addImport(Entity.class.getName());
        factory.addImport(Bean.class.getName());
        factory.addImport(HashMap.class.getName());
        factory.addImport("javax.annotation.Generated");

        JClassType[] types = typeOracle.getTypes();
        List<BeanMetadata> metadatas = new ArrayList<EntityManagerGenerator.BeanMetadata>();
        for (JClassType type : types) {
            BeanMetadata metaData = null;
            boolean candidate = false;
            if (type.isAnnotationPresent(IsEntity.class)) {
                candidate = true;
                try {
                    metaData = createEntity(context, logger, packageName, type, type.getAnnotation(IsEntity.class));
                } catch (TypeOracleException e) {
                    logger.log(Type.ERROR, e.getMessage(), e);
                    continue;
                }

            } else if (type.isAnnotationPresent(IsBean.class)) {
                candidate = true;
                try {
                    metaData = createBean(context, logger, packageName, type, type.getAnnotation(IsBean.class));
                } catch (TypeOracleException e) {
                    logger.log(Type.ERROR, e.getMessage(), e);
                    continue;
                }

            }
            if (!candidate)
                continue;
            if (metaData == null) {
                log(logger, Type.WARN, "The type %s is not instantiable", type);
                continue;
            }
            log(logger, Type.DEBUG, "The entity has been build : %s", metaData);
            factory.addImport(type.getQualifiedSourceName());

            if (metaData.implementation != null) {
                factory.addImport(metaData.implementation + "");
            }
            metadatas.add(metaData);
        }

        factory.addAnnotationDeclaration("@Generated(" + "value=\"" + AdapterEntityManager.class.getName() + "\", "
                + "date=\"" + new Date() + "\", " + "comments=\"Generated by DAO-GWT project.\")");

        SourceWriter sourceWriter = factory.createSourceWriter(context, writer);

        sourceWriter.println("//AUTO GENERATED FILE BY DAO-GWT " + getClass().getName() + ". DO NOT EDIT!\n");

        sourceWriter.println(
                "private static HashMap<Class<?>,Entity<?>> ENTITIES = new HashMap<Class<?>,Entity<?>>();");
        sourceWriter.println("private static HashMap<Class<?>,Bean<?>> BEANS = new HashMap<Class<?>,Bean<?>>();");

        sourceWriter.println("static {");
        sourceWriter.indent();
        for (BeanMetadata metaData : metadatas) {
            String variable = "entity";
            String plural = "ENTITIES";
            if (!metaData.entity) {
                variable = "bean";
                plural = "BEANS";
            }

            sourceWriter.println("{ //%s with its implementation", metaData.name);
            sourceWriter.indent();
            sourceWriter.println("%s %s = new %s();", metaData.name, variable, metaData.name);

            sourceWriter.println("%s.put(%s.class,%s);", plural, metaData.type.getName(), variable);
            if (metaData.implementation != null) {
                factory.addImport(metaData.implementation.packageName);
                sourceWriter.println("%s.put(%s.class,%s);", plural, metaData.implementation.className, variable);
            }
            sourceWriter.outdent();
            sourceWriter.println("}");

        }
        sourceWriter.outdent();
        sourceWriter.println("}");
        sourceWriter.println();
        sourceWriter.println("public %s(){", className);
        sourceWriter.indent();
        sourceWriter.println("super(ENTITIES,BEANS);");
        sourceWriter.outdent();
        sourceWriter.println("}");
        sourceWriter.outdent();
        sourceWriter.println("}");
        context.commit(logger, writer);

        return factory.getCreatedClassName();

    }

    private BeanMetadata createEntity(GeneratorContext context, TreeLogger logger, String packageName,
            JClassType type, IsEntity annotation) throws TypeOracleException {
        return create(context, logger, packageName, type, AdapterEntity.class, annotation);
    }

    private BeanMetadata createBean(GeneratorContext context, TreeLogger logger, String packageName,
            JClassType type, IsBean annotation) throws TypeOracleException {
        return create(context, logger, packageName, type, AdapterBean.class, null);
    }

    private boolean isInstantiable(JClassType type, TreeLogger logger) {
        JConstructor[] constructors = type.getConstructors();
        if (constructors == null || constructors.length == 0)
            return true;
        JConstructor constructor = type.findConstructor(new JType[0]);
        if (constructor == null)
            return false;
        if (constructor.isPublic())
            return true;

        return false;
    }

    private BeanMetadata create(GeneratorContext context, TreeLogger logger, String packageName, JClassType type,
            Class<?> classAdapter, IsEntity anno) throws TypeOracleException {
        String beanName = anno == null || anno.aliasName().isEmpty() ? type.getName() : anno.aliasName();
        Source implementation = null;
        JClassType implType = type;
        TypeOracle typeOracle = context.getTypeOracle();
        if (type.isInterface() != null) {
            implType = null;
            JClassType[] types = type.getSubtypes();
            log(logger, Type.DEBUG, "Get all sub types of %s : %s", type, Arrays.toString(types));
            if (types != null && types.length > 0) {
                for (JClassType jClassType : types) {
                    if (isInstantiable(jClassType, logger)) {
                        implType = jClassType;
                        implementation = new Source(implType.getPackage().getName(), implType.getName());
                        break;
                    }

                }

            }
            if (implType == null) {
                log(logger, Type.ERROR, "The type %s has not valid subtypes " + "%s !", type,
                        Arrays.toString(types));
                return null;
            }
        }
        if (!implType.isDefaultInstantiable())
            return null;
        String prefix = classAdapter.getSimpleName().replace("Adapter", "");
        boolean isEntity = anno != null;
        String className = prefix + beanName;
        if (parseOnlyInterface && implType != type)
            className += "Light";
        PrintWriter writer = context.tryCreate(logger, packageName, className);
        if (writer == null) {
            return new BeanMetadata(type, className, implementation, isEntity);
        }

        ClassSourceFileComposerFactory factory = new ClassSourceFileComposerFactory(packageName, className);
        logger.log(Type.DEBUG, "Create Entity " + factory.getCreatedClassName());

        factory.setSuperclass(classAdapter.getSimpleName() + "<" + type.getName() + ">");
        factory.addImport(RuntimeException.class.getName());
        factory.addImport(classAdapter.getName());
        factory.addImport(type.getQualifiedSourceName());
        if (isEntity) {
            factory.addImport(ArrayList.class.getName());
            factory.addImport(Collection.class.getName());
        }
        factory.addImport(HashMap.class.getName());
        factory.addImport(Property.class.getName());
        factory.addImport(Property.class.getName() + ".Kind");
        factory.addImport(Index.class.getName());
        factory.addImport(implType.getQualifiedSourceName());

        factory.addImport("javax.annotation.Generated");
        factory.addAnnotationDeclaration("@Generated(" + "value=\"" + AdapterEntity.class.getName() + "\", "
                + "date=\"" + new Date() + "\", " + "comments=\"Generated by DAO-GWT project.\")");

        SourceWriter sourceWriter = factory.createSourceWriter(context, writer);

        sourceWriter.println("//AUTO GENERATED FILE BY DAO-GWT " + getClass().getName() + ". DO NOT EDIT!\n");

        sourceWriter.println("private static HashMap<String,Property<%s,?>> PROPERTIES = "
                + "new HashMap<String,Property<%s,?>>();", type.getName(), type.getName());
        if (isEntity) {
            factory.addImport(ArrayList.class.getName());
            factory.addImport(Index.class.getName());
            sourceWriter.println("private static Collection<Index> INDEXES = " + "new ArrayList<Index>();");

        }
        sourceWriter.println("static {");
        sourceWriter.indent();
        JClassType interfaz = type != implType ? type : null;
        JMethod[] methods = parseOnlyInterface ? type.getInheritableMethods() : implType.getInheritableMethods();
        for (JMethod method : methods) {
            String name = method.getName();
            //Check if the method has a IsIgnored annotation before to continue
            IsIgnored ignored = method.getAnnotation(IsIgnored.class);
            if (ignored != null) {

                log(logger, Type.DEBUG, EXPLICITELY_IGNORED, name, implType);
                continue;
            }
            boolean startsWithGet = name.startsWith("get");
            boolean startsWithIs = name.startsWith("is");
            if (!startsWithGet && !startsWithIs) {
                log(logger, Type.DEBUG, IGNORE_METHOD, name, implType);
                continue;
            }

            //check no parameters
            if (method.getParameterTypes().length != 0) {
                log(logger, Type.WARN, NO_PARAMETER_GETTER, name, implType);
                continue;
            }
            //check return type
            JType returnType = method.getReturnType();
            if (returnType == null || returnType.getQualifiedSourceName().equals(Void.class.getName())
                    || returnType.getQualifiedSourceName().equals(void.class.getName())) {
                log(logger, Type.DEBUG, VOID_GETTER, name + "" + returnType, implType);
                continue;
            }
            //change the format of the name getXyy ==> xyy
            String getterSetter = name;
            if (startsWithGet)
                getterSetter = name.substring(3);
            else if (startsWithIs)
                getterSetter = name.substring(2);
            name = getterSetter.substring(0, 1).toLowerCase() + getterSetter.substring(1);
            // check if the getter has an annotation
            IsIndexable indexable = method.getAnnotation(IsIndexable.class);
            boolean isIndexable = indexable != null;
            if (isIndexable && !isEntity)
                log(logger, Type.WARN, ONLY_ENTITY_FOR_INDEX, name, implType, IsEntity.class);

            isIndexable = isIndexable && isEntity;//only entity can defined indexable element

            String indexName = isIndexable ? indexable.aliasName() : "";
            String[] compositeIndexes = isIndexable ? indexable.compoundWith() : new String[0];
            Kind kind = null;
            JType typeOfCollection = null;
            String typeOfCollectionString = "null";

            if (!isPrimitive(returnType)) {

                //load complex properties except Key
                if (returnType.isEnum() != null) {
                    kind = Kind.ENUM;
                } else {
                    boolean isPrimitive = false;
                    boolean isEnum = false;
                    JParameterizedType pType = returnType.isParameterized();
                    JType collection = typeOracle.parse(Collection.class.getName());

                    if (pType != null && pType.getRawType().isAssignableTo(collection.isClassOrInterface())) {
                        JClassType[] types = pType.getTypeArgs();
                        kind = Kind.COLLECTION_OF_PRIMITIVES;
                        if (types.length > 1) {
                            log(logger, Type.DEBUG, CANNOT_PROCESS_PARAMETERIZED_TYPE, returnType, implType);
                            continue;
                        }
                        typeOfCollection = types[0];
                        typeOfCollectionString = typeOfCollection.getQualifiedSourceName() + ".class";
                        log(logger, Type.DEBUG, "The type of the collection is %s", typeOfCollectionString);
                        isPrimitive = isPrimitive(typeOfCollection);
                        isEnum = typeOfCollection.isEnum() != null;
                    }
                    if (!isPrimitive) {

                        if (isEnum && kind != null) {
                            kind = Kind.COLLECTION_OF_ENUMS;
                        } else {
                            JClassType classType = typeOfCollection != null ? typeOfCollection.isClassOrInterface()
                                    : returnType.isClassOrInterface();
                            boolean isBean = isBean(classType);
                            if (isBean) {
                                log(logger, Type.DEBUG, "The property %s is well a type %s", name, classType);
                                if (kind == null)
                                    kind = Kind.BEAN;
                                else
                                    kind = Kind.COLLECTION_OF_BEANS;
                            } else {
                                log(logger, Type.DEBUG, "The property %s has not a bean type %s", name, classType);
                                continue;
                            }
                        }

                    }
                }

            }
            assert kind != null;

            boolean isMemo = method.getAnnotation(IsMemo.class) != null;
            String oldName = "null";
            OldName oldNameAnno = method.getAnnotation(OldName.class);
            if (oldNameAnno != null)
                oldName = "\"" + oldNameAnno.value() + "\"";
            //create a property
            if (kind == Kind.BEAN || kind == Kind.COLLECTION_OF_BEANS)
                factory.addImport(returnType.getQualifiedSourceName());
            String valueType = "";
            JClassType classType = returnType.isClassOrInterface();
            JPrimitiveType primitiveType = returnType.isPrimitive();
            if (classType != null)
                valueType = classType.getQualifiedSourceName();
            else if (primitiveType != null) {
                valueType = primitiveType.getQualifiedBoxedSourceName();
            }

            sourceWriter.println("{ //Property %s", name);
            sourceWriter.indent();
            sourceWriter.print("Index index =");
            if (isIndexable) {
                if (indexName.isEmpty())
                    indexName = name;
                sourceWriter.println("new Index(\"%s\",\"%s\",new String[]{%s});", indexName, name,
                        String.join(",", compositeIndexes));
            } else
                sourceWriter.println("null;");
            boolean useKeyAsString = anno != null ? (name.equals(anno.keyName()) ? anno.useKeyAsString() : false)
                    : false;

            KeyOf keyOf = method.getAnnotation(KeyOf.class);
            if (keyOf != null) {
                IsEntity isEntity2 = keyOf.entity().getAnnotation(IsEntity.class);
                if (isEntity2 == null) {
                    log(logger, Type.ERROR, AdapterEntityManager.KEY_OF_NO_ENTITY, method, keyOf, keyOf.entity(),
                            IsEntity.class);
                    continue;
                }
                useKeyAsString = isEntity2.useKeyAsString();
            }
            boolean isHidden = isHidden(method, interfaz);
            sourceWriter.println(
                    "Property<%s,%s> property = new Property<%s,%s>(\"%s\",%s,%s.class,%s,%s,%s,%s,index,%s){",
                    type.getName(), valueType, type.getName(), valueType, name, oldName,
                    returnType.getQualifiedSourceName(), typeOfCollectionString,
                    kind != null ? "Kind." + kind.name() : "null", useKeyAsString + "", isMemo + "", isHidden + "");
            sourceWriter.indent();
            sourceWriter.println("@Override");
            sourceWriter.println("public %s get(%s instance){", valueType, type.getName());
            sourceWriter.indent();

            sourceWriter.println("return ((%s)instance).%s();", implType.getName(),
                    startsWithGet ? "get" + getterSetter : "is" + getterSetter);
            sourceWriter.outdent();
            sourceWriter.println("}");

            sourceWriter.println("@Override");
            sourceWriter.println("public void set(%s instance, %s value){", type.getName(), valueType);
            sourceWriter.indent();

            if (getSetter(implType, getterSetter, returnType) != null)
                sourceWriter.println("((%s)instance).%s(value);", implType.getName(), "set" + getterSetter);
            else {
                logger.log(Type.WARN, " Not found setter for " + getterSetter);
                sourceWriter.println("throw new RuntimeException(\"No such setter " + getterSetter + " \");");
            }

            sourceWriter.outdent();
            sourceWriter.println("}");
            sourceWriter.outdent();
            sourceWriter.println("};");
            sourceWriter.println("PROPERTIES.put(\"%s\",property);", name);
            if (!oldName.equals("null")) {
                sourceWriter.println("PROPERTIES.put(%s,property);", oldName);
            }
            if (isIndexable)
                sourceWriter.println("INDEXES.add(index);");
            sourceWriter.outdent();
            sourceWriter.println("}");

            log(logger, Type.DEBUG, SUCCESSFUL_ADD_PROPERTY, name + ":" + valueType, implType);

        }
        sourceWriter.outdent();
        sourceWriter.println("}");

        sourceWriter.println();
        sourceWriter.println("public %s(){", className);
        sourceWriter.indent();

        /*
         * boolean asyncReady,
           boolean autoGeneratedFlag,
           String keyName,
           boolean useKeyAsString,
           Class<T> type,Class<? extends T> implType,
           Map<String, Property<T,?>> mapAllProperties, Collection<Index> indexes) {
        super(type,implType,mapAllProperties);
         */
        if (isEntity)
            sourceWriter
                    .println(String.format("super(\"%s\",%s,%s,\"%s\",%s,%s.class,%s.class,PROPERTIES,INDEXES);",
                            anno.aliasName().isEmpty() ? type.getName() : anno.aliasName(), anno.asyncReady(),
                            anno.autoGeneratedKey(), anno.keyName(), anno.useKeyAsString(), type.getName(),
                            implType.getName()));
        else {
            sourceWriter.println(
                    String.format("super(%s.class,%s.class,PROPERTIES);", type.getName(), implType.getName()));

        }
        sourceWriter.outdent();
        sourceWriter.println("}");

        sourceWriter.println();
        sourceWriter.println("@Override");
        sourceWriter.println("public %s newInstance(){", type.getName());
        sourceWriter.indent();
        sourceWriter.println("return new %s();", implType.getName());
        sourceWriter.outdent();
        sourceWriter.println("}");

        sourceWriter.outdent();
        sourceWriter.println("}");
        context.commit(logger, writer);

        return new BeanMetadata(type, className, implementation, isEntity);
    }

    private JMethod getSetter(JClassType type, String getterSetter, JType argType) {
        if (type == null)
            return null;
        JMethod method = null;
        try {
            method = type.getMethod("set" + getterSetter, new JType[] { argType });
        } catch (Exception e) {
            JClassType superType = type.getSuperclass();
            return getSetter(superType, getterSetter, argType);

        }
        return method;
    }

    private boolean isHidden(JMethod method, JClassType interfaz) {
        if (interfaz == null)
            return false;
        return interfaz.findMethod(method.getName(), method.getParameterTypes()) != null;
    }

    private boolean isBean(JClassType returnType) {
        if (returnType == null)
            return false;
        IsBean anno = returnType.getAnnotation(IsBean.class);
        if (anno != null)
            return true;
        JClassType[] types = returnType.getImplementedInterfaces();
        if (types == null)
            return false;
        for (JClassType type : types) {
            if (isBean(type))
                return true;
        }
        return false;
    }

    private void log(TreeLogger logger, Type type, String message, Object... objects) {
        logger.log(type, String.format(message, objects));
    }

    private boolean isPrimitive(JType type) {
        if (type == null)
            return false;
        JPrimitiveType primitiveType = type.isPrimitive();
        if (primitiveType != null)
            return true;
        String sourceName = type.getQualifiedSourceName();
        return sourceName.equals(String.class.getName()) || sourceName.equals(Long.class.getName())
                || sourceName.equals(Integer.class.getName()) || sourceName.equals(Double.class.getName())
                || sourceName.equals(Float.class.getName());
    }

}