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

fix: protect usage of getDeclaringType #2039

Merged
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -16,19 +16,20 @@
*/
package spoon.reflect.visitor.filter;

import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Set;

import spoon.SpoonException;
import spoon.reflect.declaration.CtExecutable;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.CtTypeMember;
import spoon.reflect.reference.CtExecutableReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.Filter;

import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Set;

/**
* This filter matches all the {@link CtExecutableReference} referencing defined one or more {@link CtExecutable}s.
*/
Expand Down Expand Up @@ -85,7 +86,8 @@ public boolean matches(CtExecutableReference<?> execRef) {
return executables.containsKey(execRef.getDeclaration());
} else {
//reference to constructor or method
if (typeQualifiedNames.contains(execRef.getDeclaringType().getQualifiedName())) {
CtTypeReference<?> declaringType = execRef.getDeclaringType();
if (declaringType != null && typeQualifiedNames.contains(declaringType.getQualifiedName())) {
if (CtExecutableReference.CONSTRUCTOR_NAME.equals(execRef.getSimpleName()) || methodNames.contains(execRef.getSimpleName())) {
return executables.containsKey(execRef.getDeclaration());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ public boolean isOverriding(CtExecutableReference<?> executable) {
if (!isSame) {
return false;
}
if (!getDeclaringType().isSubtypeOf(executable.getDeclaringType())) {
CtTypeReference<?> declaringType = getDeclaringType();
if (declaringType == null || !declaringType.isSubtypeOf(executable.getDeclaringType())) {
return false;
}
return true;
Expand Down Expand Up @@ -330,8 +331,12 @@ protected AnnotatedElement getActualAnnotatedElement() {
public Method getActualMethod() {
List<CtTypeReference<?>> parameters = this.getParameters();

CtTypeReference<?> declaringType = getDeclaringType();
if (declaringType == null) {
return null;
}
method_loop:
for (Method m : getDeclaringType().getActualClass().getDeclaredMethods()) {
for (Method m : declaringType.getActualClass().getDeclaredMethods()) {
if (!m.getDeclaringClass().isSynthetic() && m.isSynthetic()) {
continue;
}
Expand All @@ -357,8 +362,12 @@ public Method getActualMethod() {
public Constructor<?> getActualConstructor() {
List<CtTypeReference<?>> parameters = this.getParameters();

CtTypeReference<?> declaringType = getDeclaringType();
if (declaringType == null) {
return null;
}
constructor_loop:
for (Constructor<?> c : getDeclaringType().getActualClass().getDeclaredConstructors()) {
for (Constructor<?> c : declaringType.getActualClass().getDeclaredConstructors()) {
if (c.getParameterTypes().length != parameters.size()) {
continue;
}
Expand Down Expand Up @@ -421,8 +430,12 @@ public Set<ModifierKind> getModifiers() {

@Override
public CtExecutableReference<?> getOverridingExecutable() {
CtTypeReference<?> st = getDeclaringType().getSuperclass();
CtTypeReference<Object> objectType = getFactory().Type().OBJECT;
CtTypeReference<?> declaringType = getDeclaringType();
if (declaringType == null) {
return getOverloadedExecutable(objectType, objectType);
}
CtTypeReference<?> st = declaringType.getSuperclass();
if (st == null) {
return getOverloadedExecutable(objectType, objectType);
}
Expand Down