Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid duplicate addition of void.class in ClassUtils static initialization #25128

Closed
cherishsince opened this issue May 26, 2020 · 3 comments
Closed
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: feedback-provided Feedback has been provided type: task A general task
Milestone

Comments

@cherishsince
Copy link

static {
		primitiveWrapperTypeMap.put(Boolean.class, boolean.class);
		primitiveWrapperTypeMap.put(Byte.class, byte.class);
		// ...
               // Add 'void.class' repeatedly
		primitiveTypes.add(void.class);
		// ...
	}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label May 26, 2020
@bclozel bclozel added the status: waiting-for-feedback We need additional information before we can continue label May 26, 2020
@bclozel
Copy link
Member

bclozel commented May 26, 2020

I don’t understand the goal of this issue.
Is there a bug somewhere, in this case do you have a sample showing the problem?
Is this an improvement proposal - in this case could you send a PR to the project or give more context about the improvement?

Thanks

@cherishsince
Copy link
Author

cherishsince commented May 26, 2020

	static {
		primitiveWrapperTypeMap.put(Boolean.class, boolean.class);
		primitiveWrapperTypeMap.put(Byte.class, byte.class);
		primitiveWrapperTypeMap.put(Character.class, char.class);
		primitiveWrapperTypeMap.put(Double.class, double.class);
		primitiveWrapperTypeMap.put(Float.class, float.class);
		primitiveWrapperTypeMap.put(Integer.class, int.class);
		primitiveWrapperTypeMap.put(Long.class, long.class);
		primitiveWrapperTypeMap.put(Short.class, short.class);
		primitiveWrapperTypeMap.put(Void.class, void.class);

		// Map entry iteration is less expensive to initialize than forEach with lambdas
		for (Map.Entry<Class<?>, Class<?>> entry : primitiveWrapperTypeMap.entrySet()) {
			primitiveTypeToWrapperMap.put(entry.getValue(), entry.getKey());
			registerCommonClasses(entry.getKey());
		}

		Set<Class<?>> primitiveTypes = new HashSet<>(32);
		primitiveTypes.addAll(primitiveWrapperTypeMap.values());
		Collections.addAll(primitiveTypes, boolean[].class, byte[].class, char[].class,
				double[].class, float[].class, int[].class, long[].class, short[].class);
		primitiveTypes.add(void.class);
		for (Class<?> primitiveType : primitiveTypes) {
			primitiveTypeNameMap.put(primitiveType.getName(), primitiveType);
		}

		registerCommonClasses(Boolean[].class, Byte[].class, Character[].class, Double[].class,
				Float[].class, Integer[].class, Long[].class, Short[].class);
		registerCommonClasses(Number.class, Number[].class, String.class, String[].class,
				Class.class, Class[].class, Object.class, Object[].class);
		registerCommonClasses(Throwable.class, Exception.class, RuntimeException.class,
				Error.class, StackTraceElement.class, StackTraceElement[].class);
		registerCommonClasses(Enum.class, Iterable.class, Iterator.class, Enumeration.class,
				Collection.class, List.class, Set.class, Map.class, Map.Entry.class, Optional.class);

		Class<?>[] javaLanguageInterfaceArray = {Serializable.class, Externalizable.class,
				Closeable.class, AutoCloseable.class, Cloneable.class, Comparable.class};
		registerCommonClasses(javaLanguageInterfaceArray);
		javaLanguageInterfaces = new HashSet<>(Arrays.asList(javaLanguageInterfaceArray));
	}

Optimization suggestions

'primitiveWrapperTypeMap' has been added void.class So primitiveTypes.add(void.class) is a redundant operation.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels May 26, 2020
@sbrannen sbrannen added the in: core Issues in core modules (aop, beans, core, context, expression) label May 26, 2020
@sbrannen
Copy link
Member

Thanks for pointing this out.

I'll tidy it up.

@sbrannen sbrannen added type: task A general task and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels May 26, 2020
@sbrannen sbrannen self-assigned this May 26, 2020
@sbrannen sbrannen added this to the 5.3 M1 milestone May 26, 2020
@sbrannen sbrannen changed the title 'ClassUtils' Add 'void.class' repeatedly Avoid duplicate addition of void.class in ClassUtils static initialization May 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: feedback-provided Feedback has been provided type: task A general task
Projects
None yet
Development

No branches or pull requests

4 participants