question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Minimise need to iterate over a class's methods

See original GitHub issue

There are a number of places where Framework iterates over a class’s methods. This issue is intended to identify the places where the iteration is unnecessary and can be eliminated. ReflectionUtils keeps a per-Class cache of declared methods. To provide any significant benefit it will be necessary to eliminate all calls to ReflectionUtils for a particular class. It’s not yet known if this is possible.

AbstractAutowireCapableBeanFactory.determineTargetType uses the bean definition’s targetType to short-circuit type determination. When a bean is defined by ConfigurationClassBeanDefinitionReader processing a @Bean method, this type information is available but it is not used. If the metadata for the @Bean method is StandardMethodMetadata, the target type of the definition is available from the introspected method’s return type. Otherwise, the name of the target type is available from MethodMetadata.getReturnTypeName(). It appears to be possible for this type name to be stored in the definition and then be used to load the named class when getTargetType() is invoked.

When processing a ConfigurationClassBeanDefinition, ConstructorResolver.instantiateUsingFactoryMethod calls ReflectionUtils.getAllDeclaredMethods to find the Method for the @Bean method for which the bean definition was created. When the definition was created from StandardMethodMetadata, I think this method will already have been available and could, perhaps, have been stored in the definition’s resolvedConstructorOrFactoryMethod field. For ASM-based metadata, the method’s name is available and, with some changes to the metadata, it looks like its parameter types could be too. This may then allow the method to be identified directly without the need to iterate over all methods.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:20 (20 by maintainers)

github_iconTop GitHub Comments

1reaction
wilkinsonacommented, Mar 14, 2019

Things have changed quite a bit in 2.2. We’re now using the resolved factory method that’s cached on the definition so overriding should not cause any problems. That said, please don’t point people to ConfigurationBeanFactoryMetadata. While it’s currently public we would prefer that it, or at least as much as possible of it, is not.

1reaction
wilkinsonacommented, Feb 18, 2019

Somewhat related to this, I think there is some benefit in making the factory method that’s used to create something defined using @Bean more readily available.

Boot supports the use of @ConfigurationProperties on a @Bean method, predominantly to support binding to third-party classes when annotating the class itself with @ConfigurationProperties isn’t possible. Boot has a class, ConfigurationBeanFactoryMetadata, that performs factory method discovery and caches its results. It would be more robust if the factory Method was directly available via the bean definition rather than having to duplicate the reflective search over the factory class’s methods to find the factory method.

Please let me know if you’d prefer for this piece to be tracked as a separate issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ES6 Iterate over class methods - javascript - Stack Overflow
Since methods on ES6 class are non-enumerable, you have no other option but to use Object.getOwnPropertyNames() to get an array of all its ......
Read more >
The 4 Methods for Iterating Collections in Java - CodeJava.net
The Java programming language provides four methods for iterating over collections, including for loops, iterator and forEach (since Java 8) ...
Read more >
21 Iteration | R for Data Science - Hadley Wickham
One tool for reducing duplication is functions, which reduce duplication by identifying repeated patterns of code and extract them out into independent pieces ......
Read more >
How to Iterate Over a Class's Properties in Kotlin?
So, if you want to just iterate over the declared fields in the Student class, you will need the declaredMemberProperties method. Here's an ......
Read more >
for...of - JavaScript | MDN - MDN Web Docs
Each operation of the loop on a value is called an iteration, and the loop is said to iterate over the iterable.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found