Apache Lucene open-source search software
LUCENE-2415: Use reflection instead of a shim class to access Jakarta Regex prefix
Uwe Schindlerc32bb32ce1b2
diff --git a/lucene/contrib/CHANGES.txt b/lucene/contrib/CHANGES.txtindex cd51f75f5cb..3a2a67446ff 100644--- a/lucene/contrib/CHANGES.txt+++ b/lucene/contrib/CHANGES.txt@@ -184,6 +184,9 @@ Other* LUCENE-1845: Updated bdb-je jar from version 3.3.69 to 3.3.93.(Simon Willnauer via Mike McCandless)+ * LUCENE-2415: Use reflection instead of a shim class to access Jakarta+ Regex prefix. (Uwe Schindler)+================== Release 2.9.2 / 3.0.1 2010-02-26 ====================New featuresdiff --git a/lucene/contrib/queries/src/java/org/apache/lucene/search/regex/JakartaRegexpCapabilities.java b/lucene/contrib/queries/src/java/org/apache/lucene/search/regex/JakartaRegexpCapabilities.javaindex 3e7c4298e19..44cc0e30a81 100644--- a/lucene/contrib/queries/src/java/org/apache/lucene/search/regex/JakartaRegexpCapabilities.java+++ b/lucene/contrib/queries/src/java/org/apache/lucene/search/regex/JakartaRegexpCapabilities.java@@ -18,7 +18,9 @@ package org.apache.lucene.search.regex;*/import org.apache.regexp.RE;-import org.apache.regexp.RegexpTunnel;+import org.apache.regexp.REProgram;+import java.lang.reflect.Field;+import java.lang.reflect.Method;/*** Implementation tying <a href="http://jakarta.apache.org/regexp">Jakarta@@ -29,6 +31,22 @@ import org.apache.regexp.RegexpTunnel;*/public class JakartaRegexpCapabilities implements RegexCapabilities {private RE regexp;++ private static Field prefixField;+ private static Method getPrefixMethod;+ static {+ try {+ getPrefixMethod = REProgram.class.getMethod("getPrefix");+ } catch (Exception e) {+ getPrefixMethod = null;+ }+ try {+ prefixField = REProgram.class.getDeclaredField("prefix");+ prefixField.setAccessible(true);+ } catch (Exception e) {+ prefixField = null;+ }+ }// Define the flags that are possible. Redefine them here// to avoid exposing the RE class to the caller.@@ -70,8 +88,20 @@ public class JakartaRegexpCapabilities implements RegexCapabilities {}public String prefix() {- char[] prefix = RegexpTunnel.getPrefix(regexp);- return prefix == null ? null : new String(prefix);+ try {+ final char[] prefix;+ if (getPrefixMethod != null) {+ prefix = (char[]) getPrefixMethod.invoke(regexp.getProgram());+ } else if (prefixField != null) {+ prefix = (char[]) prefixField.get(regexp.getProgram());+ } else {+ return null;+ }+ return prefix == null ? null : new String(prefix);+ } catch (Exception e) {+ // if we cannot get the prefix, return none+ return null;+ }}@Overridediff --git a/lucene/contrib/queries/src/java/org/apache/regexp/RegexpTunnel.java b/lucene/contrib/queries/src/java/org/apache/regexp/RegexpTunnel.javadeleted file mode 100644index 9e43b93a142..00000000000--- a/lucene/contrib/queries/src/java/org/apache/regexp/RegexpTunnel.java+++ /dev/null@@ -1,29 +0,0 @@-package org.apache.regexp;--/**- * 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.- */---/**- * This class exists as a gateway to access useful Jakarta Regexp package protected data.- */-public class RegexpTunnel {- public static char[] getPrefix(RE regexp) {- REProgram program = regexp.getProgram();- return program.prefix;- }-}diff --git a/lucene/contrib/queries/src/java/org/apache/regexp/package.html b/lucene/contrib/queries/src/java/org/apache/regexp/package.htmldeleted file mode 100644index 15b3b7a2b8e..00000000000--- a/lucene/contrib/queries/src/java/org/apache/regexp/package.html+++ /dev/null@@ -1,24 +0,0 @@-<!doctype html public "-//w3c//dtd html 4.0 transitional//en">-<!--- 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.--->-<html><head></head>-<body>-This package exists to allow access to useful package protected data within-Jakarta Regexp. This data has now been opened up with an accessor, but-an official release with that change has not been made to date.-</body>-</html>