Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

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

This comment has been minimized.

Copy link

netom commented May 12, 2020

This is golden. :D

@diomondus

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.