Skip to content

Commit

Permalink
Merge pull request #143 from xpenatan/master
Browse files Browse the repository at this point in the history
Improved String equals
  • Loading branch information
fpetrola committed Feb 18, 2017
2 parents 73b9a37 + a0c1d93 commit 71a458b
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 29 deletions.
24 changes: 21 additions & 3 deletions dragome-js-jre/src/main/java/java/lang/String.java
Expand Up @@ -153,9 +153,27 @@ public byte[] getBytes()
*/
public boolean equals(Object obj)
{
ScriptHelper.put("obj", obj, this);
// Note that method.apply(thisArg) will passed ToObject(thisArg) as the this value.
return ScriptHelper.evalBoolean("String(this) == String(obj)", this);
if (this == obj)
return true;
if (obj instanceof String) {
ScriptHelper.put("obj", obj, this);
ScriptHelper.evalNoResult("var string1 = String(this)", this);
ScriptHelper.evalNoResult("var string2 = String(obj)", this);
int n1 = ScriptHelper.evalInt("string1.length", this);
int n2 = ScriptHelper.evalInt("string2.length", this);
if (n1 == n2) {
int i = 0;
while (n1-- != 0) {
ScriptHelper.put("i", i, this);
boolean flag = ScriptHelper.evalBoolean("string1.charAt(i) == string2.charAt(i)", this);
if (!flag)
return false;
i++;
}
return true;
}
}
return false;
}

/**
Expand Down
81 changes: 65 additions & 16 deletions dragome-js-jre/src/main/java/java/lang/reflect/Field.java
Expand Up @@ -121,31 +121,56 @@ public boolean equals(Object object)
return false;
}

public native boolean getBoolean(Object object) throws IllegalAccessException, IllegalArgumentException;
public boolean getBoolean(Object object) throws IllegalAccessException, IllegalArgumentException
{
return (boolean)this.get(object);
}

public native byte getByte(Object object) throws IllegalAccessException, IllegalArgumentException;
public byte getByte(Object object) throws IllegalAccessException, IllegalArgumentException
{
return (byte)this.get(object);
}

public native char getChar(Object object) throws IllegalAccessException, IllegalArgumentException;
public char getChar(Object object) throws IllegalAccessException, IllegalArgumentException
{
return (char)this.get(object);
}

public Class<?> getDeclaringClass()
{
return class1;
}

public native double getDouble(Object object) throws IllegalAccessException, IllegalArgumentException;
public double getDouble(Object object) throws IllegalAccessException, IllegalArgumentException
{
return (double)this.get(object);
}

public native float getFloat(Object object) throws IllegalAccessException, IllegalArgumentException;
public float getFloat(Object object) throws IllegalAccessException, IllegalArgumentException
{
return (float)this.get(object);
}

public int getInt(Object object) throws IllegalAccessException, IllegalArgumentException
{
return (int)this.get(object);
}

public native int getInt(Object object) throws IllegalAccessException, IllegalArgumentException;
public long getLong(Object object) throws IllegalAccessException, IllegalArgumentException
{
return (long)this.get(object);
}

public native long getLong(Object object) throws IllegalAccessException, IllegalArgumentException;
public short getShort(Object object) throws IllegalAccessException, IllegalArgumentException
{
return (short)this.get(object);
}

public int getModifiers()
{
return 0;
}

public native short getShort(Object object) throws IllegalAccessException, IllegalArgumentException;

native String getSignature();

Expand All @@ -159,21 +184,45 @@ public int hashCode()
return 0;
}

public native void setBoolean(Object object, boolean value) throws IllegalAccessException, IllegalArgumentException;
public void setBoolean(Object object, boolean value) throws IllegalAccessException, IllegalArgumentException
{
this.set(object, value);
}

public native void setByte(Object object, byte value) throws IllegalAccessException, IllegalArgumentException;
public void setByte(Object object, byte value) throws IllegalAccessException, IllegalArgumentException
{
this.set(object, value);
}

public native void setChar(Object object, char value) throws IllegalAccessException, IllegalArgumentException;
public void setChar(Object object, char value) throws IllegalAccessException, IllegalArgumentException
{
this.set(object, value);
}

public native void setDouble(Object object, double value) throws IllegalAccessException, IllegalArgumentException;
public void setDouble(Object object, double value) throws IllegalAccessException, IllegalArgumentException
{
this.set(object, value);
}

public native void setFloat(Object object, float value) throws IllegalAccessException, IllegalArgumentException;
public void setFloat(Object object, float value) throws IllegalAccessException, IllegalArgumentException
{
this.set(object, value);
}

public native void setInt(Object object, int value) throws IllegalAccessException, IllegalArgumentException;
public void setInt(Object object, int value) throws IllegalAccessException, IllegalArgumentException
{
this.set(object, value);
}

public native void setLong(Object object, long value) throws IllegalAccessException, IllegalArgumentException;
public void setLong(Object object, long value) throws IllegalAccessException, IllegalArgumentException
{
this.set(object, value);
}

public native void setShort(Object object, short value) throws IllegalAccessException, IllegalArgumentException;
public void setShort(Object object, short value) throws IllegalAccessException, IllegalArgumentException
{
this.set(object, value);
}

public String toString()
{
Expand Down
Expand Up @@ -28,21 +28,21 @@
@RunWith(DragomeTestRunner.class)
public class ReflectionAPITests extends TestCase
{
public static class ReflectionClass2 {
public static class ReflectionClass2 {
private boolean field1 = true;
public int field2;

public boolean getField1() {
return field1;
}
}



@Retention(RetentionPolicy.RUNTIME)
public @interface Annotation1
{
String value1() default "1";
String value2() default "1";
String[] value3() default {"1", "2"};
}

@Annotation1
Expand All @@ -68,6 +68,9 @@ public class ReflectionClass extends SuperClass
@Annotation1
public boolean field2;

@Annotation1(value3 = {"3", "4"})
public boolean field3;

@Annotation1(value1= "methodWithNoArguments")
public void methodWithNoArguments()
{
Expand Down Expand Up @@ -317,6 +320,25 @@ public void testGettingAnnotationFromField() throws Exception
assertEquals("value1:field1", annotation1.value1());
}

@Test
public void testGettingAnnotationDefaultValueFromField() throws Exception
{
Class<ReflectionClass> class1= ReflectionClass.class;
Field field= class1.getField("field1");
Annotation1 annotation1= field.getAnnotation(Annotation1.class);
assertEquals("1", annotation1.value2());
}

@Test
public void testGettingAnnotationArrayFromField() throws Exception
{
Class<ReflectionClass> class1= ReflectionClass.class;
Field field3= class1.getField("field3");
Annotation1 annotation1= field3.getAnnotation(Annotation1.class);
String[] value3 = annotation1.value3();
assertEquals("4", value3[1]);
}

@Test
public void testGettingNotParametizedAnnotationFromField() throws Exception
{
Expand All @@ -325,7 +347,7 @@ public void testGettingNotParametizedAnnotationFromField() throws Exception
Annotation1 annotation1= field.getAnnotation(Annotation1.class);
assertNotNull(annotation1);
}

@Test
public void testSetFieldWithTrue() throws Exception
{
Expand All @@ -334,7 +356,7 @@ public void testSetFieldWithTrue() throws Exception
field.set(obj, true);
assertEquals(true, obj.field1);
}

@Test
public void testSetFieldPrivateWithFalse() throws Exception
{
Expand All @@ -353,9 +375,9 @@ public void testGetFieldWithTrue() throws Exception
ReflectionClass obj = new ReflectionClass();
obj.field1 = true;
Object boolValue = field.get(obj);
assertEquals(true, boolValue);
assertEquals(true, boolValue);
}

@Test
public void testGetFieldPrivateWithTrue() throws Exception
{
Expand All @@ -365,7 +387,7 @@ public void testGetFieldPrivateWithTrue() throws Exception
Object boolValue = field.get(obj);
assertEquals(true, boolValue);
}

@Test
public void testGetFieldInteger() throws Exception
{
Expand Down
Expand Up @@ -41,7 +41,6 @@ public boolean filterClassPath(String classpathEntry)
boolean include= super.filterClassPath(classpathEntry);
include|= classpathEntry.contains("junit-4");
include|= classpathEntry.contains("hamcrest-core");
include|= classpathEntry.contains("classes");
include|= classpathEntry.contains("test-classes");

return include;
Expand Down

0 comments on commit 71a458b

Please sign in to comment.