Skip to content

Commit 826891a

Browse files
committed
Поддержка инстанцирования java-класса с помощью ключевого слова new
1 parent 483c7c1 commit 826891a

File tree

4 files changed

+31
-13
lines changed

4 files changed

+31
-13
lines changed

src/main/java/com/annimon/ownlang/lib/ClassDeclarations.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package com.annimon.ownlang.lib;
22

3-
import com.annimon.ownlang.exceptions.UnknownFunctionException;
43
import com.annimon.ownlang.parser.ast.ClassDeclarationStatement;
5-
import java.util.HashMap;
64
import java.util.Map;
5+
import java.util.concurrent.ConcurrentHashMap;
76

87
public final class ClassDeclarations {
98

109
private static final Map<String, ClassDeclarationStatement> declarations;
1110
static {
12-
declarations = new HashMap<>();
11+
declarations = new ConcurrentHashMap<>();
1312
}
1413

1514
private ClassDeclarations() { }
@@ -22,12 +21,7 @@ public static Map<String, ClassDeclarationStatement> getAll() {
2221
return declarations;
2322
}
2423

25-
public static boolean isExists(String key) {
26-
return declarations.containsKey(key);
27-
}
28-
2924
public static ClassDeclarationStatement get(String key) {
30-
if (!isExists(key)) throw new UnknownFunctionException(key);
3125
return declarations.get(key);
3226
}
3327

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.annimon.ownlang.lib;
2+
3+
/**
4+
* Interface for values that supports creating instances with `new` keyword.
5+
*/
6+
public interface Instantiable {
7+
8+
Value newInstance(Value[] args);
9+
}

src/main/java/com/annimon/ownlang/modules/java/java.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public String toString() {
103103
}
104104
}
105105

106-
private static class ClassValue extends MapValue {
106+
private static class ClassValue extends MapValue implements Instantiable {
107107

108108
public static Value classOrNull(Class<?> clazz) {
109109
if (clazz == null) return NULL;
@@ -162,7 +162,8 @@ private Value isAssignableFrom(Value[] args) {
162162
return NumberValue.fromBoolean(clazz.isAssignableFrom( ((ClassValue)args[0]).clazz ));
163163
}
164164

165-
private Value newInstance(Value[] args) {
165+
@Override
166+
public Value newInstance(Value[] args) {
166167
return findConstructorAndInstantiate(args, clazz.getConstructors());
167168
}
168169

src/main/java/com/annimon/ownlang/parser/ast/ObjectCreationExpression.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.annimon.ownlang.parser.ast;
22

3+
import com.annimon.ownlang.exceptions.UnknownClassException;
34
import com.annimon.ownlang.lib.*;
45
import java.util.Iterator;
56
import java.util.List;
@@ -17,6 +18,16 @@ public ObjectCreationExpression(String className, List<Expression> constructorAr
1718
@Override
1819
public Value eval() {
1920
final ClassDeclarationStatement cd = ClassDeclarations.get(className);
21+
if (cd == null) {
22+
// Is Instantiable?
23+
if (Variables.isExists(className)) {
24+
final Value variable = Variables.get(className);
25+
if (variable instanceof Instantiable) {
26+
return ((Instantiable) variable).newInstance(ctorArgs());
27+
}
28+
}
29+
throw new UnknownClassException(className);
30+
}
2031

2132
// Create an instance and put evaluated fields with method declarations
2233
final ClassInstanceValue instance = new ClassInstanceValue(className);
@@ -30,14 +41,17 @@ public Value eval() {
3041
}
3142

3243
// Call a constructor
44+
instance.callConstructor(ctorArgs());
45+
return instance;
46+
}
47+
48+
private Value[] ctorArgs() {
3349
final int argsSize = constructorArguments.size();
3450
final Value[] ctorArgs = new Value[argsSize];
3551
for (int i = 0; i < argsSize; i++) {
3652
ctorArgs[i] = constructorArguments.get(i).eval();
3753
}
38-
instance.callConstructor(ctorArgs);
39-
40-
return instance;
54+
return ctorArgs;
4155
}
4256

4357
@Override

0 commit comments

Comments
 (0)