Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Added null annotations to transformation services #5365

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src/test/java"/>
<classpathentry combineaccessrules="false" kind="src" path="/org.eclipse.smarthome.core.transform"/>
<classpathentry kind="output" path="target/test-classes"/>
</classpath>
33 changes: 33 additions & 0 deletions bundles/core/org.eclipse.smarthome.core.transform.test/.project
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>org.eclipse.smarthome.core.transform.test</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ManifestBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.SchemaBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ds.core.builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
@@ -0,0 +1,19 @@
Manifest-Version: 1.0
Bundle-ClassPath: .
Bundle-ManifestVersion: 2
Bundle-Name: Eclipse SmartHome Core Transform Tests
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-SymbolicName: org.eclipse.smarthome.core.transform.test
Bundle-Vendor: Eclipse.org/SmartHome
Bundle-Version: 0.10.0.qualifier
Fragment-Host: org.eclipse.smarthome.core.transform
Import-Package:
org.eclipse.jdt.annotation;resolution:=optional,
org.eclipse.smarthome.test,
org.eclipse.smarthome.test.java,
org.hamcrest;core=split,
org.junit,
org.junit.rules,
org.junit.runner,
org.junit.runners,
org.osgi.service.cm
19 changes: 19 additions & 0 deletions bundles/core/org.eclipse.smarthome.core.transform.test/NOTICE
@@ -0,0 +1,19 @@
This content is produced and maintained by the Eclipse SmartHome project.

* Project home: https://eclipse.org/smarthome/

== Declared Project Licenses

This program and the accompanying materials are made available under the terms
of the Eclipse Public License 2.0 which is available at
https://www.eclipse.org/legal/epl-2.0/.

== Source Code

https://github.com/eclipse/smarthome

== Copyright Holders

See the NOTICE file distributed with the source code at
https://github.com/eclipse/smarthome/blob/master/NOTICE
for detailed information regarding copyright ownership.
@@ -0,0 +1,5 @@
source.. = src/test/java/
output.. = target/test-classes/
bin.includes = META-INF/,\
.,\
NOTICE
106 changes: 106 additions & 0 deletions bundles/core/org.eclipse.smarthome.core.transform.test/pom.xml
@@ -0,0 +1,106 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>core</artifactId>
<groupId>org.eclipse.smarthome.bundles</groupId>
<version>0.10.0-SNAPSHOT</version>
</parent>
<groupId>org.eclipse.smarthome.core</groupId>
<artifactId>org.eclipse.smarthome.core.transform.test</artifactId>

<packaging>eclipse-test-plugin</packaging>

<name>Eclipse SmartHome Core Transform Test</name>

<properties>
<bundle.symbolicName>org.eclipse.smarthome.core.transform.test</bundle.symbolicName>
<bundle.namespace>org.eclipse.smarthome.core.transform.test</bundle.namespace>
</properties>

<build>
<plugins>
<plugin>
<groupId>${tycho-groupid}</groupId>
<artifactId>target-platform-configuration</artifactId>
<configuration>
<dependency-resolution>
<extraRequirements>
<requirement>
<type>eclipse-plugin</type>
<id>org.eclipse.equinox.ds</id>
<versionRange>0.0.0</versionRange>
</requirement>
<requirement>
<type>eclipse-plugin</type>
<id>org.eclipse.equinox.event</id>
<versionRange>0.0.0</versionRange>
</requirement>
<requirement>
<type>eclipse-plugin</type>
<id>org.eclipse.smarthome.core.thing</id>
<versionRange>0.0.0</versionRange>
</requirement>
<requirement>
<type>eclipse-plugin</type>
<id>org.eclipse.smarthome.core.transform</id>
<versionRange>0.0.0</versionRange>
</requirement>
<requirement>
<type>eclipse-plugin</type>
<id>org.eclipse.equinox.cm</id>
<versionRange>0.0.0</versionRange>
</requirement>
<requirement>
<type>eclipse-plugin</type>
<id>org.eclipse.smarthome.core</id>
<versionRange>0.0.0</versionRange>
</requirement>
</extraRequirements>
</dependency-resolution>
</configuration>
</plugin>
<plugin>
<groupId>${tycho-groupid}</groupId>
<artifactId>tycho-surefire-plugin</artifactId>
<configuration>
<bundleStartLevel>
<bundle>
<id>org.eclipse.equinox.ds</id>
<level>1</level>
<autoStart>true</autoStart>
</bundle>
<bundle>
<id>org.eclipse.smarthome.core.transform</id>
<level>4</level>
<autoStart>true</autoStart>
</bundle>
<bundle>
<id>org.eclipse.smarthome.core.thing</id>
<level>4</level>
<autoStart>true</autoStart>
</bundle>

<bundle>
<id>org.eclipse.equinox.event</id>
<level>2</level>
<autoStart>true</autoStart>
</bundle>
<bundle>
<id>org.eclipse.equinox.cm</id>
<level>2</level>
<autoStart>true</autoStart>
</bundle>
<bundle>
<id>org.eclipse.smarthome.core</id>
<level>3</level>
<autoStart>true</autoStart>
</bundle>
</bundleStartLevel>
</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,36 @@
/**
* Copyright (c) 2014,2018 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.smarthome.core.transform.actions;

import static org.junit.Assert.assertEquals;

import org.eclipse.smarthome.core.transform.TransformationException;
import org.junit.Test;

public class TransformationTest {

@Test
public void testTransform() {
String result = Transformation.transform("UnknownTransformation", "function", "test");
assertEquals("test", result);
}

@Test
public void testTransformRaw() {
try {
Transformation.transformRaw("UnknownTransformation", "function", "test");
} catch (TransformationException e) {
assertEquals("No transformation service 'UnknownTransformation' could be found.", e.getMessage());
}
}
}
Expand Up @@ -29,6 +29,8 @@
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.io.FilenameUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.config.core.ConfigConstants;
import org.eclipse.smarthome.core.i18n.LocaleProvider;
import org.osgi.framework.BundleContext;
Expand All @@ -49,16 +51,20 @@
* @author Kai Kreuzer - File caching mechanism
* @author Markus Rathgeb - Add locale provider support
*/
@NonNullByDefault
public abstract class AbstractFileTransformationService<T> implements TransformationService {

@Nullable
private WatchService watchService = null;

protected final Map<String, T> cachedFiles = new ConcurrentHashMap<>();
protected final List<String> watchedDirectories = new ArrayList<String>();

private final Logger logger = LoggerFactory.getLogger(AbstractFileTransformationService.class);

@NonNullByDefault({})
private LocaleProvider localeProvider;
@NonNullByDefault({})
private ServiceTracker<LocaleProvider, LocaleProvider> localeProviderTracker;

private class LocaleProviderServiceTrackerCustomizer
Expand All @@ -71,17 +77,17 @@ public LocaleProviderServiceTrackerCustomizer(final BundleContext context) {
}

@Override
public LocaleProvider addingService(ServiceReference<LocaleProvider> reference) {
public LocaleProvider addingService(@Nullable ServiceReference<LocaleProvider> reference) {
localeProvider = context.getService(reference);
return localeProvider;
}

@Override
public void modifiedService(ServiceReference<LocaleProvider> reference, LocaleProvider service) {
public void modifiedService(@Nullable ServiceReference<LocaleProvider> reference, LocaleProvider service) {
}

@Override
public void removedService(ServiceReference<LocaleProvider> reference, LocaleProvider service) {
public void removedService(@Nullable ServiceReference<LocaleProvider> reference, LocaleProvider service) {
localeProvider = null;
}

Expand Down Expand Up @@ -114,7 +120,7 @@ protected Locale getLocale() {
* @throws TransformationException
*/
@Override
public String transform(String filename, String source) throws TransformationException {
public @Nullable String transform(String filename, String source) throws TransformationException {
if (filename == null || source == null) {
throw new TransformationException("the given parameters 'filename' and 'source' must not be null");
}
Expand Down
Expand Up @@ -12,12 +12,15 @@
*/
package org.eclipse.smarthome.core.transform;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* A TransformationException is thrown when any step of a transformation went
* wrong. The originating exception should be attached to increase traceability.
*
* @author Thomas.Eichstaedt-Engelen
*/
@NonNullByDefault
public class TransformationException extends Exception {

/** generated serial Version UID */
Expand Down
Expand Up @@ -17,6 +17,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
Expand All @@ -27,6 +29,7 @@
*
* @author Kai Kreuzer - Initial contribution
*/
@NonNullByDefault
public class TransformationHelper {

private static final Logger LOGGER = LoggerFactory.getLogger(TransformationHelper.class);
Expand All @@ -48,10 +51,12 @@ public static boolean isTransform(String pattern) {
* Queries the OSGi service registry for a service that provides a transformation service of
* a given transformation type (e.g. REGEX, XSLT, etc.)
*
* @param context the bundle context which can be null
* @param transformationType the desired transformation type
* @return a service instance or null, if none could be found
*/
public static TransformationService getTransformationService(BundleContext context, String transformationType) {
public static @Nullable TransformationService getTransformationService(@Nullable BundleContext context,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you have added the Nullable annotation to the contect argument, I tried to check the JavaDoc for that parameter. There is no such parameter in JavaDoc. As you already touched this one can you add one?

String transformationType) {
if (context != null) {
String filter = "(smarthome.transform=" + transformationType + ")";
try {
Expand All @@ -77,10 +82,10 @@ public static TransformationService getTransformationService(BundleContext conte
* @param context a valid bundle context, required for accessing the services
* @param stateDescPattern the pattern that contains the transformation instructions
* @param state the state to be formatted before being passed into the transformation function
* @return the result of the transformation
* @return the result of the transformation. If no transformation was done, <code>null</code> is returned
* @throws TransformationException if transformation service is not available or the transformation failed
*/
public static String transform(BundleContext context, String stateDescPattern, String state)
public static @Nullable String transform(BundleContext context, String stateDescPattern, String state)
throws TransformationException {
Matcher matcher = EXTRACT_TRANSFORMFUNCTION_PATTERN.matcher(stateDescPattern);
if (matcher.find()) {
Expand Down
Expand Up @@ -12,6 +12,9 @@
*/
package org.eclipse.smarthome.core.transform;

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

/**
* A TransformationProcessor transforms a given input and returns the transformed
* result. Transformations could make sense in various situations, for example:
Expand All @@ -27,24 +30,23 @@
* @author Kai Kreuzer - Initial contribution and API
*
*/
@NonNullByDefault
public interface TransformationService {



public static final String TRANSFORM_FOLDER_NAME = "transform";

/**
* Transforms the input <code>source</code> by means of the given <code>function</code> and returns the transformed
* output. If the transformation couldn't be completed
* for any reason, one should return the unchanged <code>source</code>. This
* method should never return <code>null</code>. In case of any error an {@link TransformationException} should be
* thrown.
*
* output. The transformation may return <code>null</code> to express its operation resulted in a <code>null</code>
* output. In case of any error an {@link TransformationException} should be thrown.
*
* @param function the function to be used to transform the input
* @param source the input to be transformed
* @return the transformed result or the unchanged <code>source</code> if the
* transformation couldn't be completed for any reason.
* @return the transformed result or <code>null</code> if the
* transformation's output is <code>null</code>.
* @throws TransformationException if any error occurs
*/
@Nullable
String transform(String function, String source) throws TransformationException;

}