Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  // Copyright 2013 Google Inc. All Rights Reserved.
  //
  // Licensed 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 com.google.api.ads.adwords.axis.utility.extension.util;
 
 
 
Reflection utilities class.

Author(s):
Julian Toledo
 
 public class ReflectionUtil {
 
   static Logger log = LoggerFactory.getLogger(ReflectionUtil.class);
 
   // Hash for storing Class.getMethod results to improve performance
   private static HashMap<StringMethodmethodsCache = Maps.newHashMap();

  
A reflection Invoke method with Retries for SocketException, InternalApiError and RateExceededError with getRetryAfterSeconds sleep.

Parameters:
methodName
obj the object the underlying method is invoked from
objClass the class of the object the underlying method is invoked from
invokeMaxCount number of maximum calls
Returns:
Object, result from reflexion call
Throws:
java.rmi.RemoteException for communication-related exceptions
 
   public static Object invokeCount(String methodNameObject objClass<?> objClass,
       int invokeMaxCountthrows RemoteException {
     return invokeCount(methodNameobjobjClassnullnullinvokeMaxCount);
   }

  
A reflection Invoke method with Retries for SocketException, InternalApiError and RateExceededError with getRetryAfterSeconds sleep.

Parameters:
methodName
obj the object the underlying method is invoked from
objClass the class of the object the underlying method is invoked from
args the arguments used for the method call
invokeMaxCount number of maximum calls
Returns:
Object, result from reflexion call
Throws:
java.rmi.RemoteException for communication-related exceptions
 
   public static Object invokeCount(String methodNameObject obj,  Class<?> objClassObject args,
       int invokeMaxCountthrows RemoteException {
     return invokeCount(methodNameobjobjClassargsargs.getClass(), invokeMaxCount);
   }

  
A reflection Invoke method with Retries for SocketException, InternalApiError and RateExceededError with getRetryAfterSeconds sleep.

Parameters:
methodName
obj the object the underlying method is invoked from
objClass the class of the object the underlying method is invoked from
args the arguments used for the method call
argsClass the arguments Class used for the method call
invokeMaxCount the max number of calls
Returns:
Object, result from reflection call
Throws:
java.rmi.RemoteException for communication-related exceptions
 
   public static Object invokeCount(String methodNameObject objClass<?> objClass,
       Object argsClass<?> argsClassint invokeMaxCountthrows RemoteException {
     Method method;
 
     String className;
     // obj is null for Static methods
     if (obj == null) {
       className = objClass.getCanonicalName();
     } else {
       className = obj.getClass().getCanonicalName();
    }
    if (objClass != null) {
      className = objClass.getCanonicalName();
    } else {
      objClass = obj.getClass();
    }
    RemoteException remoteException = new RemoteException(
        "Error during reflection for " + className + "." + methodName);
    int invokeCount = 0;
    Boolean retry = true;
    while (retry && invokeCount < invokeMaxCount) {
      retry = false;
      invokeCount++;
      try {
        if (args != null) {
          // Searches the Hash for cached Methods using Class name, method name and args
          if (.containsKey(
              className + methodName + argsClass.getCanonicalName())) {
            method = .get(
                className + methodName + argsClass.getCanonicalName());
          } else {
            method = objClass.getMethod(methodNameargsClass);
            .put(
                objClass.getCanonicalName() + methodName + argsClass.getCanonicalName(),
                method);
          }
          return method.invoke(objargs);
        } else {
          // Searches the Hash for cached Methods using Class name, method name and args
          if (.containsKey(obj.getClass().getCanonicalName() + methodName)) {
            method = .get(obj.getClass().getCanonicalName() + methodName);
          } else {
            method = obj.getClass().getMethod(methodName);
            .put(obj.getClass().getCanonicalName() + methodNamemethod);
          }
          return method.invoke(obj);
        }
      } catch (Exception e) {
        remoteException = UtilityLibraryException.getUnwrapApiExceptions(
            e"Error during reflection for " + className + "." + methodName);
        if (remoteException.getCause() != null &&
            remoteException.getCause().getClass().isAssignableFrom(SocketException.class)) {
          // Retrying if SocketException
          retry = true;
          .warn("SocketException error, retrying last operation: "
              + className + "." + methodName
              + " (" + invokeCount  + "/" + invokeMaxCount + ")");
        } else if (remoteException.getClass().equals(ApiException.class)) {
          ApiException apiException = (ApiExceptionremoteException;
          ApiError[] errorArray = apiException.getErrors();
          for (ApiError apiError : errorArray) {
            if (apiError instanceof RateExceededError) {
              // Retrying if RateExceededError
              retry = true;
              try {
                int seconds = ((RateExceededErrorapiError).getRetryAfterSeconds();
                .warn("RateExceededError error, retrying last operation: "
                    + className + "." + methodName
                    + " (" + invokeCount + "/" + invokeMaxCount + ")"
                    + " waiting seconds: " + seconds);
                // wait the amount of secconds the server asked
                Thread.sleep(seconds * 1000);
              } catch (InterruptedException interruptedException) {
                throw remoteException;
              }
            }
            if (apiError instanceof InternalApiError) {
              // Retrying if InternalApiError
              retry = true;
              .warn("InternalApiError error, retrying last operation: "
                  + className + "." + methodName
                  + " (" + invokeCount + "/" + invokeMaxCount + ")");
            }
          }
        }
      }
    }
    throw remoteException;
  }
New to GrepCode? Check out our FAQ X