diff --git common/src/java/org/apache/hive/common/util/AnnotationUtils.java common/src/java/org/apache/hive/common/util/AnnotationUtils.java new file mode 100644 index 0000000..7e73c64 --- /dev/null +++ common/src/java/org/apache/hive/common/util/AnnotationUtils.java @@ -0,0 +1,38 @@ +/** + * 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.hive.common.util; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Method; + +public class AnnotationUtils { + + // to avoid https://bugs.openjdk.java.net/browse/JDK-7122142 + public static T getAnnotation(Class clazz, Class annotationClass) { + synchronized (annotationClass) { + return (T) clazz.getAnnotation(annotationClass); + } + } + + public static T getAnnotation(Method method, Class annotationClass) { + synchronized (annotationClass) { + return (T) method.getAnnotation(annotationClass); + } + } +} diff --git ql/src/java/org/apache/hadoop/hive/ql/exec/DDLTask.java ql/src/java/org/apache/hadoop/hive/ql/exec/DDLTask.java index ee074ea..1a88ca6 100644 --- ql/src/java/org/apache/hadoop/hive/ql/exec/DDLTask.java +++ ql/src/java/org/apache/hadoop/hive/ql/exec/DDLTask.java @@ -198,6 +198,7 @@ import org.apache.hadoop.io.IOUtils; import org.apache.hadoop.util.ReflectionUtils; import org.apache.hadoop.util.ToolRunner; +import org.apache.hive.common.util.AnnotationUtils; import org.stringtemplate.v4.ST; /** @@ -3212,7 +3213,7 @@ private int describeFunction(DescFunctionDesc descFunc) throws HiveException { funcClass = functionInfo.getFunctionClass(); } if (funcClass != null) { - desc = funcClass.getAnnotation(Description.class); + desc = AnnotationUtils.getAnnotation(funcClass, Description.class); } if (desc != null) { outStream.writeBytes(desc.value().replace("_FUNC_", funcName)); diff --git ql/src/java/org/apache/hadoop/hive/ql/exec/ExplainTask.java ql/src/java/org/apache/hadoop/hive/ql/exec/ExplainTask.java index 2c5b463..704679c 100644 --- ql/src/java/org/apache/hadoop/hive/ql/exec/ExplainTask.java +++ ql/src/java/org/apache/hadoop/hive/ql/exec/ExplainTask.java @@ -44,7 +44,6 @@ import org.apache.hadoop.hive.ql.Driver; import org.apache.hadoop.hive.ql.DriverContext; import org.apache.hadoop.hive.ql.hooks.ReadEntity; -import org.apache.hadoop.hive.ql.metadata.AuthorizationException; import org.apache.hadoop.hive.ql.metadata.Table; import org.apache.hadoop.hive.ql.optimizer.physical.StageIDsRearranger; import org.apache.hadoop.hive.ql.parse.BaseSemanticAnalyzer; @@ -58,6 +57,7 @@ import org.apache.hadoop.hive.ql.session.SessionState; import org.apache.hadoop.io.IOUtils; import org.apache.hadoop.util.StringUtils; +import org.apache.hive.common.util.AnnotationUtils; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -528,7 +528,7 @@ private JSONObject outputPlan(Serializable work, private JSONObject outputPlan(Serializable work, PrintStream out, boolean extended, boolean jsonOutput, int indent, String appendToHeader) throws Exception { // Check if work has an explain annotation - Annotation note = work.getClass().getAnnotation(Explain.class); + Annotation note = AnnotationUtils.getAnnotation(work.getClass(), Explain.class); String keyJSONObject = null; @@ -587,7 +587,7 @@ private JSONObject outputPlan(Serializable work, PrintStream out, for (Method m : methods) { int prop_indents = jsonOutput ? 0 : indent + 2; - note = m.getAnnotation(Explain.class); + note = AnnotationUtils.getAnnotation(m, Explain.class); if (note instanceof Explain) { Explain xpl_note = (Explain) note; diff --git ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionInfo.java ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionInfo.java index da2b3c7..e4a122e 100644 --- ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionInfo.java +++ ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionInfo.java @@ -25,6 +25,7 @@ import org.apache.hadoop.hive.ql.udf.generic.GenericUDTF; import org.apache.hadoop.hive.ql.udf.ptf.TableFunctionResolver; import org.apache.hadoop.hive.ql.udf.ptf.WindowingTableFunction; +import org.apache.hive.common.util.AnnotationUtils; /** * FunctionInfo. @@ -74,7 +75,8 @@ public FunctionInfo(String displayName, Class t { this.displayName = displayName; this.tableFunctionResolver = tFnCls; - PartitionTableFunctionDescription def = tableFunctionResolver.getAnnotation(PartitionTableFunctionDescription.class); + PartitionTableFunctionDescription def = AnnotationUtils.getAnnotation( + tableFunctionResolver, PartitionTableFunctionDescription.class); this.isNative = (def == null) ? false : def.isInternal(); this.isInternalTableFunction = isNative; } diff --git ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java index a80feb9..4f648e3 100644 --- ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java +++ ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java @@ -144,6 +144,7 @@ import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoUtils; import org.apache.hadoop.io.IOUtils; import org.apache.hadoop.util.ReflectionUtils; +import org.apache.hive.common.util.AnnotationUtils; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; @@ -1613,14 +1614,14 @@ public static boolean isDeterministic(GenericUDF genericUDF) { // the deterministic annotation declares return false; } - UDFType genericUDFType = genericUDF.getClass().getAnnotation(UDFType.class); + UDFType genericUDFType = AnnotationUtils.getAnnotation(genericUDF.getClass(), UDFType.class); if (genericUDFType != null && genericUDFType.deterministic() == false) { return false; } if (genericUDF instanceof GenericUDFBridge) { GenericUDFBridge bridge = (GenericUDFBridge) (genericUDF); - UDFType bridgeUDFType = bridge.getUdfClass().getAnnotation(UDFType.class); + UDFType bridgeUDFType = AnnotationUtils.getAnnotation(bridge.getUdfClass(), UDFType.class); if (bridgeUDFType != null && bridgeUDFType.deterministic() == false) { return false; } @@ -1638,14 +1639,14 @@ public static boolean isDeterministic(GenericUDF genericUDF) { * Returns whether a GenericUDF is stateful or not. */ public static boolean isStateful(GenericUDF genericUDF) { - UDFType genericUDFType = genericUDF.getClass().getAnnotation(UDFType.class); + UDFType genericUDFType = AnnotationUtils.getAnnotation(genericUDF.getClass(), UDFType.class); if (genericUDFType != null && genericUDFType.stateful()) { return true; } if (genericUDF instanceof GenericUDFBridge) { GenericUDFBridge bridge = (GenericUDFBridge) genericUDF; - UDFType bridgeUDFType = bridge.getUdfClass().getAnnotation(UDFType.class); + UDFType bridgeUDFType = AnnotationUtils.getAnnotation(bridge.getUdfClass(), UDFType.class); if (bridgeUDFType != null && bridgeUDFType.stateful()) { return true; } @@ -1894,7 +1895,8 @@ public static boolean impliesOrder(String functionName) { FunctionInfo info = getFunctionInfo(functionName); if (info != null) { if (info.isGenericUDF()) { - UDFType type = info.getGenericUDF().getClass().getAnnotation(UDFType.class); + UDFType type = + AnnotationUtils.getAnnotation(info.getGenericUDF().getClass(), UDFType.class); if (type != null) { return type.impliesOrder(); } @@ -1961,7 +1963,8 @@ public static boolean isRankingFunction(String name){ FunctionInfo info = getFunctionInfo(name); GenericUDAFResolver res = info.getGenericUDAFResolver(); if (res != null){ - WindowFunctionDescription desc = res.getClass().getAnnotation(WindowFunctionDescription.class); + WindowFunctionDescription desc = + AnnotationUtils.getAnnotation(res.getClass(), WindowFunctionDescription.class); if (desc != null){ return desc.rankingFunction(); } diff --git ql/src/java/org/apache/hadoop/hive/ql/exec/WindowFunctionInfo.java ql/src/java/org/apache/hadoop/hive/ql/exec/WindowFunctionInfo.java index 37139a7..461e3ec 100644 --- ql/src/java/org/apache/hadoop/hive/ql/exec/WindowFunctionInfo.java +++ ql/src/java/org/apache/hadoop/hive/ql/exec/WindowFunctionInfo.java @@ -19,6 +19,7 @@ package org.apache.hadoop.hive.ql.exec; import org.apache.hadoop.hive.ql.udf.generic.GenericUDAFResolver; +import org.apache.hive.common.util.AnnotationUtils; @SuppressWarnings("deprecation") public class WindowFunctionInfo implements CommonFunctionInfo @@ -33,7 +34,8 @@ assert fInfo.isGenericUDAF(); this.fInfo = fInfo; Class wfnCls = fInfo.getGenericUDAFResolver().getClass(); - WindowFunctionDescription def = wfnCls.getAnnotation(WindowFunctionDescription.class); + WindowFunctionDescription def = + AnnotationUtils.getAnnotation(wfnCls, WindowFunctionDescription.class); if ( def != null) { supportsWindow = def.supportsWindow(); diff --git ql/src/java/org/apache/hadoop/hive/ql/exec/vector/VectorExpressionDescriptor.java ql/src/java/org/apache/hadoop/hive/ql/exec/vector/VectorExpressionDescriptor.java index bfdd3ce..2d67b5b 100644 --- ql/src/java/org/apache/hadoop/hive/ql/exec/vector/VectorExpressionDescriptor.java +++ ql/src/java/org/apache/hadoop/hive/ql/exec/vector/VectorExpressionDescriptor.java @@ -20,6 +20,7 @@ import org.apache.hadoop.hive.ql.exec.vector.expressions.VectorExpression; import org.apache.hadoop.hive.ql.metadata.HiveException; +import org.apache.hive.common.util.AnnotationUtils; /** * Describes a vector expression and encapsulates the {@link Mode}, number of arguments, @@ -219,7 +220,8 @@ public String toString() { } public Class getVectorExpressionClass(Class udf, Descriptor descriptor) throws HiveException { - VectorizedExpressions annotation = udf.getAnnotation(VectorizedExpressions.class); + VectorizedExpressions annotation = + AnnotationUtils.getAnnotation(udf, VectorizedExpressions.class); if (annotation == null || annotation.value() == null) { return null; } diff --git ql/src/java/org/apache/hadoop/hive/ql/optimizer/StatsOptimizer.java ql/src/java/org/apache/hadoop/hive/ql/optimizer/StatsOptimizer.java index 68f8fd8..b129ddd 100644 --- ql/src/java/org/apache/hadoop/hive/ql/optimizer/StatsOptimizer.java +++ ql/src/java/org/apache/hadoop/hive/ql/optimizer/StatsOptimizer.java @@ -72,6 +72,7 @@ import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory; import org.apache.hadoop.hive.serde2.objectinspector.StandardStructObjectInspector; import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory; +import org.apache.hive.common.util.AnnotationUtils; import org.apache.thrift.TException; import com.google.common.collect.Lists; @@ -229,7 +230,7 @@ public Object process(Node nd, Stack stack, NodeProcessorCtx procCtx, // our stats for NDV is approx, not accurate. return null; } - if (aggr.getGenericUDAFName().equals(GenericUDAFSum.class.getAnnotation( + if (aggr.getGenericUDAFName().equals(AnnotationUtils.getAnnotation(GenericUDAFSum.class, Description.class).name())) { if(!(aggr.getParameters().get(0) instanceof ExprNodeConstantDesc)){ return null; @@ -243,7 +244,7 @@ public Object process(Node nd, Stack stack, NodeProcessorCtx procCtx, ois.add(PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector( PrimitiveCategory.DECIMAL)); } - else if (aggr.getGenericUDAFName().equals(GenericUDAFCount.class.getAnnotation( + else if (aggr.getGenericUDAFName().equals(AnnotationUtils.getAnnotation(GenericUDAFCount.class, Description.class).name())) { Long rowCnt = 0L; if ((aggr.getParameters().isEmpty() || aggr.getParameters().get(0) instanceof diff --git ql/src/java/org/apache/hadoop/hive/ql/plan/GroupByDesc.java ql/src/java/org/apache/hadoop/hive/ql/plan/GroupByDesc.java index 5aa3e82..4475b76 100644 --- ql/src/java/org/apache/hadoop/hive/ql/plan/GroupByDesc.java +++ ql/src/java/org/apache/hadoop/hive/ql/plan/GroupByDesc.java @@ -23,6 +23,7 @@ import org.apache.hadoop.hive.ql.udf.UDFType; import org.apache.hadoop.hive.ql.udf.generic.GenericUDAFEvaluator; +import org.apache.hive.common.util.AnnotationUtils; /** * GroupByDesc. @@ -228,7 +229,7 @@ public boolean isDistinctLike() { for (AggregationDesc ad : aggregators) { if (!ad.getDistinct()) { GenericUDAFEvaluator udafEval = ad.getGenericUDAFEvaluator(); - UDFType annot = udafEval.getClass().getAnnotation(UDFType.class); + UDFType annot = AnnotationUtils.getAnnotation(udafEval.getClass(), UDFType.class); if (annot == null || !annot.distinctLike()) { return false; } diff --git ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDAFEvaluator.java ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDAFEvaluator.java index 3bd97b0..4a5ceaa 100644 --- ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDAFEvaluator.java +++ ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDAFEvaluator.java @@ -26,6 +26,7 @@ import org.apache.hadoop.hive.ql.plan.ptf.WindowFrameDef; import org.apache.hadoop.hive.ql.udf.UDFType; import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector; +import org.apache.hive.common.util.AnnotationUtils; /** * A Generic User-defined aggregation function (GenericUDAF) for the use with @@ -49,7 +50,7 @@ public static boolean isEstimable(AggregationBuffer buffer) { if (buffer instanceof AbstractAggregationBuffer) { Class clazz = buffer.getClass(); - AggregationType annotation = clazz.getAnnotation(AggregationType.class); + AggregationType annotation = AnnotationUtils.getAnnotation(clazz, AggregationType.class); return annotation != null && annotation.estimable(); } return false;