Skip to content

fix: intersection types with wildcard "?" are allowed as simplename #3568

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

Merged
merged 4 commits into from
Sep 10, 2020
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
12 changes: 12 additions & 0 deletions src/main/java/spoon/support/reflect/reference/CtReferenceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ private boolean checkAllParts(String[] simplenameParts) {
for (String simpleName:simplenameParts) {
//because arrays use e.g. int[] and @Number is used for instances of an object e.g. foo@1
simpleName = simpleName.replaceAll("\\[\\]|@", "");
if (isWildCard(simpleName)) {
// because in intersection types a typeReference sometimes has '?' as simplename
return false;
}
if (isKeyword(simpleName) || checkIdentifierChars(simpleName)) {
return true;
}
Expand All @@ -133,4 +137,12 @@ private static Collection<String> fillWithKeywords() {
"const", "native", "super", "while", "_")
.collect(Collectors.toCollection(HashSet::new));
}

/**
* checks if the input is a wildcard '?'. The method is not null safe.
* @return boolean true is input wildcard, false otherwise
*/
private boolean isWildCard(String name) {
return name.equals("?");
}
}
7 changes: 7 additions & 0 deletions src/test/java/spoon/generating/CorrectIdentifierTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.Ignore;
import org.junit.Test;
import spoon.FluentLauncher;
import spoon.Launcher;
import spoon.SpoonException;
import spoon.reflect.reference.CtLocalVariableReference;
Expand Down Expand Up @@ -69,4 +70,10 @@ public void correctIdentiferUtfChinese() {
CtLocalVariableReference<Object> localVariableRef = new Launcher().getFactory().createLocalVariableReference();
assertDoesNotThrow(() -> localVariableRef.setSimpleName("処理"));
}

@Test
public void intersectionTypeIdentifierTest() {
//contract: intersectionTypes can have simpleNames with '?' for wildcards.
assertDoesNotThrow(() -> new FluentLauncher().inputResource("./src/test/resources/identifier/InliningImplementationMatcher.java").buildModel());
}
}
85 changes: 85 additions & 0 deletions src/test/resources/identifier/InliningImplementationMatcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2014 - 2020 Rafael Winterhalter
*
* 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 net.bytebuddy.dynamic.scaffold.inline;

import net.bytebuddy.build.HashCodeAndEqualsPlugin;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.matcher.ElementMatchers;
import net.bytebuddy.matcher.LatentMatcher;

import static net.bytebuddy.matcher.ElementMatchers.*;

/**
* A latent method matcher that identifies methods to instrument when redefining or rebasing a type.
*/
@HashCodeAndEqualsPlugin.Enhance
public class InliningImplementationMatcher implements LatentMatcher<MethodDescription> {

/**
* A method matcher that matches any ignored method.
*/
private final LatentMatcher<? super MethodDescription> ignoredMethods;

/**
* A method matcher that matches any predefined method.
*/
private final ElementMatcher<? super MethodDescription> predefinedMethodSignatures;

/**
* Creates a new inline implementation matcher.
*
* @param ignoredMethods A method matcher that matches any ignored method.
* @param predefinedMethodSignatures A method matcher that matches any predefined method.
*/
protected InliningImplementationMatcher(LatentMatcher<? super MethodDescription> ignoredMethods,
ElementMatcher<? super MethodDescription> predefinedMethodSignatures) {
this.ignoredMethods = ignoredMethods;
this.predefinedMethodSignatures = predefinedMethodSignatures;
}

/**
* Creates a matcher where only overridable or declared methods are matched unless those are ignored. Methods that
* are declared by the target type are only matched if they are not ignored. Declared methods that are not found on the
* target type are always matched.
*
* @param ignoredMethods A method matcher that matches any ignored method.
* @param originalType The original type of the instrumentation before adding any user methods.
* @return A latent method matcher that identifies any method to instrument for a rebasement or redefinition.
*/
protected static LatentMatcher<MethodDescription> of(LatentMatcher<? super MethodDescription> ignoredMethods, TypeDescription originalType) {
ElementMatcher.Junction<MethodDescription> predefinedMethodSignatures = none();
for (MethodDescription methodDescription : originalType.getDeclaredMethods()) {
ElementMatcher.Junction<MethodDescription> signature = methodDescription.isConstructor()
? isConstructor()
: ElementMatchers.<MethodDescription>named(methodDescription.getName());
signature = signature.and(returns(methodDescription.getReturnType().asErasure()));
signature = signature.and(takesArguments(methodDescription.getParameters().asTypeList().asErasures()));
predefinedMethodSignatures = predefinedMethodSignatures.or(signature);
}
return new InliningImplementationMatcher(ignoredMethods, predefinedMethodSignatures);
}

/**
* {@inheritDoc}
*/
public ElementMatcher<? super MethodDescription> resolve(TypeDescription typeDescription) {
return (ElementMatcher<? super MethodDescription>) not(ignoredMethods.resolve(typeDescription))
.and(isVirtual().and(not(isFinal())).or(isDeclaredBy(typeDescription)))
.or(isDeclaredBy(typeDescription).and(not(predefinedMethodSignatures)));
}
}