Sample

int-c9f3bb6b6dc3-SRC

Unmodified source class

Lines159

Scores

MethodScoreScale
Posnett00–1; higher is more readable
Scalabrino0.2570–1; higher is more readable
Dorn0.1620–1; higher is more readable
Mi0.0820–1; higher is more readable
LLOC49Logical lines; fewer is more readable
DeepSeek V4 Pro14.670–20; mean of three runs
GPT-6.1 Sol170–20; mean of three runs
BTTF0.4230–1; higher is more readable

Views

Source code

package org.springframework.test.context.bean.override.convention; import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Collection; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; import java.util.stream.Collectors; import org.springframework.core.MethodIntrospector; import org.springframework.core.ResolvableType; import org.springframework.test.context.TestContextAnnotationUtils; import org.springframework.test.context.bean.override.BeanOverrideProcessor; import org.springframework.test.context.bean.override.BeanOverrideStrategy; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils.MethodFilter; import static org.springframework.test.context.bean.override.BeanOverrideStrategy.REPLACE; import static org.springframework.test.context.bean.override.BeanOverrideStrategy.REPLACE_OR_CREATE; /** * {@link BeanOverrideProcessor} implementation for {@link TestBean @TestBean} * support, which creates a {@link TestBeanOverrideHandler} for annotated * fields in a given class and ensures that a corresponding static factory method * exists, according to the {@linkplain TestBean documented conventions}. * * @author Simon Baslé * @author Sam Brannen * @author Stephane Nicoll * @since 6.2 */ class TestBeanOverrideProcessor implements BeanOverrideProcessor { @Override public TestBeanOverrideHandler createHandler(Annotation overrideAnnotation, Class<?> testClass, Field field) { if (!(overrideAnnotation instanceof TestBean testBean)) { throw new IllegalStateException("Invalid annotation passed to %s: expected @TestBean on field %s.%s" .formatted(getClass().getSimpleName(), field.getDeclaringClass().getName(), field.getName())); } String beanName = (!testBean.name().isBlank() ? testBean.name() : null); String methodName = testBean.methodName(); BeanOverrideStrategy strategy = (testBean.enforceOverride() ? REPLACE : REPLACE_OR_CREATE); Method factoryMethod; if (!methodName.isBlank()) { // If the user specified an explicit method name, search for that. factoryMethod = findTestBeanFactoryMethod(field.getDeclaringClass(), field.getType(), methodName); } else { // Otherwise, search for candidate factory methods whose names match either // the field name or the explicit bean name (if any). List<String> candidateMethodNames = new ArrayList<>(); candidateMethodNames.add(field.getName()); if (beanName != null) { candidateMethodNames.add(beanName); } factoryMethod = findTestBeanFactoryMethod(field.getDeclaringClass(), field.getType(), candidateMethodNames); } return new TestBeanOverrideHandler( field, ResolvableType.forField(field, testClass), beanName, testBean.contextName(), strategy, factoryMethod); } /** * Find a test bean factory {@link Method} for the given {@link Class}. * <p>Delegates to {@link #findTestBeanFactoryMethod(Class, Class, Collection)}. */ Method findTestBeanFactoryMethod(Class<?> clazz, Class<?> methodReturnType, String... methodNames) { return findTestBeanFactoryMethod(clazz, methodReturnType, List.of(methodNames)); } /** * Find a test bean factory {@link Method} for the given {@link Class}, which * meets the following criteria. * <ul> * <li>The method is static.</li> * <li>The method does not accept any arguments.</li> * <li>The method's return type matches the supplied {@code methodReturnType}.</li> * <li>The method's name is one of the supplied {@code methodNames}.</li> * </ul> * <p>This method traverses up the type hierarchy of the given class in search * of the factory method, beginning with the class itself and then searching * implemented interfaces and superclasses. If a factory method is not found * in the type hierarchy, this method will also search the enclosing class * hierarchy if the class is a nested class. * <p>If multiple factory methods are found that match the search criteria, * an exception is thrown. * @param clazz the class in which to search for the factory method * @param methodReturnType the return type for the factory method * @param methodNames a set of supported names for the factory method * @return the corresponding factory method * @throws IllegalStateException if a matching factory method cannot * be found or multiple methods match */ Method findTestBeanFactoryMethod(Class<?> clazz, Class<?> methodReturnType, Collection<String> methodNames) { Assert.notEmpty(methodNames, "At least one candidate method name is required"); Set<Method> methods = new LinkedHashSet<>(); Set<String> originalNames = new LinkedHashSet<>(methodNames); // Process fully-qualified method names first. for (String methodName : methodNames) { int indexOfHash = methodName.lastIndexOf('#'); if (indexOfHash != -1) { String className = methodName.substring(0, indexOfHash).trim(); Assert.hasText(className, () -> "No class name present in fully-qualified method name: " + methodName); String methodNameToUse = methodName.substring(indexOfHash + 1).trim(); Assert.hasText(methodNameToUse, () -> "No method name present in fully-qualified method name: " + methodName); Class<?> declaringClass; try { declaringClass = ClassUtils.forName(className, getClass().getClassLoader()); } catch (ClassNotFoundException | LinkageError ex) { throw new IllegalStateException( "Failed to load class for fully-qualified method name: " + methodName, ex); } Method externalMethod = ReflectionUtils.findMethod(declaringClass, methodNameToUse); Assert.state(externalMethod != null && Modifier.isStatic(externalMethod.getModifiers()) && methodReturnType.isAssignableFrom(externalMethod.getReturnType()), () -> "No static method found named %s in %s with return type %s".formatted( methodNameToUse, className, methodReturnType.getName())); methods.add(externalMethod); originalNames.remove(methodName); } } Set<String> supportedNames = new LinkedHashSet<>(originalNames); MethodFilter methodFilter = method -> (Modifier.isStatic(method.getModifiers()) && supportedNames.contains(method.getName()) && methodReturnType.isAssignableFrom(method.getReturnType())); findMethods(methods, clazz, methodFilter); String methodNamesDescription = supportedNames.stream() .map(name -> name + "()").collect(Collectors.joining(" or ")); Assert.state(!methods.isEmpty(), () -> "No static method found named %s in %s with return type %s".formatted( methodNamesDescription, clazz.getName(), methodReturnType.getName())); long uniqueMethodNameCount = methods.stream().map(Method::getName).distinct().count(); Assert.state(uniqueMethodNameCount == 1, () -> "Found %d competing static methods named %s in %s with return type %s".formatted( uniqueMethodNameCount, methodNamesDescription, clazz.getName(), methodReturnType.getName())); return methods.iterator().next(); } private static Set<Method> findMethods(Set<Method> methods, Class<?> clazz, MethodFilter methodFilter) { methods.addAll(MethodIntrospector.selectMethods(clazz, methodFilter)); if (methods.isEmpty() && TestContextAnnotationUtils.searchEnclosingClass(clazz)) { findMethods(methods, clazz.getEnclosingClass(), methodFilter); } return methods; } }

Loading feature diagnosis…

Source regions

Feature contributions

Feature contributions

FeatureValueContribution
(log-odds)
Halstead volumeHow much symbolic information the code contains. It combines the total number of operators and values with the number of distinct ones.7651.854−252.511
Source linesThe number of non-empty source lines.159+63.6
Byte entropyHow varied the bytes in the source text are. Repeated bytes give a lower value; a more even mixture gives a higher value.5.047−7.57

Intercept 8.87 + feature contributions = -187.612 (log-odds).
Score = sigmoid(-187.612) = 0.

Score0.257

Feature contributions

FeatureValueContribution
(log-odds)
Comment layout frequencyHow frequently the layout switches between lines containing only comments or whitespace and lines containing code.160−4.629
Indentation frequencyHow rapidly indentation changes from line to line.120+3.414
Number layout frequencyHow rapidly the number of numeric values per line changes through the code.120−2.333
Identifier layout (vertical)How rapidly the visual pattern of identifier characters changes from top to bottom.158.896+1.811
Identifier layout (horizontal)How rapidly the visual pattern of identifier characters changes from left to right. It measures spatial frequency, not the meaning of the names.113.956−1.162
Comment layout (horizontal)How rapidly the visual pattern of comment characters changes from left to right.113.981+1.035
Operator visual areaThe share of the code’s visual area occupied by operators such as +, =, and *.0.082+0.149

Intercept 0.07 + feature contributions = -1.645 (log-odds).
Score = sigmoid(-1.645) = 0.162.

Score0.082
Score49
Run 1

The code is well-structured with clear separation of concerns between handler creation and method discovery. It maintains a flat structure with only two levels of nesting, making it easy to follow. Intent is expressed through descriptive method names and comprehensive Javadoc documentation. The code handles edge cases (fully-qualified names, competing methods) with clear error messages. Reading flow is natural, though the fully-qualified method name parsing could be extracted into a helper method for better readability. The use of collections and streams is appropriate, and the action granularity is mostly one logical operation per line. Minor improvements could include breaking down the fully-qualified name processing loop and simplifying some conditional expressions.

Run 2

The code is generally well-structured and readable, with clear method separation and good Javadoc documentation. The createHandler method is straightforward with a logical flow. However, the findTestBeanFactoryMethod method is quite long and contains multiple responsibilities: parsing fully-qualified names, loading classes, validating methods, and searching hierarchies. While the logic is clear, the method could benefit from extraction of smaller helper methods. The nesting is moderate, particularly in the fully-qualified name parsing loop. Action granularity is mostly good with one operation per line, though some assertions are lengthy. The reading flow is natural despite the complexity. Overall, this is production-quality Spring framework code that maintains readability through documentation and clear naming, but the central method's length and mixing of concerns prevents a higher score.

Run 3

The code is reasonably organized with clear method separation and Javadoc, but readability suffers from complex conditional logic and long methods. `createHandler` has moderate nesting but acceptable structure. However, `findTestBeanFactoryMethod` is overly long, mixes multiple responsibilities (fully-qualified name parsing, filtering, validation), and has a nested try/catch with assertion lambdas. The `findMethods` helper is recursive and adds a disjoined flow. Several lines are dense, combining multiple operations or assertions. Naming is generally clear, and the Javadoc helps, but the heavy use of `Assert.state` with lambdas adds visual clutter. Overall, the intent is understandable but the implementation requires effort to follow.

Run 1

Code Structure: Well organized into handler creation, factory-method resolution, and a recursive search helper; the main resolver handles several responsibilities and could be split further. Nesting: Generally shallow, though fully qualified name processing combines a loop, conditional, and try/catch. Clarity of intent: Descriptive names, thorough documentation, and useful comments make the purpose clear; reflection predicates and method-uniqueness rules require closer inspection. Code length: Mostly justified by validation and diagnostics, but extensive documentation and repeated error-message formatting add bulk. Action granularity: Statements generally perform one action each; chained streams and compound assertions are somewhat dense. Reading flow: Progresses naturally from annotation validation through candidate selection, lookup, validation, and return, with consistent formatting. Long wrapped expressions mildly interrupt scanning.

Run 2

Code Structure: Responsibilities are logically separated into handler creation, factory-method lookup, and recursive searching, though the main lookup method handles several concerns. Nesting: Generally shallow, with moderate nesting in fully qualified name processing and exception handling. Clarity of intent: Descriptive identifiers, explanatory comments, detailed Javadoc, and informative error messages make the purpose clear. Some documented search constraints are not immediately evident from the implementation. Code length: Mostly justified, but the main lookup method and documentation are lengthy; extracting qualified-name resolution could simplify it. Action granularity: Statements generally perform one action per line, although compound predicates and stream chains require more attention. Reading flow: The progression from annotation validation to candidate selection, lookup, and handler construction is natural. Multiline assertions, reflection APIs, and mutable collections slightly interrupt the otherwise consistent flow.

Run 3

Code Structure: Well-organized processor with distinct handler creation, factory lookup, and recursive search helpers; the main lookup method handles several responsibilities and could be split further. Nesting: Generally shallow, though fully qualified name processing combines a loop, conditional, and try/catch. Clarity of intent: Descriptive names, detailed Javadoc, explanatory comments, and informative exception messages make the purpose clear; mutable search sets and distinct-name ambiguity checking require closer attention. Code length: Mostly justified by validation and documentation, but the central lookup method is relatively long. Action granularity: Statements generally perform one action each; compound predicates and chained stream operations introduce modest density. Reading flow: Validation, candidate selection, lookup, and result checking follow a natural sequence, with consistent formatting. Long assertions and multiline expressions occasionally interrupt scanning.