Skip to content
This repository has been archived by the owner on Jun 7, 2019. It is now read-only.

Commit

Permalink
Add class loading for Jar files and API for arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
MWGuy committed May 16, 2019
1 parent 955ea4a commit ceaf85e
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.php.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: java-reflection-ext
version: 1.1.1
version: 1.2.0
description: Java Reflection API

plugins:
Expand Down
15 changes: 15 additions & 0 deletions sdk/java/reflection/JarClassLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php


namespace java\reflection;


use php\io\File;

class JarClassLoader
{
/**
* @param File $jarFile
*/
public function __construct(File $jarFile) {}
}
35 changes: 35 additions & 0 deletions sdk/java/reflection/ReflectionArray.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php


namespace java\reflection;


class ReflectionArray
{
/**
* @param ReflectionClass $class
* @param int $length
* @return ReflectionObject
*/
public static function newInstance(ReflectionClass $class, int $length): ReflectionObject {}

/**
* @param ReflectionObject $object
* @return int
*/
public static function getLength(ReflectionObject $object): int {}

/**
* @param ReflectionObject $object
* @param int $id
* @return ReflectionObject
*/
public static function get(ReflectionObject $object, int $id): ReflectionObject {}

/**
* @param ReflectionObject $object
* @param int $id
* @param ReflectionObject $data
*/
public static function set(ReflectionObject $object, int $id, ReflectionObject $data) {}
}
3 changes: 2 additions & 1 deletion sdk/java/reflection/ReflectionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ private function __construct() {}

/**
* @param string $javaClassName
* @param JarClassLoader $cl
* @return ReflectionClass
*/
public static function forName(string $javaClassName): ReflectionClass {}
public static function forName(string $javaClassName, JarClassLoader $cl = null): ReflectionClass {}

public function newInstance(): ReflectionObject {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
import php.runtime.env.Environment;
import php.runtime.ext.support.Extension;

import java.lang.reflect.Constructor;
import java.lang.reflect.Executable;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.*;

public class JavaReflectionExtension extends Extension {
public static final String NS = "java\\reflection";
Expand All @@ -26,12 +23,15 @@ public String getName() {

@Override
public void onRegister(CompileScope scope) {
registerClass(scope, JarClassLoader.class);

registerWrapperClass(scope, Class.class, ReflectionClass.class);
registerWrapperClass(scope, Object.class, ReflectionObject.class);
registerWrapperClass(scope, Executable.class, ReflectionExecutable.class);
registerWrapperClass(scope, Constructor.class, ReflectionConstructor.class);
registerWrapperClass(scope, Method.class, ReflectionMethod.class);
registerWrapperClass(scope, Field.class, ReflectionField.class);
registerWrapperClass(scope, Array.class, ReflectionArray.class);

registerClass(scope, ReflectionTypes.class);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.venity.javareflection.classes;

import org.venity.javareflection.JavaReflectionExtension;
import php.runtime.annotation.Reflection;
import php.runtime.env.Environment;
import php.runtime.lang.BaseObject;
import php.runtime.reflection.ClassEntity;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

@Reflection.Name("JarClassLoader")
@Reflection.Namespace(JavaReflectionExtension.NS)
public class JarClassLoader extends BaseObject {

public JarClassLoader(Environment env) {
super(env);
}

protected JarClassLoader(ClassEntity entity) {
super(entity);
}

public JarClassLoader(Environment env, ClassEntity clazz) {
super(env, clazz);
}

private URLClassLoader classLoader;

@Reflection.Signature
public void __construct(File jar) throws MalformedURLException {
classLoader = new URLClassLoader(
new URL[] {jar.toURI().toURL()},
this.getClass().getClassLoader()
);
}

public URLClassLoader getClassLoader() {
return classLoader;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.venity.javareflection.classes;

import org.venity.javareflection.JavaReflectionExtension;
import php.runtime.annotation.Reflection;
import php.runtime.env.Environment;
import php.runtime.lang.BaseWrapper;
import php.runtime.reflection.ClassEntity;

import java.lang.reflect.Array;

@Reflection.Name("ReflectionArray")
@Reflection.Namespace(JavaReflectionExtension.NS)
public class ReflectionArray extends BaseWrapper<Array> {
public ReflectionArray(Environment env, Array wrappedObject) {
super(env, wrappedObject);
}

public ReflectionArray(Environment env, ClassEntity clazz) {
super(env, clazz);
}

@Reflection.Signature
public static ReflectionObject newInstance(Environment environment, Class<?> componentType, int length) {
return new ReflectionObject(environment, Array.newInstance(componentType, length));
}

@Reflection.Signature
public static int getLength(ReflectionObject object) {
return Array.getLength(object.getWrappedObject());
}

@Reflection.Signature
public static ReflectionObject get(Environment environment, ReflectionObject object, int id) {
return new ReflectionObject(environment, Array.get(object.getWrappedObject(), id));
}

@Reflection.Signature
public static void set(Environment environment, ReflectionObject object, int id, ReflectionObject reflectionObject) {
Array.set(object.getWrappedObject(), id, reflectionObject.getWrappedObject());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public static ReflectionClass forName(Environment env, String className) throws
return new ReflectionClass(env, Class.forName(className));
}

@Reflection.Signature
public static ReflectionClass forName(Environment env, String className, JarClassLoader classLoader) throws ClassNotFoundException {
return new ReflectionClass(env, Class.forName(className, false, classLoader.getClassLoader()));
}

@Reflection.Signature
public ReflectionObject newInstance() throws IllegalAccessException, InstantiationException {
return new ReflectionObject(__env__, __wrappedObject.newInstance());
Expand Down

0 comments on commit ceaf85e

Please sign in to comment.