1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
|
{
This file is part of the Free Pascal run time library.
Copyright (c) 2011 by Jonas Maebe,
members of the Free Pascal development team.
This file implements support infrastructure for procvars under the JVM
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
constructor FpcBaseProcVarType.create(inst: jlobject; const methodName: unicodestring; const argTypes: array of JLClass);
begin
method.data:=inst;
setFpcBaseProcVarTypeBySignature(methodName,argtypes);
end;
constructor FpcBaseProcVarType.create(const meth: tmethod);
begin
method:=meth;
end;
procedure FpcBaseProcVarType.setFpcBaseProcVarTypeBySignature(const methodName: unicodestring; const argTypes: array of JLClass);
var
owningClass: JLClass;
begin
{ class method or instance method }
if method.data is JLClass then
owningClass:=JLClass(method.data)
else
owningClass:=method.data.getClass;
method.code:=nil;
{ getDeclaredMethod does not search superclasses -> manually traverse
until found. If we don't find it anywhere, we'll traverse up to the
parent of java.lang.Object = null and throw a NullPointerException }
repeat
try
method.code:=owningClass.getDeclaredMethod(methodName,argTypes);
except
on JLNoSuchMethodException do
owningClass:=owningClass.getSuperClass;
end;
until assigned(method.code);
{ required to enable calling private methods in one class from another
class -- can cause security exceptions if the security manager doesn't
allow this though... }
if not method.code.isAccessible then
method.code.setAccessible(true);
end;
function FpcBaseProcVarType.getClassProcArgs(const args: array of jlobject): TJLObjectDynArray;
var
arglen: longint;
begin
{ add the self pointer as first argument (Java class methods don't take an
implicit self parameters, Pascal ones do) }
arglen:=length(args);
setlength(result,arglen+1);
JLSystem.ArrayCopy(JLObject(@args),0,JLObject(result),1,arglen);
result[0]:=method.data;
end;
procedure FpcBaseProcVarType.fpcDeepCopy(result: FpcBaseProcVarType);
begin
result.method:=method;
end;
function FpcBaseProcVarType.clone: JLObject;
var
field: JLRField;
newmethodrec: tmethod;
begin
result:=inherited;
{ replace the method record pointer (the inherited clone will have copied
it, and there is no way we can change it using Pascal code since it's
not a pointer at the Pascal level) }
newmethodrec:=method;
field:=getClass.getField('method');
{ doesn't matter that it's a local variable, everything is garbage
collected }
field.&set(result,JLObject(@newmethodrec));
end;
procedure FpcBaseProcVarType.invokeProc(const args: array of jlobject);
begin
{ caching the length would be faster, but that would have to be done
in a synchronised way. Doing it at construction time and in fpcDeepCopy/
clone is not enough, because the method field can be manipulated
directly }
try
if length(method.code.getParameterTypes)=length(args) then
method.code.invoke(method.data,args)
else
method.code.invoke(method.data,getClassProcArgs(args));
except
on e: JLRInvocationTargetException do
raise e.getCause
end;
end;
function FpcBaseProcVarType.invokeBooleanFunc(const args: array of jlobject): jboolean;
begin
try
if length(method.code.getParameterTypes)=length(args) then
result:=JLBoolean(method.code.invoke(method.data,args)).booleanValue
else
result:=JLBoolean(method.code.invoke(method.data,getClassProcArgs(args))).booleanValue
except
on e: JLRInvocationTargetException do
raise e.getCause
end;
end;
function FpcBaseProcVarType.invokeCharFunc(const args: array of jlobject): jchar;
begin
try
if length(method.code.getParameterTypes)=length(args) then
result:=JLCharacter(method.code.invoke(method.data,args)).charValue
else
result:=JLCharacter(method.code.invoke(method.data,getClassProcArgs(args))).charValue;
except
on e: JLRInvocationTargetException do
raise e.getCause
end;
end;
function FpcBaseProcVarType.invokeByteFunc(const args: array of jlobject): jbyte;
begin
try
if length(method.code.getParameterTypes)=length(args) then
result:=JLByte(method.code.invoke(method.data,args)).byteValue
else
result:=JLByte(method.code.invoke(method.data,getClassProcArgs(args))).byteValue
except
on e: JLRInvocationTargetException do
raise e.getCause
end;
end;
function FpcBaseProcVarType.invokeShortFunc(const args: array of jlobject): jshort;
begin
try
if length(method.code.getParameterTypes)=length(args) then
result:=JLShort(method.code.invoke(method.data,args)).shortValue
else
result:=JLShort(method.code.invoke(method.data,getClassProcArgs(args))).shortValue
except
on e: JLRInvocationTargetException do
raise e.getCause
end;
end;
function FpcBaseProcVarType.invokeIntFunc(const args: array of jlobject): jint;
begin
try
if length(method.code.getParameterTypes)=length(args) then
result:=JLInteger(method.code.invoke(method.data,args)).intValue
else
result:=JLInteger(method.code.invoke(method.data,getClassProcArgs(args))).intValue
except
on e: JLRInvocationTargetException do
raise e.getCause
end;
end;
function FpcBaseProcVarType.invokeLongFunc(const args: array of jlobject): jlong;
begin
try
if length(method.code.getParameterTypes)=length(args) then
result:=JLLong(method.code.invoke(method.data,args)).longValue
else
result:=JLLong(method.code.invoke(method.data,getClassProcArgs(args))).longValue;
except
on e: JLRInvocationTargetException do
raise e.getCause
end;
end;
function FpcBaseProcVarType.invokeSingleFunc(const args: array of jlobject): jfloat;
begin
try
if length(method.code.getParameterTypes)=length(args) then
result:=JLFloat(method.code.invoke(method.data,args)).floatValue
else
result:=JLFloat(method.code.invoke(method.data,getClassProcArgs(args))).floatValue
except
on e: JLRInvocationTargetException do
raise e.getCause
end;
end;
function FpcBaseProcVarType.invokeDoubleFunc(const args: array of jlobject): jdouble;
begin
try
if length(method.code.getParameterTypes)=length(args) then
result:=JLDouble(method.code.invoke(method.data,args)).doubleValue
else
result:=JLDouble(method.code.invoke(method.data,getClassProcArgs(args))).doubleValue
except
on e: JLRInvocationTargetException do
raise e.getCause
end;
end;
function FpcBaseProcVarType.invokeObjectFunc(const args: array of jlobject): jlobject;
begin
try
if length(method.code.getParameterTypes)=length(args) then
result:=method.code.invoke(method.data,args)
else
result:=method.code.invoke(method.data,getClassProcArgs(args))
except
on e: JLRInvocationTargetException do
raise e.getCause
end;
end;
function FpcBaseNestedProcVarType.getNestedArgs(const args: array of jlobject): TJLObjectDynArray;
var
arglen: longint;
begin
{ add the parentfp struct pointer as last argument (delphi nested cc
"calling convention") }
arglen:=length(args);
setlength(result,arglen+1);
JLSystem.ArrayCopy(JLObject(@args),0,JLObject(result),0,arglen);
result[arglen]:=nestedfpstruct;
end;
constructor FpcBaseNestedProcVarType.create(inst, context: jlobject; const methodName: unicodestring; const argTypes: array of JLClass);
begin
inherited create(inst,methodName,argTypes);
nestedfpstruct:=context;
end;
procedure FpcBaseNestedProcVarType.fpcDeepCopy(result: FpcBaseProcVarType);
begin
inherited fpcDeepCopy(result);
FpcBaseNestedProcVarType(result).nestedfpstruct:=nestedfpstruct;
end;
function FpcBaseNestedProcVarType.clone: JLObject;
begin
result:=inherited;
FpcBaseNestedProcVarType(result).nestedfpstruct:=nestedfpstruct;
end;
procedure FpcBaseNestedProcVarType.invokeProc(const args: array of jlobject);
begin
inherited invokeProc(getNestedArgs(args));
end;
function FpcBaseNestedProcVarType.invokeBooleanFunc(const args: array of jlobject): jboolean;
begin
result:=inherited invokeBooleanFunc(getNestedArgs(args));
end;
function FpcBaseNestedProcVarType.invokeCharFunc(const args: array of jlobject): jchar;
begin
result:=inherited invokeCharFunc(getNestedArgs(args));
end;
function FpcBaseNestedProcVarType.invokeByteFunc(const args: array of jlobject): jbyte;
begin
result:=inherited invokeByteFunc(getNestedArgs(args));
end;
function FpcBaseNestedProcVarType.invokeShortFunc(const args: array of jlobject): jshort;
begin
result:=inherited invokeShortFunc(getNestedArgs(args));
end;
function FpcBaseNestedProcVarType.invokeIntFunc(const args: array of jlobject): jint;
begin
result:=inherited invokeIntFunc(getNestedArgs(args));
end;
function FpcBaseNestedProcVarType.invokeLongFunc(const args: array of jlobject): jlong;
begin
result:=inherited invokeLongFunc(getNestedArgs(args));
end;
function FpcBaseNestedProcVarType.invokeSingleFunc(const args: array of jlobject): jfloat;
begin
result:=inherited invokeSingleFunc(getNestedArgs(args));
end;
function FpcBaseNestedProcVarType.invokeDoubleFunc(const args: array of jlobject): jdouble;
begin
result:=inherited invokeDoubleFunc(getNestedArgs(args));
end;
function FpcBaseNestedProcVarType.invokeObjectFunc(const args: array of jlobject): jlobject;
begin
result:=inherited invokeObjectFunc(getNestedArgs(args));
end;
|