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

review: fix: stackoverflow on AbstractTypingContext #1817

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ public CtTypeReference<?> adaptType(CtTypeInformation type) {
result.setParent(parent);
List<CtTypeReference<?>> actTypeArgs = new ArrayList<>(result.getActualTypeArguments());
for (int i = 0; i < actTypeArgs.size(); i++) {
actTypeArgs.set(i, adaptType(actTypeArgs.get(i)).clone());
CtTypeReference adaptedTypeArgs = adaptType(actTypeArgs.get(i));
// for some type argument we might return null to avoid recursive calls
if (adaptedTypeArgs != null) {
actTypeArgs.set(i, adaptedTypeArgs.clone());
}
}
result.setActualTypeArguments(actTypeArgs);
}
Expand All @@ -79,7 +83,7 @@ private CtTypeReference<?> adaptTypeParameterReference(CtTypeParameterReference
private CtTypeReference<?> adaptTypeParameterReferenceBoundingType(CtTypeParameterReference typeParamRef, CtTypeReference<?> boundingType) {
CtTypeParameterReference typeParamRefAdapted = typeParamRef.clone();
typeParamRefAdapted.setParent(typeParamRef.getParent());
typeParamRefAdapted.setBoundingType(boundingType.equals(boundingType.getFactory().Type().getDefaultBoundingType()) ? null : adaptType(boundingType));
typeParamRefAdapted.setBoundingType(boundingType.equals(boundingType.getFactory().Type().getDefaultBoundingType()) ? boundingType.getFactory().Type().getDefaultBoundingType() : adaptType(boundingType));
return typeParamRefAdapted;
}

Expand Down
22 changes: 22 additions & 0 deletions src/main/java/spoon/support/visitor/MethodTypingContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import spoon.reflect.reference.CtExecutableReference;
import spoon.reflect.reference.CtTypeParameterReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.reference.CtWildcardReference;

/**
* For the scope method or constructor and super type hierarchy of it's declaring type,
Expand Down Expand Up @@ -249,6 +250,27 @@ private boolean isSameMethodFormalTypeParameter(CtTypeParameter scopeParam, CtTy
return idxOfScopeBoundTypeParam == idxOfSuperBoundTypeParam;
}
}
if (scopeBound.getActualTypeArguments().size() != superBound.getActualTypeArguments().size()) {
return false;
}

for (int i = 0; i < scopeBound.getActualTypeArguments().size(); i++) {
CtTypeReference scopeBoundATArg = scopeBound.getActualTypeArguments().get(i);
CtTypeReference superBoundATArg = superBound.getActualTypeArguments().get(i);

if (scopeBoundATArg instanceof CtWildcardReference && superBoundATArg instanceof CtWildcardReference) {
CtWildcardReference scopeBoundWildcard = (CtWildcardReference) scopeBoundATArg;

// we are in a case where we compare Thing<?> with Thing<? super X>
// here we choose to not care about X to avoid recursive calls, for example in the case of:
// T extends Comparable<? super T>
if (scopeBoundWildcard.getBoundingType().equals(scopeMethod.getFactory().Type().getDefaultBoundingType())) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May this happen too?
T extends Comparable<? super List<? extends T>>
I guess it will end wit stackoverflow too.

Anyway your solution is good fast fix. My example is quite wild and it will need some time until somebody come to such problem again.

return true;
}
}
}


CtTypeReference<?> superBoundAdapted = adaptType(superBound);
if (superBoundAdapted == null) {
return false;
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/spoon/test/method/MethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.ModifierKind;
import spoon.reflect.factory.Factory;
import spoon.reflect.visitor.filter.NamedElementFilter;
import spoon.test.delete.testclasses.Adobada;
import spoon.test.method.testclasses.Tacos;

Expand Down Expand Up @@ -85,4 +86,18 @@ public void testGetAllMethods() throws Exception {
assertEquals(1, methods.stream().filter(method -> "foo".equals(method.getSimpleName())).count());
}

@Test
public void testGetAllMethodsAdaptingType() throws Exception {
// contract: AbstractTypingContext should not enter in recursive calls when resolving autoreferenced bounding type
// such as T extends Comparable<? super T>
Launcher l = new Launcher();
l.getEnvironment().setNoClasspath(true);
l.addInputResource("src/test/resources/noclasspath/spring/PropertyComparator.java");
l.buildModel();

CtType<?> propertyComparator = l.getModel().getElements(new NamedElementFilter<CtType>(CtType.class, "PropertyComparator")).get(0);
CtMethod method = propertyComparator.getMethodsByName("compare").get(0);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which call failed on StackOverflowError?
A) getMethodsByName
B) getSignature

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getMethodsByName

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So why did you removed this method call from the test??

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:D sorry I answer yesterday evening and actually I was wrong: it's getAllMethods which led to the bug. My test wasn't failing after all yesterday.

assertEquals("compare(T,T)", method.getSignature());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private void checkMethodOverride(BiFunction<CtMethod<?>, CtMethod<?>, Boolean> i

private void combine(List<CtMethod> value, int start, BiFunction<CtMethod<?>, CtMethod<?>, Boolean> isOverriding) {
CtMethod m1 = value.get(start);
if(start+1<value.size()) {
if(start+1 < value.size()) {
for (CtMethod m2 : value.subList(start+1, value.size())) {
if(m1.getDeclaringType().isSubtypeOf(m2.getDeclaringType().getReference())) {
checkOverride(m1, m2, isOverriding);
Expand Down
156 changes: 156 additions & 0 deletions src/test/resources/noclasspath/spring/PropertyComparator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you delete as much as possible from this file, so there remains only the code needed to reproduce error?
I do not see there what code caused problem. Or is some combination of all TypeMembers??

* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.beans.support;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.BeansException;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;

/**
* PropertyComparator performs a comparison of two beans,
* evaluating the specified bean property via a BeanWrapper.
*
* @author Juergen Hoeller
* @author Jean-Pierre Pawlak
* @since 19.05.2003
* @see org.springframework.beans.BeanWrapper
*/
public class PropertyComparator<T> implements Comparator<T> {

protected final Log logger = LogFactory.getLog(getClass());

private final SortDefinition sortDefinition;

private final BeanWrapperImpl beanWrapper = new BeanWrapperImpl(false);


/**
* Create a new PropertyComparator for the given SortDefinition.
* @see MutableSortDefinition
*/
public PropertyComparator(SortDefinition sortDefinition) {
this.sortDefinition = sortDefinition;
}

/**
* Create a PropertyComparator for the given settings.
* @param property the property to compare
* @param ignoreCase whether upper and lower case in String values should be ignored
* @param ascending whether to sort ascending (true) or descending (false)
*/
public PropertyComparator(String property, boolean ignoreCase, boolean ascending) {
this.sortDefinition = new MutableSortDefinition(property, ignoreCase, ascending);
}

/**
* Return the SortDefinition that this comparator uses.
*/
public final SortDefinition getSortDefinition() {
return this.sortDefinition;
}


@Override
@SuppressWarnings("unchecked")
public int compare(T o1, T o2) {
Object v1 = getPropertyValue(o1);
Object v2 = getPropertyValue(o2);
if (this.sortDefinition.isIgnoreCase() && (v1 instanceof String) && (v2 instanceof String)) {
v1 = ((String) v1).toLowerCase();
v2 = ((String) v2).toLowerCase();
}

int result;

// Put an object with null property at the end of the sort result.
try {
if (v1 != null) {
result = (v2 != null ? ((Comparable<Object>) v1).compareTo(v2) : -1);
}
else {
result = (v2 != null ? 1 : 0);
}
}
catch (RuntimeException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Could not sort objects [" + o1 + "] and [" + o2 + "]", ex);
}
return 0;
}

return (this.sortDefinition.isAscending() ? result : -result);
}

/**
* Get the SortDefinition's property value for the given object.
* @param obj the object to get the property value for
* @return the property value
*/
@Nullable
private Object getPropertyValue(Object obj) {
// If a nested property cannot be read, simply return null
// (similar to JSTL EL). If the property doesn't exist in the
// first place, let the exception through.
try {
this.beanWrapper.setWrappedInstance(obj);
return this.beanWrapper.getPropertyValue(this.sortDefinition.getProperty());
}
catch (BeansException ex) {
logger.info("PropertyComparator could not access property - treating as null for sorting", ex);
return null;
}
}


/**
* Sort the given List according to the given sort definition.
* <p>Note: Contained objects have to provide the given property
* in the form of a bean property, i.e. a getXXX method.
* @param source the input List
* @param sortDefinition the parameters to sort by
* @throws java.lang.IllegalArgumentException in case of a missing propertyName
*/
public static void sort(List<?> source, SortDefinition sortDefinition) throws BeansException {
if (StringUtils.hasText(sortDefinition.getProperty())) {
Collections.sort(source, new PropertyComparator<>(sortDefinition));
}
}

/**
* Sort the given source according to the given sort definition.
* <p>Note: Contained objects have to provide the given property
* in the form of a bean property, i.e. a getXXX method.
* @param source input source
* @param sortDefinition the parameters to sort by
* @throws java.lang.IllegalArgumentException in case of a missing propertyName
*/
public static void sort(Object[] source, SortDefinition sortDefinition) throws BeansException {
if (StringUtils.hasText(sortDefinition.getProperty())) {
Arrays.sort(source, new PropertyComparator<>(sortDefinition));
}
}

}