Example usage for org.apache.commons.lang ClassUtils getShortClassName

List of usage examples for org.apache.commons.lang ClassUtils getShortClassName

Introduction

In this page you can find the example usage for org.apache.commons.lang ClassUtils getShortClassName.

Prototype

public static String getShortClassName(String className) 

Source Link

Document

Gets the class name minus the package name from a String.

The string passed in is assumed to be a class name - it is not checked.

Usage

From source file:org.jbpm.bpel.xml.ActivityReader.java

private static String generateName(Activity activity, CompositeActivity parent) {
    String activityClass = ClassUtils.getShortClassName(activity.getClass());
    StringBuffer nameBuffer = new StringBuffer(activityClass);

    // separator/* www .j a  va  2s .  c  om*/
    nameBuffer.append('#');

    // index
    final int baseLength = nameBuffer.length();
    String name;

    for (int i = 1; parent.hasNode(name = nameBuffer.append(i).toString()); i++)
        nameBuffer.setLength(baseLength); // remove appended number

    return name;
}

From source file:org.kuali.rice.devtools.jpa.eclipselink.conv.parser.helper.AnnotationHelper.java

private void addAnnotation(final BodyDeclaration n, final String mappedClass, Level level) {
    for (AnnotationResolver resolver : resolvers) {
        if (resolver.getLevel() == level) {
            LOG.debug("Evaluating resolver " + ClassUtils.getShortClassName(resolver.getClass()) + " for "
                    + getTypeOrFieldNameForMsg(n) + ".");

            final String fullyQualifiedName = resolver.getFullyQualifiedName();

            //1 figure out if annotation is already imported either via star import or single import.
            final CompilationUnit unit = getCompilationUnit(n);
            final List<ImportDeclaration> imports = unit.getImports() != null ? unit.getImports()
                    : new ArrayList<ImportDeclaration>();
            final boolean foundAnnImport = imported(imports, fullyQualifiedName);

            //2 check if annotation already exists...
            final AnnotationExpr existingAnnotation = findAnnotation(n, fullyQualifiedName, foundAnnImport);

            //3 if removeExisting is set and the annotation exists, then remove the annotation prior to calling the resolver
            //Note: cannot remove the import without much more complex logic because the annotation may exist on other nodes in the CompilationUnit
            //Could traverse the entire CompilationUnit searching for the annotation if we wanted to determine whether we can safely remove an import
            if (removeExisting && existingAnnotation != null) {
                LOG.info("removing existing " + existingAnnotation + " from " + getTypeOrFieldNameForMsg(n)
                        + ".");
                final List<AnnotationExpr> annotations = n.getAnnotations() != null ? n.getAnnotations()
                        : new ArrayList<AnnotationExpr>();
                annotations.remove(existingAnnotation);
                n.setAnnotations(annotations);
            }/*from  ww  w . j  a  v  a 2 s.c  om*/

            //4 add annotation if it doesn't already exist or if replaceExisting is set
            // and the annotation resolves (meaning the resolver determines if should be added by returning a non-null value)
            if (existingAnnotation == null || (existingAnnotation != null && removeExisting)) {
                NodeData nodes = resolver.resolve(n, mappedClass);
                if (nodes != null && nodes.annotation != null) {
                    LOG.info("adding " + nodes.annotation + " to " + getTypeOrFieldNameForMsg(n) + ".");
                    final List<AnnotationExpr> annotations = n.getAnnotations() != null ? n.getAnnotations()
                            : new ArrayList<AnnotationExpr>();
                    annotations.add(nodes.annotation);
                    n.setAnnotations(annotations);

                    //5 add import for annotation
                    if (!foundAnnImport) {
                        LOG.info("adding import " + fullyQualifiedName + " to " + getTypeNameForMsg(n) + ".");
                        imports.add(nodes.annotationImport);
                    }

                    //6 add additional imports if they are needed
                    if (nodes.additionalImports != null) {
                        for (ImportDeclaration aImport : nodes.additionalImports) {
                            if (aImport.isStatic() || aImport.isAsterisk()) {
                                throw new IllegalStateException(
                                        "The additional imports should not be static or star imports");
                            }
                            final boolean imported = imported(imports, aImport.getName().toString());
                            if (!imported) {
                                LOG.info("adding import " + aImport.getName().toString() + " to "
                                        + getTypeNameForMsg(n) + ".");
                                imports.add(aImport);
                            }
                        }
                    }

                    unit.setImports(imports);

                    if (nodes.nestedDeclaration != null) {
                        final TypeDeclaration parent = unit.getTypes().get(0);

                        final List<BodyDeclaration> members = parent.getMembers() != null ? parent.getMembers()
                                : new ArrayList<BodyDeclaration>();
                        final TypeDeclaration existingNestedDeclaration = findTypeDeclaration(members,
                                nodes.nestedDeclaration.getName());

                        //7 if removeExisting is set and the nested declaration exists, then remove the nested declaration
                        if (removeExisting) {
                            if (existingNestedDeclaration != null) {
                                LOG.info("removing existing nested declaration "
                                        + existingNestedDeclaration.getName() + " from "
                                        + getTypeOrFieldNameForMsg(n) + ".");
                                members.remove(existingNestedDeclaration);
                            }
                        }

                        //8 add nested class
                        if (existingNestedDeclaration == null
                                || (existingNestedDeclaration != null && removeExisting)) {
                            nodes.nestedDeclaration.setParentNode(parent);
                            LOG.info("adding nested declaration " + nodes.nestedDeclaration.getName() + " to "
                                    + getTypeOrFieldNameForMsg(n) + ".");
                            members.add(nodes.nestedDeclaration);
                        }
                        parent.setMembers(members);
                    }
                }
            }
        }
    }
}

From source file:org.kuali.rice.devtools.jpa.eclipselink.conv.parser.helper.AnnotationHelper.java

private AnnotationExpr findAnnotation(final BodyDeclaration n, String fullyQualifiedName,
        boolean foundAnnImport) {
    final String simpleName = ClassUtils.getShortClassName(fullyQualifiedName);
    final List<AnnotationExpr> annotations = n.getAnnotations() != null ? n.getAnnotations()
            : new ArrayList<AnnotationExpr>();

    for (AnnotationExpr ae : annotations) {
        final String name = ae.getName().toString();
        if ((simpleName.equals(name) && foundAnnImport)) {
            LOG.info("found " + ae + " on " + getTypeOrFieldNameForMsg(n) + ".");
            return ae;
        }/*w w w.  j  a va 2s  .c  o  m*/

        if (fullyQualifiedName.equals(name)) {
            LOG.info("found " + ae + " on " + getTypeOrFieldNameForMsg(n) + ".");
            return ae;
        }
    }
    return null;
}

From source file:org.kuali.rice.devtools.jpa.eclipselink.conv.parser.helper.resolver.ConvertResolver.java

/** gets the annotation but also adds an import in the process if a Convert annotation is required. */
@Override//w  ww  .j av a  2  s .  c om
protected NodeData getAnnotationNodes(String enclosingClass, String fieldName, String mappedClass) {
    final FieldDescriptor fd = OjbUtil.findFieldDescriptor(mappedClass, fieldName, descriptorRepositories);

    if (fd != null) {
        final FieldConversion fc = fd.getFieldConversion();
        //in ojb all columns have at least the default field conversion
        if (fc != null && FieldConversionDefaultImpl.class != fc.getClass()) {
            LOG.info(enclosingClass + "." + fieldName + " for the mapped class " + mappedClass
                    + " field has a converter " + fc.getClass().getName());

            final String jpaConverter = getJpaConverterForOjbClass(fc.getClass().getName());
            if (jpaConverter == null) {
                LOG.error(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                        + " field has a converter " + fc.getClass().getName()
                        + " but a replacement converter was not configured, unable to set Convert class");
                return new NodeData(
                        new NormalAnnotationExpr(new NameExpr(SIMPLE_NAME),
                                Collections
                                        .singletonList(new MemberValuePair("converter", new NameExpr(null)))),
                        new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false,
                                false));
            } else if (StringUtils.isBlank(jpaConverter)) {
                LOG.info(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                        + " field has a converter " + fc.getClass().getName()
                        + " But no converter definition is needed due to default converter configuration.");
            } else {
                final String shortClassName = ClassUtils.getShortClassName(jpaConverter);
                final String packageName = ClassUtils.getPackageName(jpaConverter);
                return new NodeData(
                        new NormalAnnotationExpr(new NameExpr(SIMPLE_NAME),
                                Collections.singletonList(new MemberValuePair("converter",
                                        new NameExpr(shortClassName + ".class")))),
                        new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false,
                                false),
                        Collections.singletonList(new ImportDeclaration(
                                new QualifiedNameExpr(new NameExpr(packageName), shortClassName), false,
                                false)));
            }
        }
    }
    return null;
}

From source file:org.kuali.rice.devtools.jpa.eclipselink.conv.parser.helper.resolver.ManyToManyResolver.java

/** gets the annotation but also adds an import in the process if a Convert annotation is required. */
@Override//from   w  ww .j a v  a 2s  .  c o m
protected NodeData getAnnotationNodes(String enclosingClass, String fieldName, String mappedClass) {
    final CollectionDescriptor cld = OjbUtil.findCollectionDescriptor(mappedClass, fieldName,
            descriptorRepositories);
    if (cld != null) {
        final List<MemberValuePair> pairs = new ArrayList<MemberValuePair>();
        final Collection<ImportDeclaration> additionalImports = new ArrayList<ImportDeclaration>();

        if (!cld.isMtoNRelation()) {
            return null;
        }

        boolean fkError = false;
        final String[] fkToItemClass = getFksToItemClass(cld);
        if (fkToItemClass == null || fkToItemClass.length == 0) {
            LOG.error(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                    + " field has a collection descriptor for " + fieldName
                    + " for a M:N relationship but does not have any fk-pointing-to-element-class configured");
            fkError = true;
        }

        final String[] fkToThisClass = getFksToThisClass(cld);
        if (fkToThisClass == null || fkToThisClass.length == 0) {
            LOG.error(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                    + " field has a collection descriptor for " + fieldName
                    + " for a M:N relationship but does not have any fk-pointing-to-this-class configured");
            fkError = true;
        }

        if (fkError) {
            return null;
        }

        final Collection<String> fks = cld.getForeignKeyFields();
        if (fks != null || !fks.isEmpty()) {
            LOG.warn(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                    + " field has a collection descriptor for " + fieldName
                    + " for a M:N relationship but has the inverse-foreignkey configured as opposed to "
                    + "fk-pointing-to-this-class and fk-pointing-to-element-class");
        }

        final String itemClassName = cld.getItemClassName();
        if (StringUtils.isBlank(itemClassName)) {
            LOG.error(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                    + " field has a reference descriptor for " + fieldName
                    + " but does not class name attribute");
        } else {
            final String shortClassName = ClassUtils.getShortClassName(itemClassName);
            final String packageName = ClassUtils.getPackageName(itemClassName);
            pairs.add(new MemberValuePair("targetEntity", new NameExpr(shortClassName + ".class")));
            additionalImports.add(new ImportDeclaration(
                    new QualifiedNameExpr(new NameExpr(packageName), shortClassName), false, false));
        }

        final boolean proxy = cld.isLazy();
        if (proxy) {
            pairs.add(new MemberValuePair("fetch", new NameExpr("FetchType.LAZY")));
            additionalImports.add(new ImportDeclaration(
                    new QualifiedNameExpr(new NameExpr(PACKAGE), "FetchType"), false, false));
        }

        final boolean refresh = cld.isRefresh();
        if (refresh) {
            LOG.error(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                    + " field has refresh set to " + refresh
                    + ", unsupported conversion to @OneToOne attributes");
        }

        final List<Expression> cascadeTypes = new ArrayList<Expression>();
        final boolean autoRetrieve = cld.getCascadeRetrieve();
        if (autoRetrieve) {
            cascadeTypes.add(new NameExpr("CascadeType.REFRESH"));
        } else {
            // updated default logging - false would result no additional annotations
            if (LOG.isDebugEnabled()) {
                LOG.debug(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                        + " field has auto-retrieve set to " + autoRetrieve
                        + ", unsupported conversion to CascadeType");
            }
        }

        final int autoDelete = cld.getCascadingDelete();
        if (autoDelete == ObjectReferenceDescriptor.CASCADE_NONE) {
            // updated default logging - none would result no additional annotations
            if (LOG.isDebugEnabled()) {
                LOG.debug(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                        + " field has auto-delete set to none, unsupported conversion to CascadeType");
            }
        } else if (autoDelete == ObjectReferenceDescriptor.CASCADE_LINK) {
            LOG.warn(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                    + " field has auto-delete set to link, unsupported conversion to CascadeType");
        } else if (autoDelete == ObjectReferenceDescriptor.CASCADE_OBJECT) {
            cascadeTypes.add(new NameExpr("CascadeType.REMOVE"));
        } else {
            LOG.error(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                    + " field has auto-delete set to an invalid value");
        }

        final int autoUpdate = cld.getCascadingStore();
        if (autoUpdate == ObjectReferenceDescriptor.CASCADE_NONE) {
            // updated default logging - none would result no additional annotations
            if (LOG.isDebugEnabled()) {
                LOG.debug(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                        + " field has auto-update set to none, unsupported conversion to CascadeType");
            }
        } else if (autoUpdate == ObjectReferenceDescriptor.CASCADE_LINK) {
            LOG.warn(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                    + " field has auto-update set to link, unsupported conversion to CascadeType");
        } else if (autoUpdate == ObjectReferenceDescriptor.CASCADE_OBJECT) {
            cascadeTypes.add(new NameExpr("CascadeType.PERSIST"));
        } else {
            LOG.error(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                    + " field has auto-update set to an invalid value");
        }

        if (!cascadeTypes.isEmpty()) {
            pairs.add(new MemberValuePair("cascade", new ArrayInitializerExpr(cascadeTypes)));
            additionalImports.add(new ImportDeclaration(
                    new QualifiedNameExpr(new NameExpr(PACKAGE), "CascadeType"), false, false));
        }

        final NodeData nodeData;
        if (isBidirectional(mappedClass, itemClassName)) {
            LOG.info(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                    + " bi-directional ManyToMany relationship detected");

            BidirectionalOwnerRegistry registry = BidirectionalOwnerRegistry.getInstance();
            if (registry.isOwnerThisClassManyToMany(mappedClass, itemClassName)) {
                nodeData = new NodeData(new NormalAnnotationExpr(new NameExpr(SIMPLE_NAME), pairs),
                        new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false,
                                false),
                        additionalImports);
            } else if (registry.isOwnerItemClassManyToMany(mappedClass, itemClassName)) {
                nodeData = new NodeData(
                        new NormalAnnotationExpr(new NameExpr(SIMPLE_NAME),
                                Collections.singletonList(new MemberValuePair("mappedBy",
                                        new StringLiteralExpr(getMappedBy(mappedClass, itemClassName))))),
                        new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false,
                                false),
                        additionalImports);
            } else {
                registry.assignThisClassAsOwnerManyToMany(mappedClass, itemClassName);

                nodeData = new NodeData(new NormalAnnotationExpr(new NameExpr(SIMPLE_NAME), pairs),
                        new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false,
                                false),
                        additionalImports);
            }
        } else {
            nodeData = new NodeData(new NormalAnnotationExpr(new NameExpr(SIMPLE_NAME), pairs),
                    new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false,
                            false),
                    additionalImports);
        }

        return nodeData;
    }
    return null;
}

From source file:org.kuali.rice.devtools.jpa.eclipselink.conv.parser.helper.resolver.ManyToOneResolver.java

/** gets the annotation but also adds an import in the process if a Convert annotation is required. */
@Override//from   www .  j  av a  2  s  .  c  o  m
protected NodeData getAnnotationNodes(String enclosingClass, String fieldName, String mappedClass) {
    final ObjectReferenceDescriptor ord = OjbUtil.findObjectReferenceDescriptor(mappedClass, fieldName,
            descriptorRepositories);
    if (ord != null) {
        final List<MemberValuePair> pairs = new ArrayList<MemberValuePair>();
        final Collection<ImportDeclaration> additionalImports = new ArrayList<ImportDeclaration>();

        final Collection<String> fks = ord.getForeignKeyFields();
        if (fks == null || fks.isEmpty()) {
            LOG.error(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                    + " field has a reference descriptor for " + fieldName
                    + " but does not have any foreign keys configured");
            return null;
        }

        final Collection<String> pks = OjbUtil.getPrimaryKeyNames(mappedClass, descriptorRepositories);

        if (!(pks.size() == fks.size() && pks.containsAll(fks))) {
            final String className = ord.getItemClassName();
            if (StringUtils.isBlank(className)) {
                LOG.error(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                        + " field has a reference descriptor for " + fieldName
                        + " but does not class name attribute");
            } else {
                final String shortClassName = ClassUtils.getShortClassName(className);
                final String packageName = ClassUtils.getPackageName(className);
                pairs.add(new MemberValuePair("targetEntity", new NameExpr(shortClassName + ".class")));
                additionalImports.add(new ImportDeclaration(
                        new QualifiedNameExpr(new NameExpr(packageName), shortClassName), false, false));
            }

            final boolean proxy = ord.isLazy();
            if (proxy) {
                pairs.add(new MemberValuePair("fetch", new NameExpr("FetchType.LAZY")));
                additionalImports.add(new ImportDeclaration(
                        new QualifiedNameExpr(new NameExpr(PACKAGE), "FetchType"), false, false));
            }

            final boolean refresh = ord.isRefresh();
            if (refresh) {
                LOG.error(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                        + " field has refresh set to " + refresh
                        + ", unsupported conversion to @OneToOne attributes");
            }

            final List<Expression> cascadeTypes = new ArrayList<Expression>();
            final boolean autoRetrieve = ord.getCascadeRetrieve();
            if (autoRetrieve) {
                cascadeTypes.add(new NameExpr("CascadeType.REFRESH"));
            } else {
                // updated default logging - false would result no additional annotations
                if (LOG.isDebugEnabled()) {
                    LOG.debug(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                            + " field has auto-retrieve set to " + autoRetrieve
                            + ", unsupported conversion to CascadeType");
                }
            }

            final int autoDelete = ord.getCascadingDelete();
            if (autoDelete == ObjectReferenceDescriptor.CASCADE_NONE) {
                // updated default logging - none would result no additional annotations
                if (LOG.isDebugEnabled()) {
                    LOG.debug(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                            + " field has auto-delete set to none, unsupported conversion to CascadeType");
                }
            } else if (autoDelete == ObjectReferenceDescriptor.CASCADE_LINK) {
                LOG.warn(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                        + " field has auto-delete set to link, unsupported conversion to CascadeType");
            } else if (autoDelete == ObjectReferenceDescriptor.CASCADE_OBJECT) {
                cascadeTypes.add(new NameExpr("CascadeType.REMOVE"));
            } else {
                LOG.error(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                        + " field has auto-delete set to an invalid value");
            }

            final int autoUpdate = ord.getCascadingStore();
            if (autoUpdate == ObjectReferenceDescriptor.CASCADE_NONE) {
                // updated default logging - none would result no additional annotations
                if (LOG.isDebugEnabled()) {
                    LOG.debug(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                            + " field has auto-update set to none, unsupported conversion to CascadeType");
                }
            } else if (autoUpdate == ObjectReferenceDescriptor.CASCADE_LINK) {
                LOG.warn(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                        + " field has auto-update set to link, unsupported conversion to CascadeType");
            } else if (autoUpdate == ObjectReferenceDescriptor.CASCADE_OBJECT) {
                cascadeTypes.add(new NameExpr("CascadeType.PERSIST"));
            } else {
                LOG.error(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                        + " field has auto-update set to an invalid value");
            }

            if (!cascadeTypes.isEmpty()) {
                pairs.add(new MemberValuePair("cascade", new ArrayInitializerExpr(cascadeTypes)));
                additionalImports.add(new ImportDeclaration(
                        new QualifiedNameExpr(new NameExpr(PACKAGE), "CascadeType"), false, false));
            }

            return new NodeData(new NormalAnnotationExpr(new NameExpr(SIMPLE_NAME), pairs),
                    new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false,
                            false),
                    additionalImports);
        }
    }
    return null;
}

From source file:org.kuali.rice.devtools.jpa.eclipselink.conv.parser.helper.resolver.OneToManyResolver.java

/** gets the annotation but also adds an import in the process if a Convert annotation is required. */
@Override/*from  w ww  . j  a  va2 s .c o m*/
protected NodeData getAnnotationNodes(String enclosingClass, String fieldName, String mappedClass) {
    final CollectionDescriptor cld = OjbUtil.findCollectionDescriptor(mappedClass, fieldName,
            descriptorRepositories);
    if (cld != null) {
        final List<MemberValuePair> pairs = new ArrayList<MemberValuePair>();
        final Collection<ImportDeclaration> additionalImports = new ArrayList<ImportDeclaration>();

        if (cld.isMtoNRelation()) {
            return null;
        }
        /*  I don't think this is correct.
        final String[] fkToItemClass = getFksToItemClass(cld);
        if (fkToItemClass != null || fkToItemClass.length != 0) {
        LOG.warn(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass) + " field has a collection descriptor for " + fieldName
                + " for a 1:M relationship but has fk-pointing-to-element-class configured");
        }
                
        final String[] fkToThisClass = getFksToThisClass(cld);
        if (fkToThisClass != null || fkToThisClass.length != 0) {
        LOG.warn(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass) + " field has a collection descriptor for " + fieldName
                + " for a 1:M relationship but has fk-pointing-to-this-class configured");
        }
        */
        final Collection<String> fks = cld.getForeignKeyFields();
        if (fks == null || fks.isEmpty()) {
            LOG.error(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                    + " field has a collection descriptor for " + fieldName
                    + " but does not have any foreign keys configured");
            return null;
        }

        final String itemClassName = cld.getItemClassName();
        if (StringUtils.isBlank(itemClassName)) {
            LOG.error(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                    + " field has a reference descriptor for " + fieldName
                    + " but does not class name attribute");
        } else {
            final String shortClassName = ClassUtils.getShortClassName(itemClassName);
            final String packageName = ClassUtils.getPackageName(itemClassName);
            pairs.add(new MemberValuePair("targetEntity", new NameExpr(shortClassName + ".class")));
            additionalImports.add(new ImportDeclaration(
                    new QualifiedNameExpr(new NameExpr(packageName), shortClassName), false, false));
        }

        final boolean proxy = cld.isLazy();
        if (proxy) {
            pairs.add(new MemberValuePair("fetch", new NameExpr("FetchType.LAZY")));
            additionalImports.add(new ImportDeclaration(
                    new QualifiedNameExpr(new NameExpr(PACKAGE), "FetchType"), false, false));
        }

        final boolean refresh = cld.isRefresh();
        if (refresh) {
            LOG.error(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                    + " field has refresh set to " + refresh
                    + ", unsupported conversion to @OneToOne attributes");
        }

        final List<Expression> cascadeTypes = new ArrayList<Expression>();
        final boolean autoRetrieve = cld.getCascadeRetrieve();
        if (autoRetrieve) {
            cascadeTypes.add(new NameExpr("CascadeType.REFRESH"));
        } else {
            LOG.warn(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                    + " field has auto-retrieve set to " + autoRetrieve
                    + ", unsupported conversion to CascadeType");
        }

        final int autoDelete = cld.getCascadingDelete();
        if (autoDelete == ObjectReferenceDescriptor.CASCADE_NONE) {
            LOG.warn(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                    + " field has auto-delete set to none, unsupported conversion to CascadeType");
        } else if (autoDelete == ObjectReferenceDescriptor.CASCADE_LINK) {
            LOG.warn(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                    + " field has auto-delete set to link, unsupported conversion to CascadeType");
        } else if (autoDelete == ObjectReferenceDescriptor.CASCADE_OBJECT) {
            cascadeTypes.add(new NameExpr("CascadeType.REMOVE"));
            pairs.add(new MemberValuePair("orphanRemoval", new BooleanLiteralExpr(true)));
        } else {
            LOG.error(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                    + " field has auto-delete set to an invalid value");
        }

        final int autoUpdate = cld.getCascadingStore();
        if (autoUpdate == ObjectReferenceDescriptor.CASCADE_NONE) {
            LOG.warn(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                    + " field has auto-update set to none, unsupported conversion to CascadeType");
        } else if (autoUpdate == ObjectReferenceDescriptor.CASCADE_LINK) {
            LOG.warn(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                    + " field has auto-update set to link, unsupported conversion to CascadeType");
        } else if (autoUpdate == ObjectReferenceDescriptor.CASCADE_OBJECT) {
            cascadeTypes.add(new NameExpr("CascadeType.PERSIST"));
        } else {
            LOG.error(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                    + " field has auto-update set to an invalid value");
        }

        if (!cascadeTypes.isEmpty()) {
            pairs.add(new MemberValuePair("cascade", new ArrayInitializerExpr(cascadeTypes)));
            additionalImports.add(new ImportDeclaration(
                    new QualifiedNameExpr(new NameExpr(PACKAGE), "CascadeType"), false, false));
        }

        final NodeData nodeData;
        if (isBidirectional(mappedClass, itemClassName)) {
            LOG.info(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                    + " bi-directional OneToMany relationship detected");

            BidirectionalOwnerRegistry registry = BidirectionalOwnerRegistry.getInstance();
            if (registry.isOwnerItemClassManyToOne(mappedClass, itemClassName)) {
                nodeData = new NodeData(
                        new NormalAnnotationExpr(new NameExpr(SIMPLE_NAME),
                                Collections.singletonList(new MemberValuePair("mappedBy",
                                        new StringLiteralExpr(getMappedBy(mappedClass, itemClassName))))),
                        new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false,
                                false),
                        additionalImports);
            } else {
                registry.assignItemClassAsOwnerManyToOne(mappedClass, itemClassName);

                nodeData = new NodeData(
                        new NormalAnnotationExpr(new NameExpr(SIMPLE_NAME),
                                Collections.singletonList(new MemberValuePair("mappedBy",
                                        new StringLiteralExpr(getMappedBy(mappedClass, itemClassName))))),
                        new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false,
                                false),
                        additionalImports);
            }
        } else {
            nodeData = new NodeData(new NormalAnnotationExpr(new NameExpr(SIMPLE_NAME), pairs),
                    new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false,
                            false),
                    additionalImports);
        }

        return nodeData;
    }
    return null;
}

From source file:org.kuali.rice.devtools.jpa.eclipselink.conv.parser.helper.resolver.OneToOneResolver.java

/** gets the annotation but also adds an import in the process if a Convert annotation is required. */
@Override//  ww w.jav  a2  s .  c o  m
protected NodeData getAnnotationNodes(String enclosingClass, String fieldName, String mappedClass) {
    final ObjectReferenceDescriptor ord = OjbUtil.findObjectReferenceDescriptor(mappedClass, fieldName,
            descriptorRepositories);
    if (ord != null) {
        final List<MemberValuePair> pairs = new ArrayList<MemberValuePair>();
        final Collection<ImportDeclaration> additionalImports = new ArrayList<ImportDeclaration>();

        final Collection<String> fks = ord.getForeignKeyFields();
        if (fks == null || fks.isEmpty()) {
            LOG.error(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                    + " field has a reference descriptor for " + fieldName
                    + " but does not have any foreign keys configured");
            return null;
        }

        final Collection<String> pks = OjbUtil.getPrimaryKeyNames(mappedClass, descriptorRepositories);

        if (pks.size() == fks.size() && pks.containsAll(fks)) {
            final String itemClassName = ord.getItemClassName();
            if (StringUtils.isBlank(itemClassName)) {
                LOG.error(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                        + " field has a reference descriptor for " + fieldName
                        + " but does not class name attribute");
            } else {
                final String shortClassName = ClassUtils.getShortClassName(itemClassName);
                final String packageName = ClassUtils.getPackageName(itemClassName);
                pairs.add(new MemberValuePair("targetEntity", new NameExpr(shortClassName + ".class")));
                additionalImports.add(new ImportDeclaration(
                        new QualifiedNameExpr(new NameExpr(packageName), shortClassName), false, false));
            }

            final boolean proxy = ord.isLazy();
            if (proxy) {
                pairs.add(new MemberValuePair("fetch", new NameExpr("FetchType.LAZY")));
                additionalImports.add(new ImportDeclaration(
                        new QualifiedNameExpr(new NameExpr(PACKAGE), "FetchType"), false, false));
            }

            final boolean refresh = ord.isRefresh();
            if (refresh) {
                LOG.error(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                        + " field has refresh set to " + refresh
                        + ", unsupported conversion to @OneToOne attributes");
            }

            final List<Expression> cascadeTypes = new ArrayList<Expression>();
            final boolean autoRetrieve = ord.getCascadeRetrieve();
            if (autoRetrieve) {
                cascadeTypes.add(new NameExpr("CascadeType.REFRESH"));
            } else {
                LOG.warn(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                        + " field has auto-retrieve set to " + autoRetrieve
                        + ", unsupported conversion to CascadeType");
            }

            final int autoDelete = ord.getCascadingDelete();
            if (autoDelete == ObjectReferenceDescriptor.CASCADE_NONE) {
                LOG.warn(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                        + " field has auto-delete set to none, unsupported conversion to CascadeType");
            } else if (autoDelete == ObjectReferenceDescriptor.CASCADE_LINK) {
                LOG.warn(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                        + " field has auto-delete set to link, unsupported conversion to CascadeType");
            } else if (autoDelete == ObjectReferenceDescriptor.CASCADE_OBJECT) {
                cascadeTypes.add(new NameExpr("CascadeType.REMOVE"));
                pairs.add(new MemberValuePair("orphanRemoval", new BooleanLiteralExpr(true)));
            } else {
                LOG.error(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                        + " field has auto-delete set to an invalid value");
            }

            final int autoUpdate = ord.getCascadingStore();
            if (autoUpdate == ObjectReferenceDescriptor.CASCADE_NONE) {
                LOG.warn(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                        + " field has auto-update set to none, unsupported conversion to CascadeType");
            } else if (autoUpdate == ObjectReferenceDescriptor.CASCADE_LINK) {
                LOG.warn(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                        + " field has auto-update set to link, unsupported conversion to CascadeType");
            } else if (autoUpdate == ObjectReferenceDescriptor.CASCADE_OBJECT) {
                cascadeTypes.add(new NameExpr("CascadeType.PERSIST"));
            } else {
                LOG.error(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                        + " field has auto-update set to an invalid value");
            }

            if (!cascadeTypes.isEmpty()) {
                pairs.add(new MemberValuePair("cascade", new ArrayInitializerExpr(cascadeTypes)));
                additionalImports.add(new ImportDeclaration(
                        new QualifiedNameExpr(new NameExpr(PACKAGE), "CascadeType"), false, false));
            }
            final NodeData nodeData;
            if (isBidirectional(mappedClass, itemClassName)) {
                LOG.info(ResolverUtil.logMsgForField(enclosingClass, fieldName, mappedClass)
                        + " bi-directional OneToOne relationship detected");

                final String mappedBy = getMappedBy(mappedClass, itemClassName);
                final Comment fixme = new BlockComment("\nFIXME: JPA_CONVERSION \n"
                        + "For one-to-one bidirectional relationships, the owning side corresponds to the side that contains the corresponding foreign key.\n"
                        + "Even in the absence of a foreign key, one side must be the owning side.\n"
                        + "If this is not the owning side, the required annotation should be:\n@OneToOne(mappedBy=\""
                        + mappedBy + "\")\n");
                final NormalAnnotationExpr annotation = new NormalAnnotationExpr(new NameExpr(SIMPLE_NAME),
                        pairs);
                annotation.setComment(fixme);
                nodeData = new NodeData(annotation,
                        new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false,
                                false),
                        additionalImports);

            } else {
                nodeData = new NodeData(new NormalAnnotationExpr(new NameExpr(SIMPLE_NAME), pairs),
                        new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false,
                                false),
                        additionalImports);
            }
            return nodeData;
        }
    }
    return null;
}

From source file:org.novelang.build.unicode.UnicodeNamesGenerator.java

public static void main(final String[] args) throws IOException {
    final File targetDirectory;
    if (args.length == 0) {
        final File projectDirectory = SystemUtils.USER_DIR.endsWith("idea")
                ? new File(SystemUtils.USER_DIR).getParentFile()
                : new File(SystemUtils.USER_DIR);
        targetDirectory = new File(projectDirectory, "idea/generated/antlr");
    } else if (args.length == 1) {
        targetDirectory = new File(args[0]);
    } else {//from  w  w  w .  j av  a2s .  c o  m
        throw new IllegalArgumentException(
                "Usage: " + ClassUtils.getShortClassName(UnicodeNamesGenerator.class) + "[target-directory]");
    }
    new UnicodeNamesGenerator("org.novelang.parser.unicode", "names.bin", targetDirectory).generate();

}

From source file:org.novelang.common.SimpleTree.java

@Override
public String toString() {
    return ClassUtils.getShortClassName(getClass()) + "[" + text + "]";
}