This is an automated email from the ASF dual-hosted git repository. jtulach pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/netbeans-html4j.git
The following commit(s) were added to refs/heads/master by this push: new 3dc9560 @ComputedProperty write method maybe instance one 3dc9560 is described below commit 3dc9560c31749a8065ea5b69cf662426db465d4e Author: Jaroslav Tulach <jaroslav.tul...@apidesign.org> AuthorDate: Sat Apr 3 11:35:59 2021 +0200 @ComputedProperty write method maybe instance one --- .../netbeans/html/json/impl/ModelProcessor.java | 18 +++--- .../html/json/impl/WritablePropertyTest.java | 64 ++++++++++++++++++++++ 2 files changed, 73 insertions(+), 9 deletions(-) diff --git a/json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java b/json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java index 27accd0..0dc7b87 100644 --- a/json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java +++ b/json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java @@ -46,7 +46,6 @@ import javax.annotation.processing.ProcessingEnvironment; import javax.annotation.processing.Processor; import javax.annotation.processing.RoundEnvironment; import javax.annotation.processing.SupportedAnnotationTypes; -import javax.annotation.processing.SupportedSourceVersion; import javax.lang.model.SourceVersion; import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.AnnotationValue; @@ -62,7 +61,6 @@ import javax.lang.model.type.DeclaredType; import javax.lang.model.type.MirroredTypeException; import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeMirror; -import javax.lang.model.util.Elements; import javax.lang.model.util.Types; import javax.tools.Diagnostic; import javax.tools.FileObject; @@ -780,6 +778,7 @@ public final class ModelProcessor extends AbstractProcessor { } ExecutableElement ee = (ExecutableElement)e; ExecutableElement write = null; + boolean instance = e.getEnclosingElement().getAnnotation(Model.class).instance(); if (!cp.write().isEmpty()) { write = findWrite(ee, (TypeElement)e.getEnclosingElement(), cp.write(), className); ok = write != null; @@ -928,7 +927,7 @@ public final class ModelProcessor extends AbstractProcessor { } else { w.write(" public void " + gs[4] + "(" + write.getParameters().get(1).asType()); w.write(" value) {\n"); - w.write(" " + fqn(ee.getEnclosingElement().asType(), ee) + '.' + write.getSimpleName() + "(this, value);\n"); + w.write(" " + (instance ? "instance" : fqn(ee.getEnclosingElement().asType(), ee)) + '.' + write.getSimpleName() + "(this, value);\n"); w.write(" }\n"); props.add(new GetSet( @@ -1088,20 +1087,20 @@ public final class ModelProcessor extends AbstractProcessor { } for (String pn : onPC.value()) { if (findPrprt(properties, pn) == null && findDerivedFrom(propDeps, pn).isEmpty()) { - error("No Prprt named '" + pn + "' in the model", clazz); + error("No property named '" + pn + "' in the model", clazz); return false; } } if (!instance && !e.getModifiers().contains(Modifier.STATIC)) { - error("@OnPrprtChange method needs to be static", e); + error("@OnPropertyChange method needs to be static", e); return false; } if (e.getModifiers().contains(Modifier.PRIVATE)) { - error("@OnPrprtChange method cannot be private", e); + error("@OnPropertyChange method cannot be private", e); return false; } if (e.getReturnType().getKind() != TypeKind.VOID) { - error("@OnPrprtChange method should return void", e); + error("@OnPropertyChange method should return void", e); return false; } String n = e.getSimpleName().toString(); @@ -1765,7 +1764,7 @@ public final class ModelProcessor extends AbstractProcessor { continue; } error( - "@OnPrprtChange method can only accept String or " + className + " arguments", + "@OnPropertyChange method can only accept String or " + className + " arguments", ee); } return params; @@ -2286,6 +2285,7 @@ public final class ModelProcessor extends AbstractProcessor { private ExecutableElement findWrite(ExecutableElement computedPropElem, TypeElement te, String name, String className) { String err = null; + boolean instance = te.getAnnotation(Model.class).instance(); METHODS: for (Element e : te.getEnclosedElements()) { if (e.getKind() != ElementKind.METHOD) { @@ -2297,7 +2297,7 @@ public final class ModelProcessor extends AbstractProcessor { if (e.equals(computedPropElem)) { continue; } - if (!e.getModifiers().contains(Modifier.STATIC)) { + if (!instance && !e.getModifiers().contains(Modifier.STATIC)) { computedPropElem = (ExecutableElement) e; err = "Would have to be static"; continue; diff --git a/json/src/test/java/org/netbeans/html/json/impl/WritablePropertyTest.java b/json/src/test/java/org/netbeans/html/json/impl/WritablePropertyTest.java new file mode 100644 index 0000000..85f9fd9 --- /dev/null +++ b/json/src/test/java/org/netbeans/html/json/impl/WritablePropertyTest.java @@ -0,0 +1,64 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.netbeans.html.json.impl; + +import net.java.html.json.ComputedProperty; +import net.java.html.json.Model; +import net.java.html.json.ModelOperation; +import net.java.html.json.Property; +import static org.testng.Assert.assertEquals; +import org.testng.annotations.Test; + +@Model(className = "WritablePropertyModel", instance = true, properties = { + @Property(name = "meaning", type = int.class) +}) +public class WritablePropertyTest { + private int value; + + @ComputedProperty(write = "changeMeaning") + static int realMeaning(int meaning) { + return 40 + meaning; + } + + void changeMeaning(WritablePropertyModel model, int value) { + this.value = value; + } + + @ModelOperation + void readValue(WritablePropertyModel model, int[] store) { + store[0] = this.value; + } + + @Test + public void changeValueInTest() { + WritablePropertyModel model = new WritablePropertyModel(); + model.setMeaning(2); + + assertEquals(model.getRealMeaning(), 42, "Changed to 42"); + + model.setRealMeaning(84); + + assertEquals(model.getRealMeaning(), 42, "Unchanged..."); + + int[] holder = { -1 }; + model.readValue(holder); + assertEquals(holder[0], 84, "...but value changed!"); + + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@netbeans.apache.org For additional commands, e-mail: commits-h...@netbeans.apache.org For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists