Skip to content

Commit

Permalink
Revert "feat(core): Remove/Replace deprecated Groovy classes to prepa…
Browse files Browse the repository at this point in the history
…re upgrade to Groovy 4 (#13386)"

This reverts commit e2061a8.

# Conflicts:
#	gradle.properties
#	grails-databinding/src/test/groovy/grails/databinding/SimpleDataBinderSpec.groovy
  • Loading branch information
jdaugherty committed Oct 1, 2024
1 parent d001c6c commit a7f00c7
Show file tree
Hide file tree
Showing 72 changed files with 478 additions and 566 deletions.
2 changes: 0 additions & 2 deletions gradle/idea.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import groovy.xml.XmlParser

idea {
project {
jdkName "1.7"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package grails.codegen.model

import grails.util.GrailsNameUtils
import groovy.transform.CompileStatic
import org.apache.groovy.util.BeanUtils
import org.codehaus.groovy.runtime.MetaClassHelper
import org.grails.io.support.FileSystemResource
import org.grails.io.support.GrailsResourceUtils
import org.grails.io.support.Resource
Expand Down Expand Up @@ -89,7 +89,7 @@ trait ModelBuilder {
final String packagePath

ModelImpl(String className) {
this.className = BeanUtils.capitalize(GrailsNameUtils.getShortName(className))
this.className = MetaClassHelper.capitalize(GrailsNameUtils.getShortName(className))
this.fullName = className
this.propertyName = GrailsNameUtils.getPropertyName(className)
this.packageName = GrailsNameUtils.getPackageName(className)
Expand All @@ -111,14 +111,7 @@ trait ModelBuilder {

@Override
Map<String, Object> asMap() {
[className: className,
fullName: fullName,
propertyName: propertyName,
modelName: propertyName,
packageName: packageName,
packagePath: packagePath,
simpleName: simpleName,
lowerCaseName: lowerCaseName] as Map<String, Object>
(Map<String,Object>) [ className: className, fullName: fullName, propertyName: propertyName, modelName: propertyName, packageName: packageName, packagePath: packagePath, simpleName: simpleName, lowerCaseName: lowerCaseName]
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package org.grails.io.support;

import groovy.xml.XmlSlurper;
import groovy.util.XmlSlurper;
import groovy.xml.FactorySupport;
import org.xml.sax.SAXException;
import javax.xml.XMLConstants;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
package grails.ui.shell.support

import grails.core.GrailsApplication
import org.apache.groovy.groovysh.Groovysh
import grails.ui.support.DevelopmentWebApplicationContext
import org.codehaus.groovy.tools.shell.Groovysh
import org.codehaus.groovy.tools.shell.IO
import org.springframework.context.support.GenericApplicationContext


/**
* @author Graeme Rocher
* @since 3.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ import grails.core.GrailsApplication
import grails.ui.support.DevelopmentWebApplicationContext
import groovy.transform.CompileStatic
import groovy.transform.InheritConstructors
import org.apache.groovy.groovysh.Groovysh
import org.codehaus.groovy.tools.shell.Groovysh
import org.codehaus.groovy.tools.shell.IO


/**
* @author Graeme Rocher
* @since 3.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package grails.util;

import grails.util.GrailsStringUtils;
import groovy.lang.GroovyObjectSupport;
import org.codehaus.groovy.runtime.DefaultGroovyMethods;
import org.codehaus.groovy.util.HashCodeHelper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ abstract class GrailsStringUtils extends StringUtils{
* Same as {@link StringUtils#isEmpty(java.lang.Object)} but trims the string for surrounding whitespace
*/
static boolean isBlank(String str) {
!hasText(str?.trim())
isEmpty(str?.trim())
}

/**
Expand All @@ -179,7 +179,7 @@ abstract class GrailsStringUtils extends StringUtils{
* Opposite of {@link GrailsStringUtils#isEmpty(java.lang.Object)}
*/
static boolean isNotEmpty(String str) {
hasText(str)
!isEmpty(str)
}

/**
Expand Down
15 changes: 15 additions & 0 deletions grails-core/src/main/groovy/grails/util/GrailsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,27 @@
*/
package grails.util;

import grails.io.IOUtils;
import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import groovy.lang.Writable;
import groovy.util.slurpersupport.GPathResult;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.grails.core.io.CachingPathMatchingResourcePatternResolver;
import org.grails.exceptions.reporting.DefaultStackTraceFilterer;
import org.grails.exceptions.reporting.StackTraceFilterer;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.net.URL;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

/**
* Grails utility methods for command line and GUI applications.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import grails.util.GrailsNameUtils
import groovy.transform.CompilationUnitAware
import groovy.transform.CompileDynamic
import groovy.transform.CompileStatic
import groovy.util.slurpersupport.GPathResult
import groovy.xml.MarkupBuilder
import groovy.xml.StreamingMarkupBuilder
import groovy.xml.XmlSlurper
import groovy.xml.slurpersupport.GPathResult
import org.codehaus.groovy.ast.*
import org.codehaus.groovy.ast.expr.ConstantExpression
import org.codehaus.groovy.control.CompilationUnit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class GrailsFactoriesLoader extends FactoriesLoaderSupport {
boolean hasArguments = !(arguments != null && arguments.length==0)
List<T> results = new ArrayList<T>()
for(Class<? extends T> clazz : loadFactoryClasses(factoryClass, classLoader)) {
results.add(hasArguments ? clazz.newInstance(arguments) : clazz.getDeclaredConstructor().newInstance())
results.add(hasArguments ? clazz.newInstance(arguments) : clazz.newInstance())
}
OrderComparator.sort((List<?>) results)
results
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package org.grails.plugins;

import groovy.xml.slurpersupport.GPathResult;
import groovy.util.slurpersupport.GPathResult;
import org.grails.core.exceptions.GrailsConfigurationException;
import org.grails.io.support.SpringIOUtils;
import org.springframework.core.io.Resource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class EnhancesSpec extends Specification{
void "Test that the enhances trait transform works as expected"() {

when:"The generated transformer is loaded"
def traitInjector = getClass().classLoader.loadClass("grails.artefact.FooTraitInjector").getDeclaredConstructor().newInstance()
def traitInjector = getClass().classLoader.loadClass("grails.artefact.FooTraitInjector").newInstance()

then:"It is a valid trait injector"
traitInjector instanceof TraitInjector
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.grails.compiler.injection

import groovy.xml.MarkupBuilder
import groovy.xml.XmlSlurper
import org.codehaus.groovy.ast.ClassNode
import org.codehaus.groovy.classgen.GeneratorContext
import org.codehaus.groovy.control.CompilationFailedException
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package org.grails.core.io

import grails.core.DefaultGrailsApplication
import groovy.xml.XmlSlurper
import org.grails.plugins.*
import org.grails.plugins.BinaryGrailsPlugin
import org.grails.plugins.BinaryGrailsPluginDescriptor
import org.grails.plugins.MockBinaryPluginResource
import org.grails.plugins.MockGrailsPluginManager
import org.grails.plugins.TestBinaryGrailsPlugin
import org.springframework.core.io.ByteArrayResource
import org.springframework.core.io.ResourceLoader

import spock.lang.Specification

class ResourceLocatorSpec extends Specification {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class FooController {
filterer.setCutOffPackage("org.spockframework.util")
Throwable exception
try {
cls.getDeclaredConstructor().newInstance().show()
cls.newInstance().show()
} catch (e) {
filterer.filter(e)
exception = e
Expand Down Expand Up @@ -77,7 +77,7 @@ class FooService {
filterer.setCutOffPackage("org.spockframework.util")
Throwable exception
try {
cls.getDeclaredConstructor().newInstance().show()
cls.newInstance().show()
} catch (e) {
filterer.filter(e, true)
println getExceptionContents(e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class StackTracePrinterSpec extends Specification {
void "Test pretty print simple stack trace"() {
given: "a controller that throws an exception"
final gcl = new GroovyClassLoader()
gcl.parseClass(toBufferedReader(getServiceResource().inputStream), serviceResource.filename)
def controller = gcl.parseClass(new BufferedReader(new InputStreamReader(getControllerResource().inputStream)), getControllerResource().filename).getDeclaredConstructor().newInstance()
gcl.parseClass(getServiceResource().inputStream, serviceResource.filename)
def controller = gcl.parseClass(getControllerResource().inputStream, controllerResource.filename).newInstance()
when:"An exception is pretty printed"
def printer = new DefaultErrorsPrinter()
def result = null
Expand All @@ -36,8 +36,8 @@ class StackTracePrinterSpec extends Specification {
void "Test pretty print nested stack trace"() {
given: "a controller that throws an exception"
final gcl = new GroovyClassLoader()
gcl.parseClass(toBufferedReader(getServiceResource().inputStream), serviceResource.filename)
def controller = gcl.parseClass(toBufferedReader(getControllerResource().inputStream), getControllerResource().filename).getDeclaredConstructor().newInstance()
gcl.parseClass(getServiceResource().inputStream, serviceResource.filename)
def controller = gcl.parseClass(getControllerResource().inputStream, controllerResource.filename).newInstance()
when:"An exception is pretty printed"
def printer = new DefaultErrorsPrinter()
def result = null
Expand All @@ -58,8 +58,8 @@ class StackTracePrinterSpec extends Specification {
void "Test pretty print nested stack trace for JDK 11"() {
given: "a controller that throws an exception"
final gcl = new GroovyClassLoader()
gcl.parseClass(toBufferedReader(getServiceResource().inputStream), serviceResource.filename)
def controller = gcl.parseClass(toBufferedReader(getControllerResource().inputStream), getControllerResource().filename).getDeclaredConstructor().newInstance()
gcl.parseClass(getServiceResource().inputStream, serviceResource.filename)
def controller = gcl.parseClass(getControllerResource().inputStream, controllerResource.filename).newInstance()
when:"An exception is pretty printed"
def printer = new DefaultErrorsPrinter()
def result = null
Expand All @@ -79,8 +79,8 @@ class StackTracePrinterSpec extends Specification {
void "Test pretty print code snippet"() {
given: "a controller that throws an exception"
final gcl = new GroovyClassLoader()
gcl.parseClass(toBufferedReader(getServiceResource().inputStream), serviceResource.filename)
def controller = gcl.parseClass(toBufferedReader(getControllerResource().inputStream), getControllerResource().filename).getDeclaredConstructor().newInstance()
gcl.parseClass(getServiceResource().inputStream, serviceResource.filename)
def controller = gcl.parseClass(getControllerResource().inputStream, getControllerResource().filename).newInstance()

when: "A code snippet is pretty printed"
final locator = new StaticResourceLocator()
Expand Down Expand Up @@ -118,8 +118,8 @@ Around line 5 of FooController.groovy
void "Test pretty print nested exception code snippet"() {
given:"a service that throws an exception that is caught and rethrown"
final gcl = new GroovyClassLoader()
gcl.parseClass(toBufferedReader(getServiceResource().inputStream), serviceResource.filename)
def controller = gcl.parseClass(toBufferedReader(getControllerResource().inputStream), getControllerResource().filename).getDeclaredConstructor().newInstance()
gcl.parseClass(getServiceResource().inputStream, serviceResource.filename)
def controller = gcl.parseClass(controllerResource.inputStream, controllerResource.filename).newInstance()
final locator = new StaticResourceLocator()
locator.addClassResource("test.FooController", controllerResource)
locator.addClassResource("test.FooService", serviceResource)
Expand Down Expand Up @@ -198,8 +198,4 @@ class FooService {
}
}
}

private BufferedReader toBufferedReader(InputStream inputStream) {
new BufferedReader(new InputStreamReader(inputStream))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package grails.databinding;

import grails.databinding.events.DataBindingListener;
import groovy.xml.slurpersupport.GPathResult;
import groovy.util.slurpersupport.GPathResult;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import grails.databinding.events.DataBindingListener
import grails.databinding.initializers.ValueInitializer
import groovy.transform.CompileStatic
import groovy.transform.TypeCheckingMode
import groovy.xml.slurpersupport.GPathResult
import groovy.util.slurpersupport.GPathResult
import org.codehaus.groovy.reflection.CachedMethod
import org.grails.databinding.ClosureValueConverter
import org.grails.databinding.ClosureValueInitializer
Expand Down Expand Up @@ -344,11 +344,11 @@ class SimpleDataBinder implements DataBinder {
} else if (isBasicType(genericType)) {
addElementToCollectionAt obj, propName, collectionInstance, index, convert(genericType, val)
} else if (val instanceof Map){
indexedInstance = genericType.getDeclaredConstructor().newInstance()
indexedInstance = genericType.newInstance()
bind indexedInstance, new SimpleMapDataBindingSource(val), listener
addElementToCollectionAt obj, propName, collectionInstance, index, indexedInstance
} else if (val instanceof DataBindingSource) {
indexedInstance = genericType.getDeclaredConstructor().newInstance()
indexedInstance = genericType.newInstance()
bind indexedInstance, val, listener
addElementToCollectionAt obj, propName, collectionInstance, index, indexedInstance
} else if(genericType.isEnum() && val instanceof CharSequence) {
Expand Down Expand Up @@ -752,7 +752,7 @@ class SimpleDataBinder implements DataBinder {
obj[propName] = initializer.initialize()
}
else{
obj[propName] = propertyType.getDeclaredConstructor().newInstance()
obj[propName] = propertyType.newInstance()
}
}

Expand Down Expand Up @@ -815,7 +815,7 @@ class SimpleDataBinder implements DataBinder {
} else if (typeToConvertTo.isPrimitive() || typeToConvertTo.isArray()) {
return value
} else if (value instanceof Map) {
def obj = typeToConvertTo.getDeclaredConstructor().newInstance()
def obj = typeToConvertTo.newInstance()
bind obj, new SimpleMapDataBindingSource(value)
return obj
} else if (Enum.isAssignableFrom(typeToConvertTo) && value instanceof String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import grails.databinding.CollectionDataBindingSource;
import grails.databinding.DataBindingSource;
import grails.databinding.SimpleMapDataBindingSource;
import groovy.transform.CompileStatic
import groovy.xml.slurpersupport.GPathResult
import groovy.util.slurpersupport.GPathResult

@CompileStatic
class GPathResultCollectionDataBindingSource implements CollectionDataBindingSource {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/
package org.grails.databinding.xml

import groovy.xml.slurpersupport.GPathResult
import groovy.xml.slurpersupport.Node
import groovy.xml.slurpersupport.NodeChild
import groovy.util.slurpersupport.GPathResult
import groovy.util.slurpersupport.Node
import groovy.util.slurpersupport.NodeChild

/**
* @author Jeff Brown
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
*/
package grails.databinding


import groovy.xml.XmlSlurper
import grails.databinding.SimpleDataBinder;
import spock.lang.Specification

class XMLBindingSpec extends Specification {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.grails.databinding.xml

import grails.databinding.DataBindingSource
import groovy.xml.XmlSlurper
import grails.databinding.DataBindingSource;
import spock.lang.Specification

class GPathCollectionDataBindingSourceSpec extends Specification {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
package org.grails.databinding.xml

import groovy.xml.XmlSlurper
import spock.lang.Specification

class GPathResultMapSpec extends Specification {
Expand Down
2 changes: 1 addition & 1 deletion grails-docs/src/main/groovy/grails/doc/DocEngine.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class DocEngine extends BaseRenderEngine implements WikiRenderEngine {
EscapeFilter]

for (f in filters) {
RegexFilter filter = f.getDeclaredConstructor().newInstance()
RegexFilter filter = f.newInstance()
fp.addFilter(filter)

if (filter instanceof MacroFilter) {
Expand Down
Loading

2 comments on commit a7f00c7

@paulk-asert
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect all of these changes are okay but here's a few more details on some of the changes if anyone wants to bring any back:

  • In general for Groovy 3+, using the moved packages due to JPMS split packaging requirements is recommended, e.g. groovy.xml.XmlSlurper is preferred over the deprecated groovy.util.XmlSlurper. The exception would be where you might have old plugins still using the legacy versions and you share the slurper or slurper results between core code and plugin code - which would be bad form since the slurper use should normally remain encapsulated and results should be shared as an Iterable in such a case but I digress. Things like groovy.test.NotYetImplemented should always be safe to use since it is just test code.
  • clazz.newInstance() should always be safe to replace with clazz.getDeclaredConstructor().newInstance(). That is a JDK thing not a Groovy thing since the former was deprecated for JDK9+.
  • Going from Groovy 3 to 4, type checking was strengthened. Groovy 3 ignored some cases. In general, adding in the type checking information does no harm since 3 often ignores it but 4 needs it.

@jdaugherty
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit was reverted only for the 6.2.1 release. We reverted this revert for the merge to 7.0 so these should be restored.

Please sign in to comment.