Skip to content

Commit

Permalink
Merge pull request #318 from Thrameos/method_patch
Browse files Browse the repository at this point in the history
Work of jp_method bug
  • Loading branch information
marscher committed Nov 4, 2018
2 parents 7802a53 + 09ec730 commit 580285d
Show file tree
Hide file tree
Showing 15 changed files with 250 additions and 228 deletions.
15 changes: 8 additions & 7 deletions jpype/_jarray.py
Expand Up @@ -90,12 +90,13 @@ def __setattr__(self, attr, value):
else:
raise AttributeError("%s does not have field %s"%(self.__name__, attr), self)

def _isIterable(obj):
if isinstance(obj, collections.Sequence):
return True
if hasattr(obj,'__len__') and hasattr(obj,'__iter__'):
return True
return False

def _isIterable(obj):
if isinstance(obj, collections.Sequence):
return True
if hasattr(obj,'__len__') and hasattr(obj,'__iter__'):
return True
return False

def _jarrayInit(self, *args):
if len(args) == 2 and args[0] == _jclass._SPECIAL_CONSTRUCTOR_KEY:
Expand All @@ -106,7 +107,7 @@ def _jarrayInit(self, *args):
.format(len(args) + 1))
else:
values = None
if _isIterable(args[0]):
if _isIterable(args[0]):
sz = len(args[0])
values = args[0]
else:
Expand Down
19 changes: 5 additions & 14 deletions native/common/include/jp_class.h
Expand Up @@ -39,17 +39,8 @@ public :

JPField* getInstanceField(const string& name);
JPField* getStaticField(const string& name);
JPMethod* getMethod(const string& name);
vector<JPMethod*> getMethods() const
{
vector<JPMethod*> res;
res.reserve(m_Methods.size());
for (map<string, JPMethod*>::const_iterator cur = m_Methods.begin(); cur != m_Methods.end(); cur++)
{
res.push_back(cur->second);
}
return res;
}
JPMethod* getMethod(const string& name);
vector<JPMethod*> getMethods();

jclass getClass()
{
Expand Down Expand Up @@ -96,12 +87,12 @@ private :

private :
bool m_IsInterface;
JPClass* m_SuperClass;
vector<JPClass*> m_SuperInterfaces;
JPClass* m_SuperClass;
vector<JPClass*> m_SuperInterfaces;
map<string, JPField*> m_StaticFields;
map<string, JPField*> m_InstanceFields;
map<string, JPMethod*> m_Methods;
JPMethod* m_Constructors;
JPMethod* m_Constructors;
};


Expand Down
2 changes: 2 additions & 0 deletions native/common/include/jp_hostenv.h
Expand Up @@ -148,6 +148,8 @@ public :
virtual void printReferenceInfo(HostRef* obj) = 0;
virtual bool isByteBuffer(HostRef*) = 0;
virtual void getByteBufferPtr(HostRef*, char**, long&) = 0;

virtual const char* getTypeName(HostRef*) = 0;
};

#endif // _JPHOSTENV_H_
1 change: 1 addition & 0 deletions native/common/include/jp_jniutil.h
Expand Up @@ -45,6 +45,7 @@ namespace JPJni
JPTypeName getClassName(jobject obj);
jclass getClass(jobject obj);
jstring toString(jobject obj);
string toStringC(jobject obj);

/**
* java.lang.Class.isInterface()
Expand Down
31 changes: 18 additions & 13 deletions native/common/include/jp_method.h
Expand Up @@ -17,25 +17,35 @@
#ifndef _JPMETHOD_H_
#define _JPMETHOD_H_

#include <list>
class JPObject;

class JPMethod
{
public :
typedef std::list<JPMethodOverload*> OverloadList;

/**
* Create a new method based on class and a name;
*/
JPMethod(jclass clazz, const string& name, bool isConstructor);
virtual ~JPMethod();

private:
JPMethod(const JPMethod& method);
JPMethod& operator=(const JPMethod& method);

public :
const string& getName() const;
string getClassName() const;

void addOverload(JPClass* clazz, jobject mth);
void addOverloads(JPMethod* o);

bool hasStatic();
bool hasStatic()
{
return m_hasStatic;
}

size_t getCount()
{
return m_Overloads.size();
Expand All @@ -52,23 +62,18 @@ public :
string describe(string prefix);

string matchReport(vector<HostRef*>&);
void ensureOverloadOrderCache();

private :
JPMethodOverload* findOverload(vector<HostRef*>& arg, bool needStatic);
JPMethodOverload* findOverload(vector<HostRef*>& arg, bool searchStatic, bool searchInstance);
void ensureOverloadCache();
void dumpOverloads();

jclass m_Class;
string m_Name;
map<string, JPMethodOverload> m_Overloads;

struct OverloadData {
OverloadData(JPMethodOverload* o) : m_Overload(o) {}

JPMethodOverload* m_Overload;
std::vector<JPMethodOverload*> m_MoreSpecificOverloads;
};
std::vector<OverloadData> m_OverloadOrderCache;
OverloadList m_Overloads;
bool m_IsConstructor;
bool m_hasStatic;
bool m_Cached;

};

Expand Down
32 changes: 26 additions & 6 deletions native/common/include/jp_methodoverload.h
Expand Up @@ -17,12 +17,15 @@
#ifndef _JPMETHODOVERLOAD_H_
#define _JPMETHODOVERLOAD_H_

#include <list>
class JPObject;
class JPMethod;
class JPMethodOverload
{
public :
JPMethodOverload();
JPMethodOverload(const JPMethodOverload& o);
friend class JPMethod;
typedef std::list<JPMethodOverload*> OverloadList;

JPMethodOverload(JPClass* claz, jobject mth);

virtual ~JPMethodOverload();
Expand All @@ -35,9 +38,11 @@ public :

JPObject* invokeConstructor(jclass, vector<HostRef*>& arg);

public :
string getSignature();
private:
JPMethodOverload(const JPMethodOverload& o);
JPMethodOverload& operator=(const JPMethodOverload&);

public :
bool isStatic() const
{
return m_IsStatic;
Expand All @@ -63,13 +68,26 @@ public :
return (unsigned char)m_Arguments.size();
}

string getArgumentString();
string toString();

void packArgs(vector<jvalue>& v, vector<HostRef*>& arg, size_t skip);
bool isSameOverload(JPMethodOverload& o);
string matchReport(vector<HostRef*>& args);
bool isMoreSpecificThan(JPMethodOverload& other) const;

bool checkMoreSpecificThan(JPMethodOverload* other) const
{
for (OverloadList::const_iterator it = m_MoreSpecificOverloads.begin();
it != m_MoreSpecificOverloads.end();
++it)
{
if (other == *it)
return true;
}
return false;
}

private:
void packArgs(vector<jvalue>& v, vector<HostRef*>& arg, size_t skip);
void ensureTypeCache() const;
private :
JPClass* m_Class;
Expand All @@ -83,6 +101,8 @@ private :
bool m_IsConstructor;
mutable vector<JPType*> m_ArgumentsTypeCache;
mutable JPType* m_ReturnTypeCache;
OverloadList m_MoreSpecificOverloads;
bool m_Ordered;
};

#endif // _JPMETHODOVERLOAD_H_
5 changes: 5 additions & 0 deletions native/common/include/jp_object.h
Expand Up @@ -23,6 +23,11 @@ public :
JPObject(JPClass* clazz, jobject inst);
JPObject(JPTypeName& clazz, jobject inst);
virtual ~JPObject();
private:
JPObject(const JPObject&);
JPObject& operator=(const JPObject&);

public:


JPClass* getClass()
Expand Down
12 changes: 11 additions & 1 deletion native/common/jp_class.cpp
Expand Up @@ -201,10 +201,20 @@ JPMethod* JPClass::getMethod(const string& name)
{
return NULL;
}

return it->second;
}

vector<JPMethod*> JPClass::getMethods()
{
vector<JPMethod*> res;
res.reserve(m_Methods.size());
for (map<string, JPMethod*>::const_iterator cur = m_Methods.begin(); cur != m_Methods.end(); cur++)
{
res.push_back(cur->second);
}
return res;
}

HostRef* JPClass::getStaticAttribute(const string& name)
{
// static fields
Expand Down
8 changes: 7 additions & 1 deletion native/common/jp_jniutil.cpp
Expand Up @@ -209,7 +209,6 @@ string asciiFromJava(jstring str)
}

frame.ReleaseStringUTFChars(str, cstr);

return res;
}

Expand Down Expand Up @@ -258,6 +257,13 @@ jstring toString(jobject o)
return str;
}

string toStringC(jobject o)
{
JPJavaFrame frame;
jstring jname = (jstring)frame.CallObjectMethod(o, toStringID);
return asciiFromJava(jname);
}

static string convertToSimpleName(jclass c)
{
JPJavaFrame frame;
Expand Down

0 comments on commit 580285d

Please sign in to comment.