This is an automated email from the ASF dual-hosted git repository. nightowl888 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/lucenenet.git
commit 3b40315d7602fae74d185582d274b93adc1a58d4 Author: Shad Storhaug <[email protected]> AuthorDate: Mon Feb 3 13:01:42 2020 +0700 Lucene.Net.Analysis.TokenStream: Removed Reflection code that is used to force the end user to make TokenStream subclasses or their IncrementToken() method sealed (LUCENENET-642) --- src/Lucene.Net.Tests/TestAssertions.cs | 141 +++++++++++++++++---------------- src/Lucene.Net/Analysis/TokenStream.cs | 33 ++------ 2 files changed, 78 insertions(+), 96 deletions(-) diff --git a/src/Lucene.Net.Tests/TestAssertions.cs b/src/Lucene.Net.Tests/TestAssertions.cs index cf41b27..929354f 100644 --- a/src/Lucene.Net.Tests/TestAssertions.cs +++ b/src/Lucene.Net.Tests/TestAssertions.cs @@ -1,78 +1,81 @@ -using System.Diagnostics; +// LUCENENET: Rather than using AssertFinal() to run Reflection code at runtime, +// we are using a Roslyn code analyzer to ensure the rules are followed at compile time. -namespace Lucene.Net.Tests -{ - using NUnit.Framework; - using System; - /* - * 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. - */ +//using System.Diagnostics; - using LuceneTestCase = Lucene.Net.Util.LuceneTestCase; - using TokenStream = Lucene.Net.Analysis.TokenStream; +//namespace Lucene.Net.Tests +//{ +// using NUnit.Framework; +// using System; +// /* +// * 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. +// */ - /// <summary> - /// validate that assertions are enabled during tests - /// </summary> - public class TestAssertions : LuceneTestCase - { +// using LuceneTestCase = Lucene.Net.Util.LuceneTestCase; +// using TokenStream = Lucene.Net.Analysis.TokenStream; - internal class TestTokenStream1 : TokenStream - { - public sealed override bool IncrementToken() - { - return false; - } - } +// /// <summary> +// /// validate that assertions are enabled during tests +// /// </summary> +// public class TestAssertions : LuceneTestCase +// { - internal sealed class TestTokenStream2 : TokenStream - { - public override bool IncrementToken() - { - return false; - } - } +// internal class TestTokenStream1 : TokenStream +// { +// public sealed override bool IncrementToken() +// { +// return false; +// } +// } - internal class TestTokenStream3 : TokenStream - { - public override bool IncrementToken() - { - return false; - } - } +// internal sealed class TestTokenStream2 : TokenStream +// { +// public override bool IncrementToken() +// { +// return false; +// } +// } - [Test] - public virtual void TestTokenStreams() - { - // In Java, an AssertionError is expected: TokenStream implementation classes or at least their incrementToken() implementation must be final +// internal class TestTokenStream3 : TokenStream +// { +// public override bool IncrementToken() +// { +// return false; +// } +// } - var a = new TestTokenStream1(); - var b = new TestTokenStream2(); - var doFail = false; - try - { - var c = new TestTokenStream3(); - doFail = true; - } - catch (InvalidOperationException) - { - // expected - } - assertFalse("TestTokenStream3 should fail assertion", doFail); - } - } +// [Test] +// public virtual void TestTokenStreams() +// { +// // In Java, an AssertionError is expected: TokenStream implementation classes or at least their incrementToken() implementation must be final -} \ No newline at end of file +// var a = new TestTokenStream1(); +// var b = new TestTokenStream2(); +// var doFail = false; +// try +// { +// var c = new TestTokenStream3(); +// doFail = true; +// } +// catch (InvalidOperationException) +// { +// // expected +// } +// assertFalse("TestTokenStream3 should fail assertion", doFail); +// } +// } + +//} \ No newline at end of file diff --git a/src/Lucene.Net/Analysis/TokenStream.cs b/src/Lucene.Net/Analysis/TokenStream.cs index f9ec60f..d4c6085 100644 --- a/src/Lucene.Net/Analysis/TokenStream.cs +++ b/src/Lucene.Net/Analysis/TokenStream.cs @@ -85,7 +85,8 @@ namespace Lucene.Net.Analysis /// </summary> protected TokenStream() { - AssertFinal(); + // LUCENENET: Rather than using AssertFinal() to run Reflection code at runtime, + // we are using a Roslyn code analyzer to ensure the rules are followed at compile time. } /// <summary> @@ -94,7 +95,8 @@ namespace Lucene.Net.Analysis protected TokenStream(AttributeSource input) : base(input) { - AssertFinal(); + // LUCENENET: Rather than using AssertFinal() to run Reflection code at runtime, + // we are using a Roslyn code analyzer to ensure the rules are followed at compile time. } /// <summary> @@ -104,31 +106,8 @@ namespace Lucene.Net.Analysis protected TokenStream(AttributeFactory factory) : base(factory) { - AssertFinal(); - } - - private bool AssertFinal() - { - var type = this.GetType(); - - //if (!type.desiredAssertionStatus()) return true; // not supported in .NET - - var hasCompilerGeneratedAttribute = - type.GetTypeInfo().GetCustomAttributes(typeof (CompilerGeneratedAttribute), false).Any(); - var isAnonymousType = hasCompilerGeneratedAttribute && type.FullName.Contains("AnonymousType"); - - var method = type.GetMethod("IncrementToken", BindingFlags.Public | BindingFlags.Instance); - - if (!(isAnonymousType || type.GetTypeInfo().IsSealed || (method != null && method.IsFinal))) - { - // Original Java code throws an AssertException via Java's assert, we can't do this here - throw new InvalidOperationException("TokenStream implementation classes or at least their IncrementToken() implementation must be marked sealed"); - } - - // type.GetMethod returns null if the method doesn't exist. - // To emulate Lucene (which catches a NoSuchMethodException), - // we need to return false in that case. - return method != null; + // LUCENENET: Rather than using AssertFinal() to run Reflection code at runtime, + // we are using a Roslyn code analyzer to ensure the rules are followed at compile time. } /// <summary>
