Skip to content

Commit de6006a

Browse files
committed
make beaning creation behaviour more in line with Rhino
1 parent 07cdb63 commit de6006a

File tree

1 file changed

+15
-11
lines changed
  • src/main/java/zzzank/probejs/lang/transpiler/transformation/impl

1 file changed

+15
-11
lines changed

src/main/java/zzzank/probejs/lang/transpiler/transformation/impl/InjectBeans.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,22 @@
1616
public class InjectBeans implements ClassTransformer {
1717
@Override
1818
public void transform(Clazz clazz, ClassDecl classDecl) {
19-
val excludedGetterNames = new HashSet<String>();
19+
val usedNames = new HashSet<String>();
2020
for (val method : classDecl.methods) {
21-
excludedGetterNames.add(method.name);
21+
usedNames.add(method.name);
2222
}
2323

2424
if (ProbeConfig.fieldAsBean.get()) {
25-
fromField(classDecl, excludedGetterNames);
25+
fromField(classDecl, usedNames);
26+
}
27+
for (val field : classDecl.fields) {
28+
usedNames.add(field.name);
2629
}
2730

28-
fromMethod(classDecl, excludedGetterNames);
31+
fromMethod(classDecl, usedNames);
2932
}
3033

31-
private void fromMethod(ClassDecl classDecl, Set<String> excludedGetterNames) {
34+
private void fromMethod(ClassDecl classDecl, Set<String> usedNames) {
3235
for (val method : classDecl.methods) {
3336
if (method.isStatic) {
3437
continue;
@@ -37,17 +40,17 @@ private void fromMethod(ClassDecl classDecl, Set<String> excludedGetterNames) {
3740
val name = method.name;
3841
if (method.params.size() == 1) {
3942
val beanName = extractBeanName(name, "set");
40-
if (beanName != null) {
43+
if (beanName != null && !usedNames.contains(beanName)) {
4144
classDecl.bodyCode.add(new BeanDecl.Setter(name, method.params.get(0).type));
4245
}
4346
} else if (method.params.isEmpty()) {
4447
var beanName = extractBeanName(name, "get");
4548
if (beanName == null) {
4649
beanName = extractBeanName(name, "is");
4750
}
48-
if (beanName != null && !excludedGetterNames.contains(beanName)) {
51+
if (beanName != null && !usedNames.contains(beanName)) {
4952
classDecl.bodyCode.add(new BeanDecl.Getter(beanName, method.returnType));
50-
excludedGetterNames.add(beanName);
53+
usedNames.add(beanName);
5154
}
5255
}
5356
}
@@ -61,26 +64,27 @@ private String extractBeanName(String name, String prefix) {
6164
return NameUtils.firstLower(beanName);
6265
}
6366

64-
private void fromField(ClassDecl clazzDecl, Set<String> excludedGetterNames) {
67+
private void fromField(ClassDecl clazzDecl, Set<String> excludedNames) {
6568
val keptFields = new ArrayList<FieldDecl>();
6669
for (val field : clazzDecl.fields) {
6770
if (field.isStatic) {
6871
keptFields.add(field);
6972
continue;
7073
}
7174

72-
if (!excludedGetterNames.contains(field.name)) {
75+
if (!excludedNames.contains(field.name)) {
7376
val getter = new BeanDecl.Getter(field.name, field.type);
7477
getter.comments.addAll(field.comments);
7578
clazzDecl.bodyCode.add(getter);
76-
excludedGetterNames.add(field.name);
7779
}
7880

7981
if (!field.isFinal) {
8082
val setter = new BeanDecl.Setter(field.name, field.type);
8183
setter.comments.addAll(field.comments);
8284
clazzDecl.bodyCode.add(setter);
8385
}
86+
87+
excludedNames.add(field.name);
8488
}
8589
clazzDecl.fields.clear();
8690
clazzDecl.fields.addAll(keptFields);

0 commit comments

Comments
 (0)