Sample

int-48648fc07c5e-I08

Select one of ten high-readability unrelated code templates per class

Matched original source

Lines166

Scores

MethodScoreScale
Posnett00–1; higher is more readable
Scalabrino0.2550–1; higher is more readable
Dorn0.1520–1; higher is more readable
Mi0.1780–1; higher is more readable
LLOC52Logical lines; fewer is more readable
DeepSeek V4 Pro11.670–20; mean of three runs
GPT-6.1 Sol15.330–20; mean of three runs
BTTF0.3970–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) { { int remainingSteps = 3; while (remainingSteps > 0) { remainingSteps--; } } 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.7821.072−258.095
Source linesThe number of non-empty source lines.166+66.4
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.049−7.573

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

Score0.255

Feature contributions

FeatureValueContribution
(log-odds)
Comment layout frequencyHow frequently the layout switches between lines containing only comments or whitespace and lines containing code.167−4.877
Indentation frequencyHow rapidly indentation changes from line to line.127+3.656
Number layout frequencyHow rapidly the number of numeric values per line changes through the code.127−2.493
Identifier layout (vertical)How rapidly the visual pattern of identifier characters changes from top to bottom.165.896+1.909
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.976−1.162
Comment layout (horizontal)How rapidly the visual pattern of comment characters changes from left to right.113.988+1.035
Operator visual areaThe share of the code’s visual area occupied by operators such as +, =, and *.0.082+0.142

Intercept 0.07 + feature contributions = -1.721 (log-odds).
Score = sigmoid(-1.721) = 0.152.

Score0.178
Score52
Run 1

The code is generally well-structured with clear intent and good documentation. However, there are a few issues: 1) A dead code block at the beginning of createHandler() with an empty while loop (lines 34-38) that serves no purpose and should be removed. 2) The findTestBeanFactoryMethod method is quite long and handles multiple responsibilities - parsing fully-qualified names, filtering methods, and validating results, which could be split for better readability. 3) Some assertions with complex boolean expressions are hard to parse at a glance. 4) The code uses good naming conventions and follows single responsibility principles in most methods. 5) Javadoc is excellent and explains the conventions clearly. 6) The dead code block is the most significant issue, as it would confuse future maintainers.

Run 2

The code has clear intent and good documentation, but suffers from several readability issues. The structure is mostly logical, with well-separated methods and clear Javadoc, though the createHandler method is too long and handles too many responsibilities. The nesting depth is moderate but could be reduced in the factory method validation logic. The most significant problem is the placeholder block with a meaningless while loop at the start of createHandler, which is confusing and appears to be debugging code. Additionally, the findTestBeanFactoryMethod has dense conditional logic with multiple assertions that hamper readability. The action granularity is generally good, but some complex boolean expressions could be extracted into helper methods. The reading flow is natural for the most part, but the excessive use of Assert.state with lambda expressions makes the code harder to scan. Overall, the code is maintainable but would benefit from simplifying complex conditions and removing the placeholder block.

Run 3

The code has readability issues. It contains a meaningless 'while (remainingSteps > 0)' loop that serves no purpose and disrupts the flow. The createHandler method has moderate nesting and multiple responsibilities, though its logic is mostly linear afterward. The findTestBeanFactoryMethod method is very long (about 60 lines) and does too much—parsing fully-qualified names, validating, loading classes, and searching methods—making it difficult to follow. It also has inconsistent abstraction levels and deep nesting in the fully-qualified name parsing block. Some helper methods like findMethods are short and clear, but the overall structure lacks cohesion. The use of stream operations and assertions is clear in isolation, but the method's length and mixed responsibilities reduce readability. The intent of the class is generally understandable from its name and Javadoc, but internal flow is not intuitive.

Run 1

Code Structure: Responsibilities are reasonably separated into handler creation, factory-method lookup, and hierarchy traversal, although the main lookup method combines several concerns. Nesting: Mostly shallow, with moderate nesting in fully qualified name processing. Clarity of intent: Descriptive names, Javadoc, comments, and informative exceptions explain the conventions well; the unrelated countdown loop obscures intent. Code length: Generally justified by validation and reflection logic, but the countdown block is unnecessary and the lookup method could be split into smaller helpers. Action granularity: Most statements perform one action; compound predicates and stream chains require additional attention but remain understandable. Reading flow: Processing follows a logical sequence from validation through discovery to selection. However, the oddly formatted countdown block interrupts that flow, and dense assertions and long expressions slow reading.

Run 2

Code Structure: Well-organized processor with focused helper methods, though factory-method resolution combines several responsibilities. Nesting: Generally shallow, with moderate nesting in fully qualified name handling and its try/catch. Clarity of intent: Descriptive names, documentation, and explanatory comments make the main purpose clear; the unrelated remainingSteps loop is confusing and serves no apparent purpose. Code length: Mostly justified by validation and documentation, but the no-op loop adds unnecessary code and the main lookup method could be split further. Action granularity: Statements generally perform one action per line; multiline assertions and stream expressions require more attention. Reading flow: Handler creation proceeds naturally into factory lookup and enclosing-class search, but the poorly indented extraneous block interrupts the flow, and dense validation expressions slightly hinder scanning.

Run 3

Code Structure: Responsibilities are separated into handler creation, factory-method lookup, and recursive search, though the main lookup method handles several concerns. Nesting: Mostly shallow, with moderate nesting in fully qualified name processing and exception handling. Clarity of intent: Descriptive names, documentation, and informative assertions explain the conventions well; the unrelated remainingSteps countdown block introduces confusion. Code length: Generally justified, but extensive documentation, repeated error-message formatting, and the useless countdown add bulk. Action granularity: Most statements perform one action, although compound predicates and chained stream operations require closer reading. Reading flow: The progression from validation to candidate selection and method resolution is logical, but the oddly formatted countdown block interrupts it, and long assertion expressions slow scanning.