Commit 97131c75 authored by Daniel Bluhm's avatar Daniel Bluhm

Cleanup for PR submission

Signed-off-by: Daniel Bluhm's avatarDaniel Bluhm <bluhmdj@ornl.gov>
parent 77c77fd4
/*******************************************************************************
* Copyright (c) 2020- UT-Battelle, LLC.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Michael Walsh - Initial implementation
*******************************************************************************/
package org.eclipse.ice.dev.annotations.processors;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeMirror;
import org.apache.commons.lang3.ClassUtils;
import org.eclipse.ice.data.IDataElement;
/**
* Util class passed to the Velocity template engine that enables
* logic to be executed through Java rather than templating syntax
*
* @author Michael Walsh
*
*/
public class VelocityUtils {
/**
* Utility class necessary for certain class meta data reading during templating
*/
private ProcessingEnvironment processingEnvironment;
/**
* Constructor with processingEnvironment parameter necessary for {@link #isIDataElement(TypeMirror)}
* @param processingEnvironment
*/
public VelocityUtils(ProcessingEnvironment processingEnvironment) {
this.processingEnvironment = processingEnvironment;
}
/**
* Util method that attempts to determine the parameterized type of a given
* TypeMirror
*
* @param type A TypeMirror that has a Parameterized Type e.g.
* List<ParameterizedType>
* @return TypeMirror The ParamterizedType's TypeMirror or null if it does not
* exist
*/
public static TypeMirror getCollectionParameterType(TypeMirror type) {
if (type != null && DeclaredType.class.isAssignableFrom(type.getClass())
&& ((DeclaredType) type).getTypeArguments() != null
&& !((DeclaredType) type).getTypeArguments().isEmpty()) {
return ((DeclaredType) type).getTypeArguments().get(0);
}
return null;
}
/**
* Get the Class object the parameter type represents, if it is null or not a
* {@link javax.lang.model.type.DeclaredType} then this method returns null
* @param type Any given TypeMirror
* @return Class The actual class the TypeMirror type represents
* @throws ClassNotFoundException
*/
public static Class<?> getClass(TypeMirror type) throws ClassNotFoundException {
if (type != null && DeclaredType.class.isAssignableFrom(type.getClass())) {
return ClassUtils.getClass(((DeclaredType) type).asElement().toString());
}
return null;
}
/**
* Util method for collection all IDataElement and IDataElement collections in a
* given list of fields
*
* @param fields to be filtered
* @return a list of {@link IDataElement} (or collections of) contained within the given list
*/
public List<Field> collectAllDataElementFields(List<Field> fields) {
return fields.stream()
.filter(field -> field.getMirror() != null && isIDataElementOrCollectionOf(field.getMirror()))
.collect(Collectors.toList());
}
/**
* Util method for checking if there are any fields of type IDataElement or a
* collection of IDataElements in a given list of fields
*
* @param fields to be searched
* @return boolean true if any results are found, false if none
*/
public boolean anyDataElementsExist(List<Field> fields) {
return fields.stream()
.anyMatch(field -> field.getMirror() != null && isIDataElementOrCollectionOf(field.getMirror()));
}
/**
* Checks if the given TypeMirror type represents an IDataElement
*
* @param type any TypeMirror
* @return boolean true if it or its parent classes are of type {@link IDataElement}, else false
*/
public boolean isIDataElement(TypeMirror type) {
if (type != null) {
if (type.toString().contains(IDataElement.class.getSimpleName())) {
return true;
}
for (TypeMirror supertype : processingEnvironment.getTypeUtils().directSupertypes(type)) {
DeclaredType declared = (DeclaredType) supertype;
Element supertypeElement = declared.asElement();
if (supertypeElement.asType().toString().contains(IDataElement.class.getSimpleName())) {
return true;
}
}
}
return false;
}
/**
* Checks if a given TypeMirror type represents an IDataElement or a
* parameterized collection of IDataElement's
*
* @param type any given TypeMirror
* @return boolean true if it is an {@link IDataElement} or collection of {@link IDataElement}, else false
*/
public boolean isIDataElementOrCollectionOf(TypeMirror type) {
return this.isIDataElement(type)
|| (getCollectionParameterType(type) != null && this.isIDataElement(getCollectionParameterType(type)));
}
}
package org.eclipse.ice.tests.dev.annotations.processors;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Types;
import org.eclipse.ice.data.IDataElement;
import org.eclipse.ice.dev.annotations.processors.Field;
import org.eclipse.ice.dev.annotations.processors.VelocityUtils;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import junit.framework.Assert;
/**
* Class that runs a battery of tests through the methods of the
* utility class {@link org.eclipse.ice.dev.annotations.processors.VelocityUtils}
*
* @author Michael Walsh
*
*/
@SuppressWarnings("deprecation")
public class VelocityUtilsTest {
/**
* Class to be tested
*/
private VelocityUtils utils;
/**
* Mocked class used in VelocityUtils
*/
private Types typeUtils;
/**
* Performs mocks/setup necessary for most tests
*/
@Before
public void setup() {
ProcessingEnvironment env = Mockito.mock(ProcessingEnvironment.class);
typeUtils = Mockito.mock(Types.class);
Mockito.when(env.getTypeUtils()).thenReturn(typeUtils);
utils = new VelocityUtils(env);
}
/**
* Test for {@link org.eclipse.ice.dev.annotations.processors.VelocityUtils#getCollectionParameterType(TypeMirror)}
* This test ensures the expected result of parameter extraction executes successfully
*/
@Test
public void testGetCollectionParameterType() {
DeclaredType type = Mockito.mock(DeclaredType.class);
TypeMirror mirror = Mockito.mock(TypeMirror.class);
List typeArguments = Arrays.asList(mirror);
Mockito.when(type.getTypeArguments()).thenReturn(typeArguments);
Assert.assertEquals(mirror, utils.getCollectionParameterType(type));
}
/**
* Test for {@link org.eclipse.ice.dev.annotations.processors.VelocityUtils#getClass(TypeMirror)}
* Ensures that, if given a {@link javax.lang.model.type.DeclaredType} it will return the class extracted from it.
* @throws ClassNotFoundException from {@link org.eclipse.ice.dev.annotations.processors.VelocityUtils#getClass(TypeMirror)}
*/
@Test
public void testGetClass() throws ClassNotFoundException {
DeclaredType type = Mockito.mock(DeclaredType.class);
Element elem = Mockito.mock(Element.class);
Mockito.when(elem.toString()).thenReturn(Element.class.getCanonicalName());
Mockito.when(type.asElement()).thenReturn(elem);
Assert.assertEquals(Element.class, VelocityUtils.getClass(type));
}
/**
* Test for {@link org.eclipse.ice.dev.annotations.processors.VelocityUtils#collectAllDataElementFields(List)}
* Given a list of {@link org.eclipse.ice.dev.annotations.processors.Field}, test that it returns all fields of type
* {@link org.eclipse.ice.data.IDataElement} from the list
*/
@Test
public void testCollectAllDataElementFields() {
List<Field> fields = new ArrayList<>();
Field field = Mockito.mock(Field.class);
TypeMirror type = Mockito.mock(TypeMirror.class);
Mockito.when(type.toString()).thenReturn(IDataElement.class.getSimpleName());
Mockito.when(field.getMirror()).thenReturn(type);
fields.add(field);
Assert.assertEquals(field, utils.collectAllDataElementFields(fields).get(0));
}
/**
* Test for {@link org.eclipse.ice.dev.annotations.processors.VelocityUtils#anyDataElementsExist(List)}
* Given a list of {@link org.eclipse.ice.dev.annotations.processors.Field}, test that it returns true if there
* exists at least one {@link org.eclipse.ice.data.IDataElement} in the list
*/
@Test
public void testAnyDataElementsExist() {
List<Field> fields = new ArrayList<>();
Field field = Mockito.mock(Field.class);
TypeMirror type = Mockito.mock(TypeMirror.class);
Mockito.when(type.toString()).thenReturn(IDataElement.class.getSimpleName());
Mockito.when(field.getMirror()).thenReturn(type);
fields.add(field);
Assert.assertTrue(utils.anyDataElementsExist(fields));
}
/**
* Test for {@link org.eclipse.ice.dev.annotations.processors.VelocityUtils#isIDataElement(TypeMirror)}
* Given a {@link javax.lang.model.type.TypeMirror}, test that the method correctly looks at the supertypes to determine
* that a class implements {@link org.eclipse.ice.data.IDataElement}
*/
@Test
public void testIsIDataElement() {
TypeMirror baseType = Mockito.mock(TypeMirror.class);
DeclaredType type = Mockito.mock(DeclaredType.class);
Element elem = Mockito.mock(Element.class);
Mockito.when(baseType.toString()).thenReturn("nuh uh");
Mockito.when(type.toString()).thenReturn(IDataElement.class.getSimpleName());
Mockito.when(elem.asType()).thenReturn(type);
Mockito.when(elem.toString()).thenReturn(Element.class.getCanonicalName());
Mockito.when(type.asElement()).thenReturn(elem);
List typeArguments = Arrays.asList(type);
Mockito.when(typeUtils.directSupertypes(Mockito.any())).thenReturn(typeArguments);
Assert.assertTrue(utils.isIDataElement(baseType));
}
}
......@@ -51,6 +51,5 @@
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="/org.eclipse.ice.dev.annotations"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
......@@ -29,25 +29,6 @@
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>copy-frontend</id>
<phase>compile</phase>
<goals><goal>copy-resources</goal></goals>
<configuration>
<outputDirectory>${project.build.directory}/classes/META-INF/frontend</outputDirectory>
<resources>
<resource>
<directory>${project.build.directory}/generated-sources/annotations/frontend</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
......
......@@ -188,25 +188,6 @@
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>copy-frontend</id>
<phase>compile</phase>
<goals><goal>copy-resources</goal></goals>
<configuration>
<outputDirectory>frontend/src</outputDirectory>
<resources>
<resource>
<directory>${project.build.directory}/generated-sources/annotations/frontend</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
......
......@@ -2,14 +2,12 @@
* This file has been autogenerated as it didn't exist or was made for an older incompatible version.
* This file can be used for manual configuration will not be modified if the flowDefaults constant exists.
*/
const path = require('path');
const merge = require('webpack-merge');
const flowDefaults = require('./webpack.generated.js');
module.exports = merge(flowDefaults, {
resolve: {
extensions: ['.ts', '.tsx', '.js', '.json'],
alias: {DataElement$: path.resolve(__dirname, 'frontend/src/data-element.ts')}
extensions: ['.ts', '.tsx', '.js', '.json']
},
devtool: 'inline-source-map',
module: {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment