Skip to content

Commit

Permalink
Merge pull request #73 from CellProfiler/issues/72
Browse files Browse the repository at this point in the history
Fixes #72 prevent call to getField for random attributes
  • Loading branch information
LeeKamentsky committed Feb 22, 2016
2 parents e295de4 + a490782 commit 61712c1
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions javabridge/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,23 @@ def __init__(self, o):
fn = getattr(self, name)
fn.__doc__ = fn.__doc__ +"\n"+J.to_string(jmethod)
methods[name].append(method)
jfields = env.get_object_array_elements(
self.class_wrapper.getFields(self))
field_class = env.find_class("java/lang/reflect/Field")
method_id = env.get_method_id(
field_class, "getName", "()Ljava/lang/String;")
self.field_names = [
env.get_string_utf(env.call_method(o, method_id)) for o in jfields]
self.methods = methods

def __getattr__(self, name):
if name in ("o", "class_wrapper", "methods"):
if name in ("o", "class_wrapper", "methods", "field_names"):
raise AttributeError()
if not hasattr(self, "methods"):
if not hasattr(self, "methods") or not hasattr(self, "field_names"):
# not initialized
raise AttributeError()
if name not in self.field_names:
raise AttributeError()
try:
jfield = self.class_wrapper.getField(name)
except:
Expand All @@ -90,7 +99,7 @@ def __getattr__(self, name):
return result

def __setattr__(self, name, value):
if name in ("o", "class_wrapper", "methods") or \
if name in ("o", "class_wrapper", "methods", "field_names") or \
not hasattr(self, "methods"):
object.__setattr__(self, name, value)
return
Expand Down Expand Up @@ -192,13 +201,22 @@ def __init__(self, class_name):
fn = getattr(self, name)
fn.__doc__ = fn.__doc__ +"\n"+J.to_string(jmethod)
methods[name].append(method)
jfields = env.get_object_array_elements(self.klass.getFields(self))
field_class = env.find_class("java/lang/reflect/Field")
method_id = env.get_method_id(
field_class, "getName", "()Ljava/lang/String;")
self.field_names = [
env.get_string_utf(env.call_method(o, method_id)) for o in jfields]
self.methods = methods

def __getattr__(self, name):
if name in ("klass", "static_methods", "methods", "cname"):
if name in ("klass", "static_methods", "methods", "cname",
"field_names"):
raise AttributeError()
if not hasattr(self, "methods"):
if not hasattr(self, "methods") or not hasattr(self, "field_names"):
raise AttributeError()
if name not in self.field_names:
raise AttributeError("Cound not find field %s" % name)
try:
jfield = self.klass.getField(name)
except:
Expand All @@ -214,8 +232,8 @@ def __getattr__(self, name):
return result

def __setattr__(self, name, value):
if name in ("klass", "static_methods", "methods", "cname") or\
not hasattr(self, "methods"):
if name in ("klass", "static_methods", "methods", "cname",
"field_names") or not hasattr(self, "methods"):
object.__setattr__(self, name, value)
return
try:
Expand Down

0 comments on commit 61712c1

Please sign in to comment.