Java Code Examples for org.eclipse.xtext.common.types.JvmGenericType#setInterface()

The following examples show how to use org.eclipse.xtext.common.types.JvmGenericType#setInterface() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XbaseResourceDescriptionStrategyTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testInterfaceDescription_01() {
	JvmGenericType interfaceType = TypesFactory.eINSTANCE.createJvmGenericType();
	interfaceType.setInterface(true);
	interfaceType.setPackageName("foo");
	interfaceType.setSimpleName("MyType");
	List<IEObjectDescription> list = new ArrayList<>();
	descriptionStrategy.createEObjectDescriptions(interfaceType, it -> list.add(it));
	Assert.assertTrue(
			list.stream().anyMatch(it -> "true".equals(it.getUserData(JvmTypesResourceDescriptionStrategy.IS_INTERFACE))));
}
 
Example 2
Source File: XbaseResourceDescriptionStrategyTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testInterfaceDescription_02() {
	JvmGenericType interfaceType = TypesFactory.eINSTANCE.createJvmGenericType();
	interfaceType.setInterface(false);
	interfaceType.setPackageName("foo");
	interfaceType.setSimpleName("MyType");
	List<IEObjectDescription> list = new ArrayList<>();
	descriptionStrategy.createEObjectDescriptions(interfaceType, it -> list.add(it));
	Assert.assertFalse(list.stream().anyMatch(
			it -> "true".equals(it.getUserData(JvmTypesResourceDescriptionStrategy.IS_INTERFACE))));
}
 
Example 3
Source File: RegisterGlobalsContextImpl.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void registerInterface(final String qualifiedName) throws IllegalArgumentException {
  final JvmGenericType newType = TypesFactory.eINSTANCE.createJvmGenericType();
  newType.setVisibility(JvmVisibility.PUBLIC);
  newType.setInterface(true);
  this.setNameAndAccept(newType, qualifiedName);
}
 
Example 4
Source File: JvmDeclaredTypeBuilder.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void visit(
       final int version,
       final int access,
       final String name,
       final String signature,
       final String superName,
       final String[] interfaces)
   {
   	if ((access & ACC_SYNTHETIC) != 0)
		throw new IllegalStateException("Cannot create type for anonymous or synthetic classes");
   	if ((ACC_ENUM & access) != 0) {
   		result = TypesFactory.eINSTANCE.createJvmEnumerationType();
   		offset = 2;
   	} else if ((ACC_ANNOTATION & access) != 0) {
   		result = TypesFactory.eINSTANCE.createJvmAnnotationType();
   	} else {
   		JvmGenericType generic = TypesFactory.eINSTANCE.createJvmGenericType(); 
   		result = generic;

   		generic.setInterface((access & ACC_INTERFACE) != 0);
   		generic.setStrictFloatingPoint((access & ACC_STRICT) != 0);
   	}
   	setTypeModifiers(access);
   	
   	proxies.setVisibility(access, result);
   	
   	setNameAndPackage(name);

   	BinarySuperTypeSignature genericSignature = null;
	if (signature != null) {
		if ((access & (ACC_STATIC | ACC_INTERFACE)) != 0) {
			typeParameters = Collections.emptyMap();
		}
		genericSignature = BinarySignatures.createSuperTypeSignature(signature);
		if (((ACC_ENUM | ACC_ANNOTATION) & access) == 0) {
			typeParameters = proxies.createTypeParameters(genericSignature, (JvmTypeParameterDeclarator) result, typeParameters);
		}
	}
	setSuperTypes(name, genericSignature, superName, interfaces);
   }
 
Example 5
Source File: JdtBasedTypeFactory.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @since 2.4
 */
protected JvmDeclaredType createType(ITypeBinding typeBinding, String handleIdentifier, List<String> path, StringBuilder fqn) {
	if (typeBinding.isAnonymous() || typeBinding.isSynthetic())
		throw new IllegalStateException("Cannot create type for anonymous or synthetic classes");

	// Creates the right type of instance based on the type of binding.
	//
	JvmGenericType jvmGenericType;
	JvmDeclaredType result;
	if (typeBinding.isAnnotation()) {
		jvmGenericType = null;
		result = TypesFactory.eINSTANCE.createJvmAnnotationType();
	} else if (typeBinding.isEnum()) {
		jvmGenericType = null;
		result = TypesFactory.eINSTANCE.createJvmEnumerationType();
	} else {
		result = jvmGenericType = TypesFactory.eINSTANCE.createJvmGenericType();
		jvmGenericType.setInterface(typeBinding.isInterface());
	}

	// Populate the information computed from the modifiers.
	//
	int modifiers = typeBinding.getModifiers();
	setTypeModifiers(result, modifiers);
	result.setDeprecated(typeBinding.isDeprecated());
	setVisibility(result, modifiers);

	// Determine the simple name and compose the fully qualified name and path, remembering the fqn length and path size so we can reset them.
	//
	String simpleName = typeBinding.getName();
	fqn.append(simpleName);
	int length = fqn.length();
	int size = path.size();
	path.add(simpleName);

	String qualifiedName = fqn.toString();
	result.internalSetIdentifier(qualifiedName);
	result.setSimpleName(simpleName);

	// Traverse the nested types using '$' as the qualified name separator.
	//
	fqn.append('$');
	createNestedTypes(typeBinding, result, handleIdentifier, path, fqn);

	// Traverse the methods using '.'as the qualifed name separator.
	//
	fqn.setLength(length);
	fqn.append('.');
	createMethods(typeBinding, handleIdentifier, path, fqn, result);
	
	createFields(typeBinding, fqn, result);

	// Set the super types.
	//
	setSuperTypes(typeBinding, qualifiedName, result);

	// If this is for a generic type, populate the type parameters.
	//
	if (jvmGenericType != null) {
		ITypeBinding[] typeParameterBindings = typeBinding.getTypeParameters();
		if (typeParameterBindings.length > 0) {
			InternalEList<JvmTypeParameter> typeParameters = (InternalEList<JvmTypeParameter>)jvmGenericType.getTypeParameters();
			for (ITypeBinding variable : typeParameterBindings) {
				typeParameters.addUnique(createTypeParameter(variable, result));
			}
		}
	}

	// Populate the annotation values.
	//
	createAnnotationValues(typeBinding, result);

	// Restore the path.
	//
	path.remove(size);

	return result;
}
 
Example 6
Source File: SARLJvmModelInferrer.java    From sarl with Apache License 2.0 4 votes vote down vote up
/** Initialize the SARL capacity context-aware wrapper.
 *
 * @param source the source.
 * @param inferredJvmType the JVM type.
 * @since 0.6
 */
protected void appendCapacityContextAwareWrapper(SarlCapacity source, JvmGenericType inferredJvmType) {
	final JvmGenericType innerType = this.typesFactory.createJvmGenericType();
	innerType.setInterface(false);
	innerType.setAbstract(false);
	innerType.setVisibility(JvmVisibility.PUBLIC);
	innerType.setStatic(true);
	innerType.setStrictFloatingPoint(false);
	innerType.setFinal(false);
	final String innerTypeName = Capacity.ContextAwareCapacityWrapper.class.getSimpleName();
	innerType.setSimpleName(innerTypeName);

	inferredJvmType.getMembers().add(innerType);

	this.typeBuilder.setDocumentation(innerType, "@ExcludeFromApidoc"); //$NON-NLS-1$

	final JvmTypeParameter typeParameter = this.typesFactory.createJvmTypeParameter();
	typeParameter.setName("C"); //$NON-NLS-1$
	final JvmUpperBound constraint = this.typesFactory.createJvmUpperBound();
	constraint.setTypeReference(this._typeReferenceBuilder.typeRef(inferredJvmType));
	typeParameter.getConstraints().add(constraint);
	innerType.getTypeParameters().add(typeParameter);

	final Iterator<JvmTypeReference> extendedTypeIterator = inferredJvmType.getExtendedInterfaces().iterator();
	if (extendedTypeIterator.hasNext()) {
		final JvmTypeReference extendedType = extendedTypeIterator.next();
		final JvmTypeReference superType = this._typeReferenceBuilder.typeRef(
				extendedType.getQualifiedName() + "$" + innerTypeName, //$NON-NLS-1$
				this._typeReferenceBuilder.typeRef(typeParameter));
		innerType.getSuperTypes().add(superType);
	}

	innerType.getSuperTypes().add(this._typeReferenceBuilder.typeRef(inferredJvmType));

	final JvmConstructor constructor = this.typesFactory.createJvmConstructor();
	constructor.setVisibility(JvmVisibility.PUBLIC);
	innerType.getMembers().add(constructor);
	final JvmFormalParameter parameter1 = this.typesFactory.createJvmFormalParameter();
	parameter1.setName("capacity"); //$NON-NLS-1$
	parameter1.setParameterType(this._typeReferenceBuilder.typeRef(typeParameter));
	constructor.getParameters().add(parameter1);
	final JvmFormalParameter parameter2 = this.typesFactory.createJvmFormalParameter();
	parameter2.setName("caller"); //$NON-NLS-1$
	parameter2.setParameterType(this._typeReferenceBuilder.typeRef(AgentTrait.class));
	constructor.getParameters().add(parameter2);
	setBody(constructor, it -> {
		it.append("super(capacity, caller);"); //$NON-NLS-1$
	});

	final Set<ActionPrototype> createdActions = new TreeSet<>();
	for (final JvmGenericType sourceType : Iterables.concat(
			Collections.singletonList(inferredJvmType),
			Iterables.transform(Iterables.skip(inferredJvmType.getExtendedInterfaces(), 1), it -> {
				return (JvmGenericType) it.getType();
			}))) {
		copyNonStaticPublicJvmOperations(sourceType, innerType, createdActions, (operation, it) -> {
			it.append("try {"); //$NON-NLS-1$
			it.newLine();
			it.append("  ensureCallerInLocalThread();"); //$NON-NLS-1$
			it.newLine();
			it.append("  "); //$NON-NLS-1$
			if (operation.getReturnType() != null && !Objects.equal("void", operation.getReturnType().getIdentifier())) { //$NON-NLS-1$
				it.append("return "); //$NON-NLS-1$
			}
			it.append("this.capacity."); //$NON-NLS-1$
			it.append(operation.getSimpleName());
			it.append("("); //$NON-NLS-1$
			boolean first = true;
			for (final JvmFormalParameter fparam : operation.getParameters()) {
				if (first) {
					first = false;
				} else {
					it.append(", "); //$NON-NLS-1$
				}
				it.append(fparam.getName());
			}
			it.append(");"); //$NON-NLS-1$
			it.newLine();
			it.append("} finally {"); //$NON-NLS-1$
			it.newLine();
			it.append("  resetCallerInLocalThread();"); //$NON-NLS-1$
			it.newLine();
			it.append("}"); //$NON-NLS-1$
		});
	}
}
 
Example 7
Source File: JvmTypesBuilder.java    From xtext-extras with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Creates a public interface 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 class.
 * @param initializer
 *            the initializer to apply on the created interface element. If <code>null</code>, the interface won't be initialized.
 * 
 * @return a {@link JvmGenericType} representing a Java class of the given name, <code>null</code> 
 *            if sourceElement or name are <code>null</code>.
 */
/* @Nullable */ 
public JvmGenericType toInterface(/* @Nullable */ EObject sourceElement, /* @Nullable */ String name, /* @Nullable */ Procedure1<? super JvmGenericType> initializer) {
	final JvmGenericType result = createJvmGenericType(sourceElement, name);
	if (result == null)
		return null;
	result.setInterface(true);
	result.setAbstract(true);
	associate(sourceElement, result);
	return initializeSafely(result, initializer);
}