コード例 #1
0
ファイル: JConstructor.cpp プロジェクト: alaineman/PowerGrid
/**
 * Gets the method id matching the given arguments.
 *
 */
jmethodID JConstructor::getMethodID( const JClass* jClass, const JArguments& arguments ) {

  /* We cache the jmethodID locally, so if we've already found it,
   * we don't need to go looking for it again.
   */
  if ( mMethodID ) {
    return mMethodID;
  }

  /* If we don't already have the jmethodID, we need to determine
   * the signature of this method.
   */

  /* We construct this signature with a void return type,
   * because the return type for constructors is void.
   */
  JSignature signature( *JVoid::staticGetJavaJniClass() ); 
  typedef list<JValue*> ValueList;
  ValueList args = arguments.asList();
  
  ValueList::iterator i = args.begin();
  ValueList::iterator end = args.end();

  for ( ; i != end; ++i ) {
    JValue* value = *i;
    signature << *value->getJavaJniClass();
  }

  string methodSignature = signature.toString();

  /* Now that we have the signature for the method, we could look
   * in a global cache for the jmethodID corresponding to this method,
   * but for now, we'll always find it.
   */
  JNIEnv* env = helper::attach();

  mMethodID = env->GetMethodID( jClass->getClass(), "<init>", methodSignature.c_str() );

  if ( mMethodID == NULL ) {
    string msg = string( "JConstructor::getMethodID\n" ) +
                 "Unable to find a constructor for " + jClass->getName() + "\n" +
                 "The signature is <" + methodSignature + ">";
		try
		{
			helper::catchAndThrow();
		}
		catch (JNIException e)
		{
			msg.append("\ncaused by:\n");
			msg.append(e.what());
		}
    throw JNIException( msg );
  }

  return mMethodID;
}