@Test public void testMemberCount_16() { String typeName = TestEnum.class.getName(); JvmEnumerationType type = (JvmEnumerationType) getTypeProvider().findTypeByName(typeName); int innerTypesCount = TestEnum.class.getDeclaredClasses().length; // Nested assertEquals(1, innerTypesCount); int methodCount = TestEnum.class.getDeclaredMethods().length; // TestEnum.values + TestEnum.valueOf assertEquals(2, methodCount); int constructorCount = TestEnum.class.getDeclaredConstructors().length; // TestEnum(String, int, String), TestEnum(String, int, String, EnumType) assertEquals(2, constructorCount); int fieldCount = TestEnum.class.getDeclaredFields().length; // FirstValue, SecondValue, string, ENUM$VALUES assertEquals(Arrays.toString(TestEnum.class.getDeclaredFields()), 4, fieldCount); // ENUM$VALUES is synthetic // TestEnum(String, String, EnumType) is synthetic assertEquals(type.getMembers().toString(), innerTypesCount + methodCount + constructorCount + fieldCount - 2, type.getMembers().size()); }
@Test public void testEnum_04() throws Exception { String typeName = TestEnum.class.getName(); JvmEnumerationType type = (JvmEnumerationType) getTypeProvider().findTypeByName(typeName); List<JvmMember> members = type.getMembers(); boolean constructorFound = false; for (JvmMember member : members) { if (member instanceof JvmConstructor) { assertFalse(constructorFound); constructorFound = true; List<JvmFormalParameter> parameters = ((JvmConstructor) member).getParameters(); assertEquals(1, parameters.size()); // synthetic parameters are not returned } } assertTrue(constructorFound); }
@Test public void testNestedEnum_04() throws Exception { String typeName = TestEnum.Nested.class.getName(); JvmEnumerationType type = (JvmEnumerationType) getTypeProvider().findTypeByName(typeName); List<JvmMember> members = type.getMembers(); boolean constructorFound = false; for (JvmMember member : members) { if (member instanceof JvmConstructor) { assertFalse(constructorFound); constructorFound = true; List<JvmFormalParameter> parameters = ((JvmConstructor) member).getParameters(); assertEquals(0, parameters.size()); // synthetic parameters are not returned } } assertTrue(constructorFound); }
@Test public void testStubGeneration_03() { StringConcatenation _builder = new StringConcatenation(); _builder.append("public enum MyTest {"); _builder.newLine(); _builder.append("\t"); _builder.append("FOO, BAR"); _builder.newLine(); _builder.append("}"); _builder.newLine(); final Procedure1<IResourceDescription> _function = (IResourceDescription it) -> { Assert.assertEquals("MyTest", IterableExtensions.<IEObjectDescription>head(it.getExportedObjects()).getQualifiedName().toString()); EObject _eObjectOrProxy = IterableExtensions.<IEObjectDescription>head(it.getExportedObjects()).getEObjectOrProxy(); Assert.assertTrue((_eObjectOrProxy instanceof JvmEnumerationType)); Assert.assertEquals(1, IterableExtensions.size(it.getExportedObjects())); }; this.resultsIn(_builder, _function); }
@Override public boolean validate(IAcceptor<? super AbstractDiagnostic> result) { JvmType type = (JvmType) description.getElementOrProxy(); String typeKind = ""; if (type instanceof JvmPrimitiveType || type instanceof JvmVoid) { typeKind = "primitive type"; } else if (type instanceof JvmAnnotationType) { typeKind = "annotation type"; } else if (type instanceof JvmEnumerationType) { typeKind = "enum type"; } else if (type instanceof JvmGenericType && ((JvmGenericType) type).isInterface()) { typeKind = "interface type"; } else if (type instanceof JvmTypeParameter) { typeKind = "type parameter"; } String message = String.format("Cannot instantiate the %s %s", typeKind, type.getSimpleName()); AbstractDiagnostic diagnostic = new EObjectDiagnosticImpl(Severity.ERROR, IssueCodes.ILLEGAL_CLASS_INSTANTIATION, message, getExpression(), XbasePackage.Literals.XCONSTRUCTOR_CALL__CONSTRUCTOR, -1, null); result.accept(diagnostic); return false; }
public void complete(JvmIdentifiableElement element) { if (element instanceof JvmGenericType) { completeJvmGenericType((JvmGenericType)element); } if (element instanceof JvmDeclaredType) { JvmDeclaredType declaredType = (JvmDeclaredType) element; complete(declaredType.getMembers()); } if(element instanceof JvmConstructor) { completeJvmConstructor((JvmConstructor) element); } if (element instanceof JvmEnumerationType) { completeJvmEnumerationType((JvmEnumerationType)element); } if (element instanceof JvmEnumerationLiteral) { completeJvmEnumerationLiteral((JvmEnumerationLiteral)element); } if (element instanceof JvmAnnotationType) { completeJvmAnnotationType((JvmAnnotationType)element); } }
protected Iterable<JvmMember> _getMembersToBeCompiled(final JvmEnumerationType type) { Iterable<JvmMember> _xblockexpression = null; { String _identifier = type.getIdentifier(); String _plus = (_identifier + "."); String _plus_1 = (_plus + "valueOf(java.lang.String)"); String _identifier_1 = type.getIdentifier(); String _plus_2 = (_identifier_1 + "."); String _plus_3 = (_plus_2 + "values()"); final Set<String> syntheticEnumMethods = Collections.<String>unmodifiableSet(CollectionLiterals.<String>newHashSet(_plus_1, _plus_3)); final Function1<JvmMember, Boolean> _function = (JvmMember it) -> { return Boolean.valueOf((!((it instanceof JvmOperation) && syntheticEnumMethods.contains(it.getIdentifier())))); }; _xblockexpression = IterableExtensions.<JvmMember>filter(type.getMembers(), _function); } return _xblockexpression; }
@Test public void testEnumerationWithCompleter() { try { final XExpression expression = this.expression("null", false); final Procedure1<JvmEnumerationType> _function = (JvmEnumerationType it) -> { EList<JvmMember> _members = it.getMembers(); JvmEnumerationLiteral _enumerationLiteral = this.builder.toEnumerationLiteral(expression, "BAR"); this.builder.<JvmEnumerationLiteral>operator_add(_members, _enumerationLiteral); EList<JvmMember> _members_1 = it.getMembers(); JvmEnumerationLiteral _enumerationLiteral_1 = this.builder.toEnumerationLiteral(expression, "BAZ"); this.builder.<JvmEnumerationLiteral>operator_add(_members_1, _enumerationLiteral_1); }; final JvmEnumerationType enumeration = this.builder.toEnumerationType(expression, "my.test.Foo", _function); expression.eResource().getContents().add(enumeration); this.completer.complete(enumeration); final Class<?> compiled = this.compile(expression.eResource(), enumeration); final Method valuesMethod = compiled.getMethod("values"); Object _invoke = valuesMethod.invoke(null); final Object[] values = ((Object[]) _invoke); Assert.assertEquals("BAR", values[0].toString()); Assert.assertEquals("BAZ", values[1].toString()); } catch (Throwable _e) { throw Exceptions.sneakyThrow(_e); } }
protected JvmEnumerationType createEnumerationType(Class<?> clazz) { JvmEnumerationType result = TypesFactory.eINSTANCE.createJvmEnumerationType(); result.internalSetIdentifier(clazz.getName()); result.setSimpleName(clazz.getSimpleName()); if (clazz.getDeclaringClass() == null && clazz.getPackage() != null) result.setPackageName(clazz.getPackage().getName()); setVisibility(clazz, result); setTypeModifiers(clazz, result); createNestedTypes(clazz, result); createMethods(clazz, result); createFields(clazz, result); createConstructors(clazz, result); setSuperTypes(clazz, result); createAnnotationValues(clazz, result); return result; }
@Test public void testEnum_01() throws Exception { String typeName = TestEnum.class.getName(); JvmType type = getTypeProvider().findTypeByName(typeName); assertNotNull(type); assertTrue(type instanceof JvmEnumerationType); assertTrue(((JvmEnumerationType) type).isFinal()); assertFalse(((JvmEnumerationType) type).isStatic()); diagnose(type); Resource resource = type.eResource(); getAndResolveAllFragments(resource); recomputeAndCheckIdentifiers(resource); }
@Test public void testEnum_02() throws Exception { String typeName = TestEnum.class.getName(); JvmEnumerationType type = (JvmEnumerationType) getTypeProvider().findTypeByName(typeName); assertEquals(1, type.getSuperTypes().size()); assertEquals(Enum.class.getName(), type.getSuperTypes().get(0).getType().getIdentifier()); String superTypeName = Enum.class.getName() + "<" + typeName + ">"; assertEquals(superTypeName, type.getSuperTypes().get(0).getIdentifier()); }
@Test public void testEnum_03() throws Exception { String typeName = TestEnum.class.getName(); JvmEnumerationType type = (JvmEnumerationType) getTypeProvider().findTypeByName(typeName); assertEquals(2, type.getLiterals().size()); Set<String> expectedLiterals = Sets.newHashSet(TestEnum.FirstValue.name(), TestEnum.SecondValue.name()); for (JvmEnumerationLiteral literal : type.getLiterals()) { assertTrue(expectedLiterals.remove(literal.getSimpleName())); assertSame(type, literal.getEnumType()); assertEquals(JvmVisibility.PUBLIC, literal.getVisibility()); } assertTrue(expectedLiterals.toString(), expectedLiterals.isEmpty()); }
@Test public void testEnum_05() throws Exception { String typeName = TestEnum.class.getName(); JvmEnumerationType type = (JvmEnumerationType) getTypeProvider().findTypeByName(typeName); JvmOperation valuesOperation = (JvmOperation) Iterables.getOnlyElement(type.findAllFeaturesByName("values")); String qualifiedReturnValue = valuesOperation.getReturnType().getQualifiedName(); assertEquals(typeName + "[]", qualifiedReturnValue); }
@Test public void testEnum_06() throws Exception { String typeName = TestEnum.class.getName(); JvmEnumerationType type = (JvmEnumerationType) getTypeProvider().findTypeByName(typeName); JvmOperation valuesOperation = (JvmOperation) Iterables.getFirst(type.findAllFeaturesByName("valueOf"), null); String qualifiedReturnValue = valuesOperation.getReturnType().getQualifiedName(); assertEquals(typeName, qualifiedReturnValue); }
@Test public void testNestedEnum_01() throws Exception { String typeName = TestEnum.Nested.class.getName(); JvmType type = getTypeProvider().findTypeByName(typeName); assertNotNull(type); assertTrue(type instanceof JvmEnumerationType); assertTrue(((JvmEnumerationType) type).isFinal()); assertTrue(((JvmEnumerationType) type).isStatic()); diagnose(type); Resource resource = type.eResource(); getAndResolveAllFragments(resource); recomputeAndCheckIdentifiers(resource); }
@Test public void testNestedEnum_02() throws Exception { String typeName = TestEnum.Nested.class.getName(); JvmEnumerationType type = (JvmEnumerationType) getTypeProvider().findTypeByName(typeName); assertEquals(1, type.getSuperTypes().size()); assertEquals(Enum.class.getName(), type.getSuperTypes().get(0).getType().getIdentifier()); String superTypeName = Enum.class.getName() + "<" + typeName + ">"; assertEquals(superTypeName, type.getSuperTypes().get(0).getIdentifier()); }
@Test public void testNestedEnum_03() throws Exception { String typeName = TestEnum.Nested.class.getName(); JvmEnumerationType type = (JvmEnumerationType) getTypeProvider().findTypeByName(typeName); assertEquals(1, type.getLiterals().size()); Set<String> expectedLiterals = Sets.newHashSet(TestEnum.Nested.SINGLETON.name()); for (JvmEnumerationLiteral literal : type.getLiterals()) { assertTrue(expectedLiterals.remove(literal.getSimpleName())); assertSame(type, literal.getEnumType()); assertEquals(JvmVisibility.PUBLIC, literal.getVisibility()); } assertTrue(expectedLiterals.toString(), expectedLiterals.isEmpty()); }
@Test public void testNestedEnum_05() throws Exception { String typeName = ClassContainingEnum.InnerEnumWithInt.class.getName(); JvmType type = getTypeProvider().findTypeByName(typeName); assertNotNull(type); assertTrue(type instanceof JvmEnumerationType); assertTrue(((JvmEnumerationType) type).isFinal()); assertTrue(((JvmEnumerationType) type).isStatic()); diagnose(type); Resource resource = type.eResource(); getAndResolveAllFragments(resource); recomputeAndCheckIdentifiers(resource); }
/** * Creates a public enum declaration, associated to the given sourceElement. It sets the given name, which might be * fully qualified using the standard Java notation. * * @param sourceElement * the sourceElement the resulting element is associated with. * @param name * the qualified name of the resulting enum type. * @param initializer * the initializer to apply on the created enumeration type. If <code>null</code>, the enum won't be initialized. * * @return a result representing a Java enum type with the given name, <code>null</code> * if sourceElement or name are <code>null</code>. */ /* @Nullable */ public JvmEnumerationType toEnumerationType(/* @Nullable */ EObject sourceElement, /* @Nullable */ String name, /* @Nullable */ Procedure1<? super JvmEnumerationType> initializer) { if (sourceElement == null || name == null) return null; Pair<String, String> fullName = splitQualifiedName(name); JvmEnumerationType result = typesFactory.createJvmEnumerationType(); result.setSimpleName(fullName.getSecond()); result.setVisibility(JvmVisibility.PUBLIC); if (fullName.getFirst() != null) result.setPackageName(fullName.getFirst()); associate(sourceElement, result); return initializeSafely(result, initializer); }
protected void completeJvmEnumerationType(JvmEnumerationType element) { if (element.getSuperTypes().isEmpty()) { JvmTypeReference objectType = references.getTypeForName(Enum.class, element, references.createTypeRef(element)); if (objectType != null) element.getSuperTypes().add(objectType); } addAnnotations(element); EObject primarySourceElement = associations.getPrimarySourceElement(element); JvmOperation values = typesFactory.createJvmOperation(); values.setVisibility(JvmVisibility.PUBLIC); values.setStatic(true); values.setSimpleName("values"); values.setReturnType(references.createArrayType(references.createTypeRef(element))); typeExtensions.setSynthetic(values, true); if (primarySourceElement != null) { associator.associate(primarySourceElement, values); } element.getMembers().add(values); JvmOperation valueOf = typesFactory.createJvmOperation(); valueOf.setVisibility(JvmVisibility.PUBLIC); valueOf.setStatic(true); valueOf.setSimpleName("valueOf"); valueOf.setReturnType(references.createTypeRef(element)); JvmFormalParameter param = typesFactory.createJvmFormalParameter(); param.setName("name"); param.setParameterType(references.getTypeForName(String.class, element)); valueOf.getParameters().add(param); typeExtensions.setSynthetic(valueOf, true); if (primarySourceElement != null) { associator.associate(primarySourceElement, valueOf); } element.getMembers().add(valueOf); }
public ITreeAppendable generateBody(final JvmDeclaredType it, final ITreeAppendable appendable, final GeneratorConfig config) { if (it instanceof JvmAnnotationType) { return _generateBody((JvmAnnotationType)it, appendable, config); } else if (it instanceof JvmEnumerationType) { return _generateBody((JvmEnumerationType)it, appendable, config); } else if (it instanceof JvmGenericType) { return _generateBody((JvmGenericType)it, appendable, config); } else { throw new IllegalArgumentException("Unhandled parameter types: " + Arrays.<Object>asList(it, appendable, config).toString()); } }
public Iterable<JvmMember> getMembersToBeCompiled(final JvmDeclaredType type) { if (type instanceof JvmEnumerationType) { return _getMembersToBeCompiled((JvmEnumerationType)type); } else if (type instanceof JvmGenericType) { return _getMembersToBeCompiled((JvmGenericType)type); } else if (type != null) { return _getMembersToBeCompiled(type); } else { throw new IllegalArgumentException("Unhandled parameter types: " + Arrays.<Object>asList(type).toString()); } }
@Test public void testInitializeSafely_5() { final Runnable _function = () -> { final Function3<EObject, String, Procedure1<? super JvmEnumerationType>, EObject> _function_1 = (EObject expr, String name, Procedure1<? super JvmEnumerationType> init) -> { return this._jvmTypesBuilder.toEnumerationType(expr, name, init); }; this.<JvmEnumerationType>genericTestInitializeSafely(_function_1); }; this.expectErrorLogging(2, _function); }
protected void setTypeModifiers(Class<?> clazz, JvmDeclaredType result) { int modifiers = clazz.getModifiers(); result.setAbstract(Modifier.isAbstract(modifiers)); result.setStatic(Modifier.isStatic(modifiers)); if (!(result instanceof JvmEnumerationType)) result.setFinal(Modifier.isFinal(modifiers)); }
/** * <!-- begin-user-doc --> * <!-- end-user-doc --> * @generated */ public JvmEnumerationType getEnumType() { // TODO: implement this method // Ensure that you remove @generated or mark it @generated NOT throw new UnsupportedOperationException(); }
protected ImageDescriptor imageDescriptor(final Object at) { if (at instanceof JvmConstructor) { return _imageDescriptor((JvmConstructor)at); } else if (at instanceof JvmOperation) { return _imageDescriptor((JvmOperation)at); } else if (at instanceof JvmAnnotationType) { return _imageDescriptor((JvmAnnotationType)at); } else if (at instanceof JvmEnumerationType) { return _imageDescriptor((JvmEnumerationType)at); } else if (at instanceof JvmField) { return _imageDescriptor((JvmField)at); } else if (at instanceof JvmGenericType) { return _imageDescriptor((JvmGenericType)at); } else if (at instanceof JvmTypeParameter) { return _imageDescriptor((JvmTypeParameter)at); } else if (at instanceof JvmFormalParameter) { return _imageDescriptor((JvmFormalParameter)at); } else if (at instanceof XVariableDeclaration) { return _imageDescriptor((XVariableDeclaration)at); } else if (at instanceof AlgorithmType) { return _imageDescriptor((AlgorithmType)at); } else if (at instanceof GraphPolicies) { return _imageDescriptor((GraphPolicies)at); } else if (at instanceof Model) { return _imageDescriptor((Model)at); } else if (at instanceof PathGeneratorStopCondition) { return _imageDescriptor((PathGeneratorStopCondition)at); } else if (at instanceof Policies) { return _imageDescriptor((Policies)at); } else if (at instanceof Severity) { return _imageDescriptor((Severity)at); } else if (at instanceof StopCondition) { return _imageDescriptor((StopCondition)at); } else if (at instanceof IResolvedConstructor) { return _imageDescriptor((IResolvedConstructor)at); } else if (at instanceof IResolvedOperation) { return _imageDescriptor((IResolvedOperation)at); } else if (at instanceof XImportDeclaration) { return _imageDescriptor((XImportDeclaration)at); } else if (at instanceof XImportSection) { return _imageDescriptor((XImportSection)at); } else if (at instanceof IResolvedField) { return _imageDescriptor((IResolvedField)at); } else if (at != null) { return _imageDescriptor(at); } else { throw new IllegalArgumentException("Unhandled parameter types: " + Arrays.<Object>asList(at).toString()); } }
/** * @deprecated override {@link #highlightReferenceJvmType(IHighlightedPositionAcceptor, EObject, EReference, EObject)} * or {@link #highlightFeature(IHighlightedPositionAcceptor, EObject, org.eclipse.emf.ecore.EStructuralFeature, String...)} * in order to customize the coloring of references of {@link JvmType JvmTypes}. */ @Deprecated protected void highlightReferenceJvmType(IHighlightedPositionAcceptor acceptor, EObject referencer, EReference reference, EObject resolvedReferencedObject, String highlightingConfiguration) { highlightDeprecation(acceptor, referencer, reference, resolvedReferencedObject); final Object referencersContainingFeature = referencer.eContainingFeature(); if (resolvedReferencedObject instanceof JvmTypeParameter) { // may happen in cast expressions highlightFeature(acceptor, referencer, reference, TYPE_VARIABLE); } else if (referencer instanceof JvmParameterizedTypeReference && (referencersContainingFeature == TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__ARGUMENTS || referencersContainingFeature == TypesPackage.Literals.JVM_TYPE_CONSTRAINT__TYPE_REFERENCE || referencersContainingFeature == XbasePackage.Literals.XABSTRACT_FEATURE_CALL__TYPE_ARGUMENTS || referencersContainingFeature == XbasePackage.Literals.XCONSTRUCTOR_CALL__TYPE_ARGUMENTS)) { // case 1: 'referencer' is a type reference within the arguments reference of another (parameterized) type reference // 'referencer' definitely is a type argument and to be colored as such // (if 'resolvedReferencedObject' is not a type parameter, which is tested above) // case 2: type reference is nested in a JvmWildcardTypeReference -> JvmTypeConstraint // case 3: the type reference is part of the type arguments of a method call if (resolvedReferencedObject instanceof JvmEnumerationType) { highlightFeature(acceptor, referencer, TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE, ENUM); } else if (resolvedReferencedObject instanceof JvmGenericType) { highlightFeature(acceptor, referencer, TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE, getStyle((JvmGenericType) resolvedReferencedObject)); } else if (resolvedReferencedObject instanceof JvmAnnotationType) { highlightFeature(acceptor, referencer, TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE, highlightingConfiguration); } highlightFeature(acceptor, referencer, TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE, TYPE_ARGUMENT); } else if (resolvedReferencedObject instanceof JvmDeclaredType) { if (referencer instanceof XImportDeclaration) { // don't highlight import statements return; } else if (resolvedReferencedObject instanceof JvmEnumerationType) { highlightFeature(acceptor, referencer, reference, ENUM); } else if (resolvedReferencedObject instanceof JvmGenericType) { highlightFeature(acceptor, referencer, reference, getStyle((JvmGenericType) resolvedReferencedObject)); } else if (resolvedReferencedObject instanceof JvmAnnotationType) { highlightFeature(acceptor, referencer, reference, highlightingConfiguration); } } }
@Override public JvmEnumerationType getEnumType() { return (JvmEnumerationType) getType().getType(); }