forked from apache/calcite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CALCITE-5008] Ignore synthetic and static methods in MetadataDef
According to https://www.jacoco.org/jacoco/trunk/doc/faq.html , Jacoco adds a synthetic static method called $jacocoInit() during instrumentation. MetadataDef constructor breaks if it encounters such method on the class given to it because it does not expect it. But it looks like it simply should not consider synthetic methods (as well as static methods) at all. - extract code for finding handler methods - reuse it in both places - add tests concerning synthetic methods
- Loading branch information
Showing
11 changed files
with
239 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
core/src/test/java/org/apache/calcite/rel/metadata/MetadataDefTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to you 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.apache.calcite.rel.metadata; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
|
||
/** | ||
* Test cases for {@link MetadataDef}. | ||
*/ | ||
class MetadataDefTest { | ||
@Test void staticMethodInHandlerIsIgnored() { | ||
assertDoesNotThrow( | ||
() -> MetadataDef.of(TestMetadata.class, MetadataHandlerWithStaticMethod.class) | ||
); | ||
} | ||
|
||
@Test void synthenticMethodInHandlerIsIgnored() { | ||
assertDoesNotThrow( | ||
() -> MetadataDef.of(TestMetadata.class, | ||
TestMetadataHandlers.handlerClassWithSyntheticMethod()) | ||
); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
core/src/test/java/org/apache/calcite/rel/metadata/MetadataHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to you 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.apache.calcite.rel.metadata; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.emptyArray; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
/** | ||
* Tests for {@link MetadataHandler}. | ||
*/ | ||
class MetadataHandlerTest { | ||
@Test void findsHandlerMethods() { | ||
Method[] methods = MetadataHandler.handlerMethods(TestMetadataHandler.class); | ||
|
||
assertThat(methods.length, is(1)); | ||
assertThat(methods[0].getName(), is("getTestMetadata")); | ||
} | ||
|
||
@Test void getDefMethodInHandlerIsIgnored() { | ||
Method[] methods = MetadataHandler.handlerMethods( | ||
MetadataHandlerWithGetDefMethodOnly.class); | ||
|
||
assertThat(methods, is(emptyArray())); | ||
} | ||
|
||
@Test void staticMethodInHandlerIsIgnored() { | ||
Method[] methods = MetadataHandler.handlerMethods(MetadataHandlerWithStaticMethod.class); | ||
|
||
assertThat(methods, is(emptyArray())); | ||
} | ||
|
||
@Test void synthenticMethodInHandlerIsIgnored() { | ||
Method[] methods = MetadataHandler.handlerMethods( | ||
TestMetadataHandlers.handlerClassWithSyntheticMethod()); | ||
|
||
assertThat(methods, is(emptyArray())); | ||
} | ||
|
||
/** | ||
* {@link MetadataHandler} which has a handler method. | ||
*/ | ||
interface TestMetadataHandler extends MetadataHandler<TestMetadata> { | ||
@SuppressWarnings("unused") | ||
TestMetadata getTestMetadata(); | ||
} | ||
|
||
/** | ||
* {@link MetadataHandler} which only has getDef() method. | ||
*/ | ||
interface MetadataHandlerWithGetDefMethodOnly extends MetadataHandler<TestMetadata> { | ||
MetadataDef<TestMetadata> getDef(); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
core/src/test/java/org/apache/calcite/rel/metadata/MetadataHandlerWithStaticMethod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to you 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.apache.calcite.rel.metadata; | ||
|
||
/** | ||
* A {@link MetadataHandler} having a static method. | ||
*/ | ||
interface MetadataHandlerWithStaticMethod extends MetadataHandler<TestMetadata> { | ||
@SuppressWarnings("unused") | ||
static void staticMethod() { | ||
// do nothing | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
core/src/test/java/org/apache/calcite/rel/metadata/TestMetadata.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to you 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.apache.calcite.rel.metadata; | ||
|
||
/** | ||
* A test {@link Metadata} interface. | ||
*/ | ||
interface TestMetadata extends Metadata { | ||
} |
54 changes: 54 additions & 0 deletions
54
core/src/test/java/org/apache/calcite/rel/metadata/TestMetadataHandlers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to you 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.apache.calcite.rel.metadata; | ||
|
||
import net.bytebuddy.ByteBuddy; | ||
import net.bytebuddy.description.modifier.SyntheticState; | ||
import net.bytebuddy.description.modifier.Visibility; | ||
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy; | ||
import net.bytebuddy.implementation.FixedValue; | ||
|
||
/** | ||
* Constructs {@link MetadataHandler} classes useful for tests. | ||
*/ | ||
class TestMetadataHandlers { | ||
/** | ||
* Returns a class representing an interface extending {@link MetadataHandler} and having | ||
* a synthetic method. | ||
* | ||
* @return MetadataHandler class with a synthetic method | ||
*/ | ||
static Class<? extends MetadataHandler<TestMetadata>> handlerClassWithSyntheticMethod() { | ||
return new ByteBuddy() | ||
.redefine(BlankMetadataHandler.class) | ||
.defineMethod("syntheticMethod", Void.class, SyntheticState.SYNTHETIC, Visibility.PUBLIC) | ||
.intercept(FixedValue.nullValue()) | ||
.make() | ||
.load(TestMetadataHandlers.class.getClassLoader(), ClassLoadingStrategy.Default.CHILD_FIRST) | ||
.getLoaded(); | ||
} | ||
|
||
private TestMetadataHandlers() { | ||
// prevent instantiation | ||
} | ||
|
||
/** | ||
* A blank {@link MetadataHandler} that is used as a base for adding a synthetic method. | ||
*/ | ||
private interface BlankMetadataHandler extends MetadataHandler<TestMetadata> { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters