Permalink
Browse files

moving Hibernate from Grails core to here

  • Loading branch information...
1 parent 8a62938 commit 8abd5d94586815848c6cfdc7a706ad9293554ca6 @burtbeckwith burtbeckwith committed Mar 15, 2013
Showing with 19,788 additions and 0 deletions.
  1. +1,986 −0 grails-datastore-gorm-hibernate/src/main/groovy/grails/orm/HibernateCriteriaBuilder.java
  2. +201 −0 grails-datastore-gorm-hibernate/src/main/groovy/grails/orm/PagedResultList.java
  3. +79 −0 grails-datastore-gorm-hibernate/src/main/groovy/grails/orm/RlikeExpression.java
  4. +112 −0 ...tore-gorm-hibernate/src/main/groovy/org/codehaus/groovy/grails/compiler/gorm/GormTransformer.java
  5. +109 −0 ...hibernate/src/main/groovy/org/codehaus/groovy/grails/compiler/gorm/GormValidationTransformer.java
  6. +367 −0 ...src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/ConfigurableLocalSessionFactoryBean.java
  7. +293 −0 ...ibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/EventTriggeringInterceptor.java
  8. +322 −0 ...ibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/GrailsHibernateDomainClass.java
  9. +263 −0 .../src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/GrailsHibernateDomainClassProperty.java
  10. +64 −0 ...m-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/GrailsHibernateTemplate.java
  11. +39 −0 ...src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/GrailsHibernateTransactionManager.groovy
  12. +85 −0 ...e-gorm-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/HibernateDatastore.java
  13. +30 −0 ...m-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/HibernateEventListeners.java
  14. +1,011 −0 ...m-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/HibernateGormEnhancer.groovy
  15. +292 −0 ...ore-gorm-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/HibernateSession.java
  16. +40 −0 ...gorm-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/SessionFactoryHolder.java
  17. +444 −0 ...-gorm-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/SessionFactoryProxy.java
  18. +31 −0 ...re-gorm-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/cfg/CacheConfig.groovy
  19. +39 −0 ...e-gorm-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/cfg/ColumnConfig.groovy
  20. +29 −0 ...m-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/cfg/CompositeIdentity.groovy
  21. +124 −0 ...rc/main/groovy/org/codehaus/groovy/grails/orm/hibernate/cfg/DefaultGrailsDomainConfiguration.java
  22. +70 −0 ...rc/main/groovy/org/codehaus/groovy/grails/orm/hibernate/cfg/GORMEnhancingBeanPostProcessor.groovy
  23. +31 −0 ...-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/cfg/GORMNamespaceHandler.java
  24. +257 −0 .../main/groovy/org/codehaus/groovy/grails/orm/hibernate/cfg/GORMSessionFactoryDefinitionParser.java
  25. +228 −0 ...e/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/cfg/GrailsAnnotationConfiguration.java
  26. +3,099 −0 ...rm-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/cfg/GrailsDomainBinder.java
  27. +49 −0 ...rnate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/cfg/GrailsDomainConfiguration.java
  28. +570 −0 ...m-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/cfg/GrailsHibernateUtil.java
  29. +594 −0 ...rnate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/cfg/HibernateMappingBuilder.groovy
  30. +298 −0 .../src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/cfg/HibernateNamedQueriesBuilder.groovy
  31. +292 −0 ...gorm-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/cfg/HibernateUtils.groovy
  32. +34 −0 ...store-gorm-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/cfg/Identity.groovy
  33. +222 −0 ...gorm-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/cfg/IdentityEnumType.java
  34. +50 −0 ...-gorm-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/cfg/InstanceProxy.groovy
  35. +14 −0 ...tore-gorm-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/cfg/JoinTable.groovy
  36. +151 −0 ...astore-gorm-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/cfg/Mapping.groovy
  37. +10 −0 ...tore-gorm-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/cfg/NaturalId.groovy
  38. +179 −0 ...gorm-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/cfg/PropertyConfig.groovy
  39. +31 −0 ...atastore-gorm-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/cfg/Table.groovy
  40. +57 −0 ...main/groovy/org/codehaus/groovy/grails/orm/hibernate/events/PatchedDefaultFlushEventListener.java
  41. +28 −0 ...te/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/events/SaveOrUpdateEventListener.java
  42. +42 −0 ...g/codehaus/groovy/grails/orm/hibernate/exceptions/CouldNotDetermineHibernateDialectException.java
  43. +42 −0 ...vy/org/codehaus/groovy/grails/orm/hibernate/exceptions/GrailsHibernateConfigurationException.java
  44. +44 −0 ...src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/exceptions/GrailsHibernateException.java
  45. +44 −0 ...ate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/exceptions/GrailsQueryException.java
  46. +535 −0 ...ovy/org/codehaus/groovy/grails/orm/hibernate/metaclass/AbstractClausedStaticPersistentMethod.java
  47. +111 −0 ...in/groovy/org/codehaus/groovy/grails/orm/hibernate/metaclass/AbstractDynamicPersistentMethod.java
  48. +156 −0 ...ain/groovy/org/codehaus/groovy/grails/orm/hibernate/metaclass/AbstractFindByPersistentMethod.java
  49. +378 −0 .../main/groovy/org/codehaus/groovy/grails/orm/hibernate/metaclass/AbstractSavePersistentMethod.java
  50. +112 −0 ...ain/groovy/org/codehaus/groovy/grails/orm/hibernate/metaclass/AbstractStaticPersistentMethod.java
  51. +56 −0 ...nate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/metaclass/BeforeValidateHelper.java
  52. +87 −0 ...e/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/metaclass/CountByPersistentMethod.java
  53. +197 −0 .../main/groovy/org/codehaus/groovy/grails/orm/hibernate/metaclass/ExecuteQueryPersistentMethod.java
  54. +146 −0 ...main/groovy/org/codehaus/groovy/grails/orm/hibernate/metaclass/ExecuteUpdatePersistentMethod.java
  55. +49 −0 .../org/codehaus/groovy/grails/orm/hibernate/metaclass/FindAllByBooleanPropertyPersistentMethod.java
  56. +128 −0 ...src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/metaclass/FindAllByPersistentMethod.java
  57. +284 −0 ...e/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/metaclass/FindAllPersistentMethod.java
  58. +49 −0 ...ovy/org/codehaus/groovy/grails/orm/hibernate/metaclass/FindByBooleanPropertyPersistentMethod.java
  59. +47 −0 ...te/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/metaclass/FindByPersistentMethod.java
  60. +73 −0 ...ain/groovy/org/codehaus/groovy/grails/orm/hibernate/metaclass/FindOrCreateByPersistentMethod.java
  61. +25 −0 .../main/groovy/org/codehaus/groovy/grails/orm/hibernate/metaclass/FindOrSaveByPersistentMethod.java
  62. +206 −0 ...nate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/metaclass/FindPersistentMethod.java
  63. +96 −0 ...c/main/groovy/org/codehaus/groovy/grails/orm/hibernate/metaclass/ListOrderByPersistentMethod.java
  64. +86 −0 ...nate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/metaclass/ListPersistentMethod.java
  65. +74 −0 ...ate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/metaclass/MergePersistentMethod.java
  66. +94 −0 ...nate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/metaclass/SavePersistentMethod.java
  67. +177 −0 .../src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/metaclass/ValidatePersistentMethod.java
  68. +54 −0 ...dehaus/groovy/grails/orm/hibernate/persister/entity/GroovyAwareJoinedSubclassEntityPersister.java
  69. +54 −0 .../codehaus/groovy/grails/orm/hibernate/persister/entity/GroovyAwareSingleTableEntityPersister.java
  70. +236 −0 ...in/groovy/org/codehaus/groovy/grails/orm/hibernate/proxy/GroovyAwareJavassistLazyInitializer.java
  71. +75 −0 .../main/groovy/org/codehaus/groovy/grails/orm/hibernate/proxy/GroovyAwareJavassistProxyFactory.java
  72. +140 −0 ...bernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/proxy/HibernateProxyHandler.java
  73. +303 −0 ...ate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/query/HibernateCriterionAdapter.java
  74. +91 −0 ...te/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/query/HibernateProjectionAdapter.java
  75. +837 −0 ...gorm-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/query/HibernateQuery.java
  76. +123 −0 ...oovy/org/codehaus/groovy/grails/orm/hibernate/support/AggregatePersistenceContextInterceptor.java
  77. +453 −0 ...ernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/support/ClosureEventListener.java
  78. +348 −0 ...in/groovy/org/codehaus/groovy/grails/orm/hibernate/support/ClosureEventTriggeringInterceptor.java
  79. +48 −0 ...rc/main/groovy/org/codehaus/groovy/grails/orm/hibernate/support/FlushOnRedirectEventListener.java
  80. +77 −0 ...c/main/groovy/org/codehaus/groovy/grails/orm/hibernate/support/GrailsOpenSessionInViewFilter.java
  81. +138 −0 ...n/groovy/org/codehaus/groovy/grails/orm/hibernate/support/GrailsOpenSessionInViewInterceptor.java
  82. +62 −0 ...ernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/support/HibernateBeanWrapper.java
  83. +103 −0 .../groovy/org/codehaus/groovy/grails/orm/hibernate/support/HibernateDialectDetectorFactoryBean.java
  84. +186 −0 ...oovy/org/codehaus/groovy/grails/orm/hibernate/support/HibernatePersistenceContextInterceptor.java
  85. +51 −0 ...tore-gorm-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/support/SoftKey.java
  86. +84 −0 .../groovy/org/codehaus/groovy/grails/orm/hibernate/support/SpringLobHandlerDetectorFactoryBean.java
  87. +89 −0 ...main/groovy/org/codehaus/groovy/grails/orm/hibernate/validation/AbstractPersistentConstraint.java
  88. +53 −0 ...ain/groovy/org/codehaus/groovy/grails/orm/hibernate/validation/HibernateConstraintsEvaluator.java
  89. +100 −0 ...ain/groovy/org/codehaus/groovy/grails/orm/hibernate/validation/HibernateDomainClassValidator.java
  90. +35 −0 ...ate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/validation/PersistentConstraint.java
  91. +62 −0 .../main/groovy/org/codehaus/groovy/grails/orm/hibernate/validation/PersistentConstraintFactory.java
  92. +243 −0 ...bernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/validation/UniqueConstraint.java
  93. +1 −0 grails-datastore-gorm-hibernate/src/main/resources/META-INF/spring.handlers
  94. +1 −0 grails-datastore-gorm-hibernate/src/main/resources/META-INF/spring.schemas
  95. +28 −0 ...datastore-gorm-hibernate/src/main/resources/org/codehaus/groovy/grails/orm/hibernate/cfg/gorm.xsd
  96. +109 −0 grails-datastore-gorm-hibernate/src/test/groovy/grails/gorm/tests/OptimisticLockingSpec.groovy
  97. +42 −0 grails-datastore-gorm-hibernate/src/test/groovy/grails/gorm/tests/UniqueConstraintSpec.groovy

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,201 @@
+/*
+ * Copyright 2004-2005 the original author or authors.
+ *
+ * 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 grails.orm;
+
+import java.io.IOException;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+import java.sql.SQLException;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ListIterator;
+
+import org.codehaus.groovy.grails.orm.hibernate.GrailsHibernateTemplate;
+import org.hibernate.Criteria;
+import org.hibernate.HibernateException;
+import org.hibernate.Session;
+import org.hibernate.criterion.Projections;
+import org.hibernate.impl.CriteriaImpl;
+import org.springframework.orm.hibernate3.HibernateCallback;
+
+/**
+ * A result list for Criteria list calls, which is aware of the totalCount for
+ * the paged result.
+ *
+ * @author Siegfried Puchbauer
+ * @since 1.0
+ */
+@SuppressWarnings({"unchecked","rawtypes"})
+public class PagedResultList implements List, Serializable {
+
+ private static final long serialVersionUID = -5820655628956173929L;
+
+ protected List list;
+
+ protected int totalCount = Integer.MIN_VALUE;
+
+ private transient GrailsHibernateTemplate hibernateTemplate;
+ private final Criteria criteria;
+
+ public PagedResultList(GrailsHibernateTemplate template, Criteria crit) {
+ list = crit.list();
+ criteria = crit;
+ hibernateTemplate = template;
+ }
+
+ public int size() {
+ return list.size();
+ }
+
+ public boolean isEmpty() {
+ return list.isEmpty();
+ }
+
+ public boolean contains(Object o) {
+ return list.contains(o);
+ }
+
+ public Iterator iterator() {
+ return list.iterator();
+ }
+
+ public Object[] toArray() {
+ return list.toArray();
+ }
+
+ public Object[] toArray(Object[] objects) {
+ return list.toArray(objects);
+ }
+
+ public boolean add(Object o) {
+ return list.add(o);
+ }
+
+ public boolean remove(Object o) {
+ return list.remove(o);
+ }
+
+ public boolean containsAll(Collection collection) {
+ return list.containsAll(collection);
+ }
+
+ public boolean addAll(Collection collection) {
+ return list.addAll(collection);
+ }
+
+ public boolean addAll(int i, Collection collection) {
+ return list.addAll(i, collection);
+ }
+
+ public boolean removeAll(Collection collection) {
+ return list.removeAll(collection);
+ }
+
+ public boolean retainAll(Collection collection) {
+ return list.retainAll(collection);
+ }
+
+ public void clear() {
+ list.clear();
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ return list.equals(o);
+ }
+
+ @Override
+ public int hashCode() {
+ return list.hashCode();
+ }
+
+ public Object get(int i) {
+ return list.get(i);
+ }
+
+ public Object set(int i, Object o) {
+ return list.set(i, o);
+ }
+
+ public void add(int i, Object o) {
+ list.add(i, o);
+ }
+
+ public Object remove(int i) {
+ return list.remove(i);
+ }
+
+ public int indexOf(Object o) {
+ return list.indexOf(o);
+ }
+
+ public int lastIndexOf(Object o) {
+ return list.lastIndexOf(o);
+ }
+
+ public ListIterator listIterator() {
+ return list.listIterator();
+ }
+
+ public ListIterator listIterator(int i) {
+ return list.listIterator(i);
+ }
+
+ public List subList(int i, int i1) {
+ return list.subList(i, i1);
+ }
+
+ public int getTotalCount() {
+ if (totalCount == Integer.MIN_VALUE) {
+ totalCount = (Integer)hibernateTemplate.execute(new HibernateCallback<Object>() {
+ public Object doInHibernate(Session session) throws HibernateException, SQLException {
+ CriteriaImpl impl = (CriteriaImpl) criteria;
+ Criteria totalCriteria = session.createCriteria(impl.getEntityOrClassName());
+ hibernateTemplate.applySettings(totalCriteria);
+
+ Iterator iterator = impl.iterateExpressionEntries();
+ while (iterator.hasNext()) {
+ CriteriaImpl.CriterionEntry entry = (CriteriaImpl.CriterionEntry) iterator.next();
+ totalCriteria.add(entry.getCriterion());
+ }
+ Iterator subcriteriaIterator = impl.iterateSubcriteria();
+ while (subcriteriaIterator.hasNext()) {
+ CriteriaImpl.Subcriteria sub = (CriteriaImpl.Subcriteria) subcriteriaIterator.next();
+ totalCriteria.createAlias(sub.getPath(), sub.getAlias(), sub.getJoinType(), sub.getWithClause());
+ }
+ totalCriteria.setProjection(impl.getProjection());
+ totalCriteria.setProjection(Projections.rowCount());
+ return ((Number)totalCriteria.uniqueResult()).intValue();
+ }
+ });
+ }
+ return totalCount;
+ }
+
+ public void setTotalCount(int totalCount) {
+ this.totalCount = totalCount;
+ }
+
+ private void writeObject(ObjectOutputStream out) throws IOException {
+
+ // find the total count if it hasn't been done yet so when this is deserialized
+ // the null GrailsHibernateTemplate won't be an issue
+ getTotalCount();
+
+ out.defaultWriteObject();
+ }
+}
@@ -0,0 +1,79 @@
+/* Copyright 2004-2005 the original author or authors.
+ *
+ * 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 grails.orm;
+
+import org.hibernate.Criteria;
+import org.hibernate.HibernateException;
+import org.hibernate.criterion.CriteriaQuery;
+import org.hibernate.criterion.Criterion;
+import org.hibernate.criterion.MatchMode;
+import org.hibernate.dialect.Dialect;
+import org.hibernate.dialect.MySQLDialect;
+import org.hibernate.dialect.Oracle8iDialect;
+import org.hibernate.engine.TypedValue;
+
+/**
+ * Adds support for rlike to Hibernate in supported dialects.
+ *
+ * @author Graeme Rocher
+ * @since 1.1.1
+ */
+public class RlikeExpression implements Criterion {
+
+ private static final long serialVersionUID = -214329918050957956L;
+
+ private final String propertyName;
+ private final Object value;
+
+ public RlikeExpression(String propertyName, Object value) {
+ this.propertyName = propertyName;
+ this.value = value;
+ }
+
+ public RlikeExpression(String propertyName, String value, MatchMode matchMode) {
+ this(propertyName, matchMode.toMatchString(value));
+ }
+
+ public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
+ Dialect dialect = criteriaQuery.getFactory().getDialect();
+ String[] columns = criteriaQuery.getColumnsUsingProjection(criteria, propertyName);
+ if (columns.length != 1) {
+ throw new HibernateException("ilike may only be used with single-column properties");
+ }
+
+ if (dialect instanceof MySQLDialect) {
+ return columns[0] + " rlike ?";
+ }
+
+ if (isOracleDialect(dialect)) {
+ return " REGEXP_LIKE (" + columns[0] + ", ?)";
+ }
+
+ return columns[0] + " like ?";
+ }
+
+ private boolean isOracleDialect(Dialect dialect) {
+ return (dialect instanceof Oracle8iDialect);
+ }
+
+ public TypedValue[] getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
+ return new TypedValue[] { criteriaQuery.getTypedValue(criteria, propertyName, value.toString().toLowerCase()) };
+ }
+
+ @Override
+ public String toString() {
+ return propertyName + " rlike " + value;
+ }
+}
@@ -0,0 +1,112 @@
+/*
+ * Copyright 2011 SpringSource
+ *
+ * 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 org.codehaus.groovy.grails.compiler.gorm;
+
+import grails.persistence.Entity;
+
+import java.net.URL;
+import java.util.Arrays;
+import java.util.List;
+
+import org.codehaus.groovy.ast.ClassNode;
+import org.codehaus.groovy.ast.MethodNode;
+import org.codehaus.groovy.ast.expr.ClassExpression;
+import org.codehaus.groovy.ast.expr.MethodCallExpression;
+import org.codehaus.groovy.ast.stmt.BlockStatement;
+import org.codehaus.groovy.ast.stmt.ExpressionStatement;
+import org.codehaus.groovy.control.SourceUnit;
+import org.codehaus.groovy.grails.commons.DomainClassArtefactHandler;
+import org.codehaus.groovy.grails.commons.GrailsClassUtils;
+import org.codehaus.groovy.grails.commons.metaclass.CreateDynamicMethod;
+import org.codehaus.groovy.grails.compiler.injection.AbstractGrailsArtefactTransformer;
+import org.codehaus.groovy.grails.compiler.injection.AstTransformer;
+import org.codehaus.groovy.grails.compiler.injection.GrailsASTUtils;
+import org.codehaus.groovy.grails.io.support.GrailsResourceUtils;
+import org.grails.datastore.gorm.GormInstanceApi;
+import org.grails.datastore.gorm.GormStaticApi;
+
+/**
+ * Transforms GORM entities making the GORM API available to Java.
+ *
+ * @author Graeme Rocher
+ * @since 2.0
+ */
+@AstTransformer
+public class GormTransformer extends AbstractGrailsArtefactTransformer {
+
+ public static final String NEW_INSTANCE_METHOD = "newInstance";
+
+ private static final List<String> EXCLUDES = Arrays.asList("create", "setTransactionManager");
+ private static final Class<?>[] EMPTY_JAVA_CLASS_ARRAY = {};
+ private static final Class<?>[] OBJECT_CLASS_ARG = { Object.class };
+
+ @Override
+ protected boolean isStaticCandidateMethod(ClassNode classNode, MethodNode declaredMethod) {
+ String methodName = declaredMethod.getName();
+ return !EXCLUDES.contains(methodName) &&
+ !isGetter(methodName, declaredMethod) &&
+ !isSetter(methodName, declaredMethod) &&
+ super.isStaticCandidateMethod(classNode, declaredMethod);
+ }
+
+ private boolean isSetter(String methodName, MethodNode declaredMethod) {
+ return declaredMethod.getParameters().length ==2 && GrailsClassUtils.isSetter(methodName, OBJECT_CLASS_ARG);
+ }
+
+ private boolean isGetter(String methodName, MethodNode declaredMethod) {
+ return declaredMethod.getParameters().length == 1 && GrailsClassUtils.isGetter(methodName, EMPTY_JAVA_CLASS_ARRAY);
+ }
+
+ @Override
+ public String getArtefactType() {
+ return DomainClassArtefactHandler.TYPE;
+ }
+
+ public Class<?> getInstanceImplementation() {
+ return GormInstanceApi.class;
+ }
+
+ public Class<?> getStaticImplementation() {
+ return GormStaticApi.class;
+ }
+
+ @Override
+ protected boolean requiresStaticLookupMethod() {
+ return true;
+ }
+
+ protected MethodNode populateAutowiredApiLookupMethod(ClassNode classNode, ClassNode implementationNode, String apiInstanceProperty, String methodName, BlockStatement methodBody) {
+ return new MethodNode(methodName, PUBLIC_STATIC_MODIFIER, implementationNode,ZERO_PARAMETERS,null,methodBody);
+ }
+
+ @Override
+ protected void performInjectionInternal(String apiInstanceProperty, SourceUnit source, ClassNode classNode) {
+ classNode.setUsingGenerics(true);
+ GrailsASTUtils.addAnnotationIfNecessary(classNode, Entity.class);
+
+ final BlockStatement methodBody = new BlockStatement();
+ methodBody.addStatement(new ExpressionStatement(new MethodCallExpression(new ClassExpression(classNode), NEW_INSTANCE_METHOD,ZERO_ARGS)));
+ MethodNode methodNode = classNode.getDeclaredMethod(CreateDynamicMethod.METHOD_NAME, ZERO_PARAMETERS);
+ classNode = GrailsASTUtils.nonGeneric(classNode);
+ if (methodNode == null) {
+ classNode.addMethod(new MethodNode(CreateDynamicMethod.METHOD_NAME, PUBLIC_STATIC_MODIFIER, classNode, ZERO_PARAMETERS,null, methodBody));
+ }
+ }
+
+ public boolean shouldInject(URL url) {
+ return GrailsResourceUtils.isDomainClass(url);
+ }
+}
Oops, something went wrong.

0 comments on commit 8abd5d9

Please sign in to comment.