Skip to content

Instantly share code, notes, and snippets.

@thom-nic
Last active November 16, 2023 07:35
Show Gist options
  • Star 47 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save thom-nic/2c74ed4075569da0f80b to your computer and use it in GitHub Desktop.
Save thom-nic/2c74ed4075569da0f80b to your computer and use it in GitHub Desktop.
find the largest classnames in Spring libraries. Also find FactoryFactories
/**
* Find the longest class names in Spring.
* Also find FactoryFactory classes.
* a goof-off project by @thom_nic
*/
import java.util.jar.*
defaultTasks 'longest', 'factoryfactory'
apply plugin: 'java'
repositories {
mavenCentral()
}
def group = 'org.springframework'
def version = '4.0.5.RELEASE'
def jars = [
'spring-context',
'spring-core',
'spring-beans',
'spring-test',
'spring-web',
'spring-aop',
'spring-webmvc',
'spring-jdbc',
'spring-tx',
'spring-context-support',
'spring-orm',
'spring-jms',
'spring-expression',
'spring-aspects',
'spring-oxm',
'spring-webmvc-portlet',
'spring-instrument',
'spring-messaging',
'spring-websocket',
'spring-instrument-tomcat',
'spring-framework-bom'
]
dependencies {
jars.each {
compile group: group, name: it, version: version
}
}
def iterateClassNames(cb) {
sourceSets.main.compileClasspath.each {
// println it.name
def jar = new JarFile( it )
jar.entries().each {
if ( it.directory ) return
if ( ! it.name.endsWith( '.class' ) ) return
def n = it.name
n = n.substring( n.lastIndexOf( '/' )+1 )[0..-7]
cb n
}
}
}
task longest << {
def winners = new TreeSet( { a,b -> a.size() - b.size() } as Comparator )
iterateClassNames { n ->
n.split( /\$/ ).each {
if ( it.matches(/\d+/) ) return // ignore anonymous subclasses
// println " $it"
winners << it
}
}
println "============ Top 5: ==============="
(1..5).each {
def v = winners.pollLast()
println "$v: ${v.size()}"
}
}
task factoryfactory << {
def factories = new HashSet()
iterateClassNames {
if ( it.matches( /.*Factory.*Factory.*/ ) ) factories << it
}
println "============ Factories: ==============="
factories.each {
println it
}
}
:longest
============ Top 5: ===============
HasThisTypePatternTriedToSneakInSomeGenericOrParameterizedTypePatternMatchingStuffAnywhereVisitor: 97
AbstractAnnotationConfigDispatcherServletInitializer: 52
AbstractInterruptibleBatchPreparedStatementSetter: 49
AbstractInterceptorDrivenBeanDefinitionDecorator: 48
GenericInterfaceDrivenDependencyInjectionAspect: 47
:factoryfactory
============ Factories: ===============
DefaultListableBeanFactory$DependencyObjectFactory
ObjectFactoryCreatingFactoryBean
SimpleBeanFactoryAwareAspectInstanceFactory
SingletonBeanFactoryLocator$BeanFactoryGroup
ConnectionFactoryUtils$ResourceFactory
DefaultListableBeanFactory$DependencyProviderFactory
ObjectFactoryCreatingFactoryBean$TargetBeanObjectFactory
JndiObjectFactoryBean$JndiObjectProxyFactory
DefaultListableBeanFactory$SerializedBeanFactoryReference
AbstractEntityManagerFactoryBean$SerializedEntityManagerFactoryBeanReference
BeanFactoryAspectInstanceFactory
SingletonBeanFactoryLocator$CountingBeanFactoryReference
TransactionAwarePersistenceManagerFactoryProxy$PersistenceManagerFactoryInvocationHandler
AbstractEntityManagerFactoryBean$ManagedEntityManagerFactoryInvocationHandler
@mraible
Copy link

mraible commented Jul 13, 2019

Here's a version that works with Gradle 5.3.1 and Spring 5.1.8.

/**
 * Find the longest class names in Spring.  
 * Also find FactoryFactory classes.
 * a goof-off project by @thom_nic
 */

import java.util.jar.*

defaultTasks 'longest', 'factoryfactory'

apply plugin: 'java'

repositories {
  mavenCentral()
}

def group = 'org.springframework'
def version = '5.1.8.RELEASE'
def jars = [
	'spring-context',
	'spring-core',
	'spring-beans',
	'spring-test',
	'spring-web',
	'spring-aop',
	'spring-webmvc',
	'spring-jdbc',
	'spring-tx',
	'spring-context-support',
	'spring-orm',
	'spring-jms',
	'spring-expression',
	'spring-aspects',
	'spring-oxm',
	'spring-instrument',
	'spring-messaging',
	'spring-websocket',
	'spring-framework-bom'
]

dependencies {
	jars.each {
		compile group: group, name: it, version: version
	}
}

def iterateClassNames(cb) {
  sourceSets.main.compileClasspath.each {
//		println it.name
		def jar = new JarFile( it )
		jar.entries().each {
			if ( it.directory ) return
			if ( ! it.name.endsWith( '.class' ) ) return
			def n = it.name
			n = n.substring( n.lastIndexOf( '/' )+1 )[0..-7]

			cb n
		}
	}
}

task longest {
	def winners = new TreeSet( { a,b -> a.size() - b.size() } as Comparator )
		
	iterateClassNames { n ->
		n.split( /\$/ ).each {
			if ( it.matches(/\d+/) ) return  // ignore anonymous subclasses
			winners << it
		}
	}

	println "============ Top 5: ==============="
	(1..5).each {
		def v = winners.pollLast()
		println "$v: ${v.size()}"
	}
}

task factoryfactory {
	def factories = new HashSet()

	iterateClassNames {
		if ( it.matches( /.*Factory.*Factory.*/ ) ) factories << it
	}
	
	println "============ Factories: ==============="
	factories.each {
		println it
	}
}

Output:

============ Top 5: ===============
HasThisTypePatternTriedToSneakInSomeGenericOrParameterizedTypePatternMatchingStuffAnywhereVisitor: 97
AbstractAnnotationConfigDispatcherServletInitializer: 52
ClassFilterAwareUnionIntroductionAwareMethodMatcher: 51
AbstractInterruptibleBatchPreparedStatementSetter: 49
AbstractInterceptorDrivenBeanDefinitionDecorator: 48
============ Factories: ===============
AbstractEntityManagerFactoryBean$ManagedEntityManagerFactoryInvocationHandler
ObjectFactoryCreatingFactoryBean
DefaultListableBeanFactory$FactoryAwareOrderSourceProvider
LocalSessionFactoryBuilder$BootstrapSessionFactoryInvocationHandler
DefaultListableBeanFactory$SerializedBeanFactoryReference
DefaultListableBeanFactory$Jsr330Factory
AbstractEntityManagerFactoryBean$SerializedEntityManagerFactoryBeanReference
SimpleBeanFactoryAwareAspectInstanceFactory
JndiObjectFactoryBean$JndiObjectProxyFactory
ConnectionFactoryUtils$ResourceFactory
ObjectFactoryCreatingFactoryBean$TargetBeanObjectFactory
BeanFactoryAspectInstanceFactory
DefaultListableBeanFactory$Jsr330Factory$Jsr330Provider

@netom
Copy link

netom commented May 12, 2020

This is golden. :D

@TangoEnSkai
Copy link

wow this attempt is rad.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment