From 26452eb5dea15d12fcb932d51334590a9db20986 Mon Sep 17 00:00:00 2001 From: Daniel Holle Date: Mon, 11 Mar 2024 16:16:10 +0100 Subject: [PATCH] Fix #286 --- resources/bytecode/javFiles/Access.jav | 14 +++++++++ resources/bytecode/javFiles/Bug98.jav | 16 ++++++++++ resources/bytecode/javFiles/Pair.jav | 31 +++++-------------- .../de/dhbwstuttgart/bytecode/Codegen.java | 12 +++---- src/test/java/TestComplete.java | 24 ++++++++++++-- 5 files changed, 65 insertions(+), 32 deletions(-) create mode 100644 resources/bytecode/javFiles/Access.jav create mode 100644 resources/bytecode/javFiles/Bug98.jav diff --git a/resources/bytecode/javFiles/Access.jav b/resources/bytecode/javFiles/Access.jav new file mode 100644 index 00000000..46cd8c8b --- /dev/null +++ b/resources/bytecode/javFiles/Access.jav @@ -0,0 +1,14 @@ +public class Access { + public int fPublic; + int fDefault; + private int fPrivate; + protected int fProtected; + + public void mPublic() {} + void mDefault() {} + private void mPrivate() {} + protected void mProtected() {} +} + +class AccessDefault { +} \ No newline at end of file diff --git a/resources/bytecode/javFiles/Bug98.jav b/resources/bytecode/javFiles/Bug98.jav new file mode 100644 index 00000000..2da3a4ef --- /dev/null +++ b/resources/bytecode/javFiles/Bug98.jav @@ -0,0 +1,16 @@ +import java.util.Vector; +import java.lang.Integer; +import java.lang.String; + +public class Bug98 { + + m(x, y ,z) { + x = new Vector(); + y = new Vector(); + x.add(1); + y.add("2"); + //Integer i = x.elementAt(0); + //String s = y.elementAt(0); + return z.vectorAddAll(x, y); + } +} \ No newline at end of file diff --git a/resources/bytecode/javFiles/Pair.jav b/resources/bytecode/javFiles/Pair.jav index aacb8d4f..9b32bc02 100644 --- a/resources/bytecode/javFiles/Pair.jav +++ b/resources/bytecode/javFiles/Pair.jav @@ -2,31 +2,16 @@ import java.util.Vector; import java.lang.Boolean; import java.lang.Object; -class Pair { - U a; - T b; - make(x) { - var ret = new Pair<>(); - ret.a = x.elementAt(0); - ret.b = x.elementAt(1); - return ret; - } - /* - eq(a, b) { - b = a; - return a == b; - } +class Pair { + T x; + U y; - compare( p) { - return eq(p.a, p.b); - //return p.a == p.b; - } + fst () { + return x; + } - void m(Pair p, List b) - { - //this.compare(p); //1, type incorrect - this.compare(this.make(b)); //2, OK + snd () { + return y; } -*/ } \ No newline at end of file diff --git a/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java b/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java index 7445e772..b28fe67d 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java @@ -1357,8 +1357,8 @@ public class Codegen { private void generateField(TargetField field) { var access = field.access(); - if ((access & ACC_PRIVATE) == 0 && (access & ACC_PROTECTED) == 0) // TODO Implement access modifiers properly - access |= ACC_PUBLIC; + //if ((access & ACC_PRIVATE) == 0 && (access & ACC_PROTECTED) == 0) // TODO Implement access modifiers properly + // access |= ACC_PUBLIC; cw.visitField(access, field.name(), field.type().toSignature(), field.type().toDescriptor(), null); } @@ -1376,7 +1376,7 @@ public class Codegen { } private void generateConstructor(TargetConstructor constructor) { - MethodVisitor mv = cw.visitMethod(constructor.access() | ACC_PUBLIC, "", constructor.getDescriptor(), constructor.getSignature(), null); + MethodVisitor mv = cw.visitMethod(constructor.access(), "", constructor.getDescriptor(), constructor.getSignature(), null); if (constructor.txGenerics() != null) mv.visitAttribute(new JavaTXSignatureAttribute(constructor.getTXSignature())); @@ -1431,7 +1431,7 @@ public class Codegen { } private void generateMethod(TargetMethod method) { - var access = method.access() | ACC_PUBLIC; + var access = method.access(); if (method.block() == null) access |= ACC_ABSTRACT; @@ -1475,8 +1475,8 @@ public class Codegen { public byte[] generate() { var access = clazz.modifiers(); - if ((access & ACC_PRIVATE) == 0 && (access & ACC_PROTECTED) == 0) // TODO Implement access modifiers properly - access |= ACC_PUBLIC; + //if ((access & ACC_PRIVATE) == 0 && (access & ACC_PROTECTED) == 0) // TODO Implement access modifiers properly + // access |= ACC_PUBLIC; if (!(clazz instanceof TargetInterface)) access |= ACC_SUPER; diff --git a/src/test/java/TestComplete.java b/src/test/java/TestComplete.java index 45d3cdb6..92f674ab 100644 --- a/src/test/java/TestComplete.java +++ b/src/test/java/TestComplete.java @@ -3,9 +3,7 @@ import de.dhbwstuttgart.environment.ByteArrayClassLoader; import org.junit.Ignore; import org.junit.Test; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.lang.reflect.ParameterizedType; +import java.lang.reflect.*; import java.util.Arrays; import java.util.Vector; @@ -849,6 +847,26 @@ public class TestComplete { var instance = clazz.getDeclaredConstructor().newInstance(); } + @Test + public void testAccess() throws Exception { + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Access.jav"); + var clazzPublic = classFiles.get("Access"); + var clazzDefault = classFiles.get("AccessDefault"); + + assertEquals(clazzPublic.getModifiers(), Modifier.PUBLIC); + assertEquals(clazzDefault.getModifiers(), 0); + + assertEquals(clazzPublic.getDeclaredMethod("mPublic").getModifiers(), Modifier.PUBLIC); + assertEquals(clazzPublic.getDeclaredMethod("mProtected").getModifiers(), Modifier.PROTECTED); + assertEquals(clazzPublic.getDeclaredMethod("mDefault").getModifiers(), 0); + assertEquals(clazzPublic.getDeclaredMethod("mPrivate").getModifiers(), Modifier.PRIVATE); + + assertEquals(clazzPublic.getDeclaredField("fPublic").getModifiers(), Modifier.PUBLIC); + assertEquals(clazzPublic.getDeclaredField("fProtected").getModifiers(), Modifier.PROTECTED); + assertEquals(clazzPublic.getDeclaredField("fDefault").getModifiers(), 0); + assertEquals(clazzPublic.getDeclaredField("fPrivate").getModifiers(), Modifier.PRIVATE); + } + @Test public void testBug122() throws Exception { var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Bug122.jav");