Skip to content

Commit

Permalink
[jsscripting] Reduce compiler warnings
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
  • Loading branch information
florian-h05 committed Dec 12, 2022
1 parent 3e8bbda commit e9708bb
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import javax.script.ScriptEngine;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.automation.jsscripting.internal.fs.watch.JSDependencyTracker;
import org.openhab.core.automation.module.script.ScriptDependencyTracker;
Expand All @@ -37,6 +38,7 @@
@Component(service = ScriptEngineFactory.class, configurationPid = "org.openhab.jsscripting", property = Constants.SERVICE_PID
+ "=org.openhab.jsscripting")
@ConfigurableService(category = "automation", label = "JS Scripting", description_uri = "automation:jsscripting")
@NonNullByDefault
public final class GraalJSScriptEngineFactory implements ScriptEngineFactory {
private static final String CFG_INJECTION_ENABLED = "injectionEnabled";
private static final String INJECTION_CODE = "Object.assign(this, require('openhab'));";
Expand Down Expand Up @@ -80,7 +82,7 @@ public void scopeValues(ScriptEngine scriptEngine, Map<String, Object> scopeValu
}

@Override
public ScriptEngine createScriptEngine(String scriptType) {
public @Nullable ScriptEngine createScriptEngine(String scriptType) {
return new DebuggingGraalScriptEngine<>(
new OpenhabGraalJSScriptEngine(injectionEnabled ? INJECTION_CODE : null, jsScriptServiceUtil));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public class OpenhabGraalJSScriptEngine

// these fields start as null because they are populated on first use
private String engineIdentifier;
private Consumer<String> scriptDependencyListener;
private @Nullable Consumer<String> scriptDependencyListener;

private boolean initialized = false;
private final String globalScript;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@

package org.openhab.automation.jsscripting.internal.scope;

import java.util.*;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

Expand All @@ -28,7 +32,7 @@
* @author Jonathan Gilbert - Initial contribution
*/
public abstract class AbstractScriptExtensionProvider implements ScriptExtensionProvider {
private Map<String, Function<String, Object>> types;
private Map<String, Function<String, Object>> types = new HashMap<>();
private Map<String, Map<String, Object>> idToTypes = new ConcurrentHashMap<>();

protected abstract String getPresetName();
Expand All @@ -41,7 +45,6 @@ protected void addType(String name, Function<String, Object> value) {

@Activate
public void activate(final BundleContext context) {
types = new HashMap<>();
initializeTypes(context);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@

package org.openhab.automation.jsscripting.internal.scope;

import java.util.*;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package org.openhab.automation.jsscripting.internal.scriptengine;

import java.io.Reader;
import java.util.Objects;

import javax.script.Bindings;
import javax.script.Invocable;
Expand All @@ -23,7 +22,7 @@
import javax.script.ScriptEngineFactory;
import javax.script.ScriptException;

import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jdt.annotation.NonNull;

/**
* {@link ScriptEngine} implementation that delegates to a supplied ScriptEngine instance. Allows overriding specific
Expand All @@ -33,94 +32,90 @@
*/
public abstract class DelegatingScriptEngineWithInvocableAndAutocloseable<T extends ScriptEngine & Invocable & AutoCloseable>
implements ScriptEngine, Invocable, AutoCloseable {
protected T delegate;
protected @NonNull T delegate;

public DelegatingScriptEngineWithInvocableAndAutocloseable(T delegate) {
public DelegatingScriptEngineWithInvocableAndAutocloseable(@NonNull T delegate) {
this.delegate = delegate;
}

@Override
public @Nullable Object eval(String s, ScriptContext scriptContext) throws ScriptException {
return Objects.nonNull(delegate) ? delegate.eval(s, scriptContext) : null;
public Object eval(String s, ScriptContext scriptContext) throws ScriptException {
return delegate.eval(s, scriptContext);
}

@Override
public @Nullable Object eval(Reader reader, ScriptContext scriptContext) throws ScriptException {
return Objects.nonNull(delegate) ? delegate.eval(reader, scriptContext) : null;
public Object eval(Reader reader, ScriptContext scriptContext) throws ScriptException {
return delegate.eval(reader, scriptContext);
}

@Override
public @Nullable Object eval(String s) throws ScriptException {
return Objects.nonNull(delegate) ? delegate.eval(s) : null;
public Object eval(String s) throws ScriptException {
return delegate.eval(s);
}

@Override
public @Nullable Object eval(Reader reader) throws ScriptException {
return Objects.nonNull(delegate) ? delegate.eval(reader) : null;
public Object eval(Reader reader) throws ScriptException {
return delegate.eval(reader);
}

@Override
public @Nullable Object eval(String s, Bindings bindings) throws ScriptException {
return Objects.nonNull(delegate) ? delegate.eval(s, bindings) : null;
public Object eval(String s, Bindings bindings) throws ScriptException {
return delegate.eval(s, bindings);
}

@Override
public @Nullable Object eval(Reader reader, Bindings bindings) throws ScriptException {
return Objects.nonNull(delegate) ? delegate.eval(reader, bindings) : null;
public Object eval(Reader reader, Bindings bindings) throws ScriptException {
return delegate.eval(reader, bindings);
}

@Override
public void put(String s, Object o) {
if (Objects.nonNull(delegate))
delegate.put(s, o);
delegate.put(s, o);
}

@Override
public @Nullable Object get(String s) {
return Objects.nonNull(delegate) ? delegate.get(s) : null;
public Object get(String s) {
return delegate.get(s);
}

@Override
public @Nullable Bindings getBindings(int i) {
return Objects.nonNull(delegate) ? delegate.getBindings(i) : null;
public Bindings getBindings(int i) {
return delegate.getBindings(i);
}

@Override
public void setBindings(Bindings bindings, int i) {
if (Objects.nonNull(delegate))
delegate.setBindings(bindings, i);
delegate.setBindings(bindings, i);
}

@Override
public @Nullable Bindings createBindings() {
return Objects.nonNull(delegate) ? delegate.createBindings() : null;
public Bindings createBindings() {
return delegate.createBindings();
}

@Override
public @Nullable ScriptContext getContext() {
return Objects.nonNull(delegate) ? delegate.getContext() : null;
public ScriptContext getContext() {
return delegate.getContext();
}

@Override
public void setContext(ScriptContext scriptContext) {
if (Objects.nonNull(delegate))
delegate.setContext(scriptContext);
delegate.setContext(scriptContext);
}

@Override
public @Nullable ScriptEngineFactory getFactory() {
return Objects.nonNull(delegate) ? delegate.getFactory() : null;
public ScriptEngineFactory getFactory() {
return delegate.getFactory();
}

@Override
public @Nullable Object invokeMethod(Object o, String s, Object... objects)
throws ScriptException, NoSuchMethodException {
return Objects.nonNull(delegate) ? delegate.invokeMethod(o, s, objects) : null;
public Object invokeMethod(Object o, String s, Object... objects) throws ScriptException, NoSuchMethodException {
return delegate.invokeMethod(o, s, objects);
}

@Override
public @Nullable Object invokeFunction(String s, Object... objects) throws ScriptException, NoSuchMethodException {
return Objects.nonNull(delegate) ? delegate.invokeFunction(s, objects) : null;
public Object invokeFunction(String s, Object... objects) throws ScriptException, NoSuchMethodException {
return delegate.invokeFunction(s, objects);
}

@Override
Expand All @@ -135,7 +130,6 @@ public <T> T getInterface(Object o, Class<T> aClass) {

@Override
public void close() throws Exception {
if (Objects.nonNull(delegate))
delegate.close();
delegate.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public long setTimeout(Runnable callback, Long delay) {
* @return Positive integer value which identifies the timer created; this value can be passed to
* <code>clearTimeout()</code> to cancel the timeout.
*/
public long setTimeout(Runnable callback, Long delay, Object... args) {
public long setTimeout(Runnable callback, Long delay, @Nullable Object... args) {
long id = lastId.incrementAndGet();
ScheduledCompletableFuture<Object> future = scheduler.schedule(() -> {
try {
Expand Down Expand Up @@ -166,7 +166,7 @@ public long setInterval(Runnable callback, Long delay) {
* @return Numeric, non-zero value which identifies the timer created; this value can be passed to
* <code>clearInterval()</code> to cancel the interval.
*/
public long setInterval(Runnable callback, Long delay, Object... args) {
public long setInterval(Runnable callback, Long delay, @Nullable Object... args) {
long id = lastId.incrementAndGet();
ScheduledCompletableFuture<Object> future = scheduler.schedule(() -> {
try {
Expand Down

0 comments on commit e9708bb

Please sign in to comment.