agozhiy commented on a change in pull request #1639: DRILL-6642: Update 
protocol-buffers version
URL: https://github.com/apache/drill/pull/1639#discussion_r257180668
 
 

 ##########
 File path: 
exec/java-exec/src/main/java/org/apache/drill/exec/util/ProtobufPatcher.java
 ##########
 @@ -0,0 +1,140 @@
+/*
+ * 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.drill.exec.util;
+
+import javassist.CannotCompileException;
+import javassist.ClassPool;
+import javassist.CtClass;
+import javassist.CtMethod;
+import javassist.CtNewConstructor;
+import javassist.CtNewMethod;
+import javassist.Modifier;
+
+public class ProtobufPatcher {
+
+  private static final org.slf4j.Logger logger = 
org.slf4j.LoggerFactory.getLogger(ProtobufPatcher.class);
+
+  private static boolean patched;
+
+  public static synchronized void patch() {
+    if (!patched) {
+      try {
+        patchByteString();
+        patchGeneratedMessageLite();
+        patchGeneratedMessageLiteBuilder();
+        patched = true;
+      } catch (Throwable e) {
+        logger.warn("Unable to patch Protobuf.", e);
+      }
+    }
+  }
+
+
+  /**
+   * HBase client overrides methods from {@link 
com.google.protobuf.ByteString},
+   * that were made final in version 3.6.0 of protobuf.
+   * This method removes the final modifiers.
+   *
+   * @throws Exception
+   */
+  private static void patchByteString() throws Exception {
+    ClassPool cp = ClassPool.getDefault();
+    CtClass cc = cp.get("com.google.protobuf.ByteString");
+    removeFinal(cc.getDeclaredMethod("toString"));
+    removeFinal(cc.getDeclaredMethod("hashCode"));
+    removeFinal(cc.getDeclaredMethod("iterator"));
+    cc.toClass();
+  }
+
+  /**
+   * MapR-DB client extends {@link com.google.protobuf.GeneratedMessageLite} 
and overrides some methods,
+   * that were made final in version 3.6.0 of protobuf.
+   * This method removes the final modifiers.
+   *
+   * @throws Exception
+   */
+  private static void patchGeneratedMessageLite() throws Exception {
+    ClassPool cp = ClassPool.getDefault();
+    CtClass cc = cp.get("com.google.protobuf.GeneratedMessageLite");
+    removeFinal(cc.getDeclaredMethod("getParserForType"));
+    removeFinal(cc.getDeclaredMethod("isInitialized"));
+
+    // The method was removed, but it is used in com.mapr.fs.proto.Dbserver.
+    // Adding it back.
+    cc.addMethod(CtNewMethod.make("protected void makeExtensionsImmutable() { 
}", cc));
+
+    // A constructor with this signature was removed. Adding it back.
+    cc.addConstructor(CtNewConstructor.make("protected 
GeneratedMessageLite(com.google.protobuf.GeneratedMessageLite.Builder builder) 
{ }", cc));
+
+    // This single method was added instead of several abstract methods.
+    // MapR-DB client doesn't use it, but it was added in overridden equals() 
method.
+    // Adding default implementation.
+    CtMethod cm = cc.getDeclaredMethod("dynamicMethod", new CtClass[] {
+        cp.get("com.google.protobuf.GeneratedMessageLite$MethodToInvoke"),
+        cp.get("java.lang.Object"),
+        cp.get("java.lang.Object")});
+    addImplementation(cm, "if 
($1.equals(com.google.protobuf.GeneratedMessageLite.MethodToInvoke.GET_DEFAULT_INSTANCE))
 {" +
+                                  "  return this;" +
+                                  "} else {" +
+                                  "  return null;" +
 
 Review comment:
   Cannot throw exception here: this method is still used in several places, 
but in most cases the returned value is not used.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to