blob: 16d0586798549dc57dc03c21bfa159e27202874d [file] [log] [blame]
/**
* Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
*
*
* This copyright notice shows up in the generated Java code
*/
package org.eclipse.osbp.utils.annotation;
import java.util.ArrayList;
import java.util.Arrays;
import javax.inject.Inject;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.common.types.JvmAnnotationReference;
import org.eclipse.xtext.common.types.JvmAnnotationValue;
import org.eclipse.xtext.common.types.JvmBooleanAnnotationValue;
import org.eclipse.xtext.common.types.JvmEnumAnnotationValue;
import org.eclipse.xtext.common.types.JvmEnumerationLiteral;
import org.eclipse.xtext.common.types.JvmOperation;
import org.eclipse.xtext.common.types.JvmStringAnnotationValue;
import org.eclipse.xtext.common.types.JvmTypeAnnotationValue;
import org.eclipse.xtext.common.types.JvmTypeReference;
import org.eclipse.xtext.common.types.TypesFactory;
@SuppressWarnings("all")
public class CommonUtils {
@Inject
private TypesFactory typesFactory;
public void addStringValuesToAnnotation(final JvmAnnotationReference annotationRef, final ArrayList<String> annotationStringList) {
JvmStringAnnotationValue value = this.typesFactory.createJvmStringAnnotationValue();
for (final String annotationString : annotationStringList) {
EList<String> _values = value.getValues();
_values.add(annotationString);
}
EList<JvmAnnotationValue> _explicitValues = annotationRef.getExplicitValues();
_explicitValues.add(value);
}
/**
* Creates a string annotation value and adds it to the given annotation reference
*/
protected JvmAnnotationValue _addAnnAttr(final JvmAnnotationReference annRef, final EObject context, final String name, final String stringValue) {
final String[] stringValues = { stringValue };
return this.addAnnAttrArr(annRef, context, name, stringValues);
}
/**
* Creates a string annotation value and adds it to the given annotation reference
*/
public JvmStringAnnotationValue addAnnAttrArr(final JvmAnnotationReference annRef, final EObject context, final String name, final String[] stringValues) {
final JvmStringAnnotationValue value = this.typesFactory.createJvmStringAnnotationValue();
EList<JvmAnnotationValue> _explicitValues = annRef.getExplicitValues();
_explicitValues.add(value);
final JvmOperation op = this.typesFactory.createJvmOperation();
op.setSimpleName(name);
value.setOperation(op);
for (final String stringValue : stringValues) {
EList<String> _values = value.getValues();
_values.add(stringValue);
}
return value;
}
/**
* Creates a boolean annotation value and adds it to the given annotation reference
*/
protected JvmAnnotationValue _addAnnAttr(final JvmAnnotationReference annRef, final EObject context, final String name, final boolean booleanValue) {
final JvmBooleanAnnotationValue value = this.typesFactory.createJvmBooleanAnnotationValue();
EList<JvmAnnotationValue> _explicitValues = annRef.getExplicitValues();
_explicitValues.add(value);
final JvmOperation op = this.typesFactory.createJvmOperation();
op.setSimpleName(name);
value.setOperation(op);
EList<Boolean> _values = value.getValues();
_values.add(Boolean.valueOf(booleanValue));
return value;
}
/**
* Creates a string annotation value and adds it to the given annotation reference
*/
protected JvmAnnotationValue _addAnnAttr(final JvmAnnotationReference annRef, final EObject context, final String name, final JvmTypeReference typeValue) {
final JvmTypeAnnotationValue value = this.typesFactory.createJvmTypeAnnotationValue();
EList<JvmAnnotationValue> _explicitValues = annRef.getExplicitValues();
_explicitValues.add(value);
final JvmOperation op = this.typesFactory.createJvmOperation();
op.setSimpleName(name);
value.setOperation(op);
EList<JvmTypeReference> _values = value.getValues();
_values.add(typeValue);
return value;
}
/**
* Creates an enum annotation value and adds it to the given annotation reference
*/
protected JvmAnnotationValue _addAnnAttr(final JvmAnnotationReference annRef, final EObject context, final String name, final Enum<?>... enums) {
final JvmEnumAnnotationValue value = this.typesFactory.createJvmEnumAnnotationValue();
EList<JvmAnnotationValue> _explicitValues = annRef.getExplicitValues();
_explicitValues.add(value);
for (final Enum<?> enumxx : enums) {
{
final JvmOperation op = this.typesFactory.createJvmOperation();
op.setSimpleName(name);
value.setOperation(op);
final JvmEnumerationLiteral literal = this.typesFactory.createJvmEnumerationLiteral();
literal.setSimpleName(enumxx.name());
EList<JvmEnumerationLiteral> _values = value.getValues();
_values.add(literal);
}
}
return value;
}
public JvmAnnotationValue addAnnAttr(final JvmAnnotationReference annRef, final EObject context, final String name, final Object typeValue) {
if (typeValue instanceof JvmTypeReference) {
return _addAnnAttr(annRef, context, name, (JvmTypeReference)typeValue);
} else if (typeValue instanceof Boolean) {
return _addAnnAttr(annRef, context, name, (Boolean)typeValue);
} else if (typeValue instanceof String) {
return _addAnnAttr(annRef, context, name, (String)typeValue);
} else if (typeValue instanceof Enum[]) {
return _addAnnAttr(annRef, context, name, (Enum<?>[])typeValue);
} else {
throw new IllegalArgumentException("Unhandled parameter types: " +
Arrays.<Object>asList(annRef, context, name, typeValue).toString());
}
}
}