Java Code Examples for org.eclipse.xtext.common.types.JvmConstructor#setSimpleName()

The following examples show how to use org.eclipse.xtext.common.types.JvmConstructor#setSimpleName() . 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: JvmModelCompleter.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void completeJvmGenericType(JvmGenericType element) {
	// if no super type add Object
	ensureSuperTypeObject(element);
	addAnnotations(element);
	if (!element.isInterface()) {
		// if no constructors have been added, add a default constructor
		if (isEmpty(element.getDeclaredConstructors())) {
			JvmConstructor constructor = TypesFactory.eINSTANCE.createJvmConstructor();
			constructor.setSimpleName(element.getSimpleName());
			constructor.setVisibility(JvmVisibility.PUBLIC);
			typeExtensions.setSynthetic(constructor, true);
			EObject primarySourceElement = associations.getPrimarySourceElement(element);
			if (primarySourceElement != null) {
				associator.associate(primarySourceElement, constructor);
			}
			element.getMembers().add(constructor);
		}
	}
}
 
Example 2
Source File: XtendJvmModelInferrer.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected void transform(XtendConstructor source, JvmGenericType container) {
	JvmConstructor constructor = typesFactory.createJvmConstructor();
	container.getMembers().add(constructor);
	associator.associatePrimary(source, constructor);
	JvmVisibility visibility = source.getVisibility();
	constructor.setSimpleName(container.getSimpleName());
	constructor.setVisibility(visibility);
	for (XtendParameter parameter : source.getParameters()) {
		translateParameter(constructor, parameter);
	}
	copyAndFixTypeParameters(source.getTypeParameters(), constructor);
	for (JvmTypeReference exception : source.getExceptions()) {
		constructor.getExceptions().add(jvmTypesBuilder.cloneWithProxies(exception));
	}
	translateAnnotationsTo(source.getAnnotations(), constructor);
	setBody(constructor, source.getExpression());
	jvmTypesBuilder.copyDocumentationTo(source, constructor);
}
 
Example 3
Source File: JvmTypeDeclarationImpl.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
public MutableConstructorDeclaration addConstructor(final Procedure1<MutableConstructorDeclaration> initializer) {
  this.checkMutable();
  Preconditions.checkArgument((initializer != null), "initializer cannot be null");
  final Function1<JvmConstructor, Boolean> _function = (JvmConstructor it) -> {
    return Boolean.valueOf(this.getCompilationUnit().getTypeExtensions().isSingleSyntheticDefaultConstructor(it));
  };
  final JvmConstructor constructor = IterableExtensions.<JvmConstructor>findFirst(Iterables.<JvmConstructor>filter(this.getDelegate().getMembers(), JvmConstructor.class), _function);
  if ((constructor != null)) {
    EcoreUtil.remove(constructor);
  }
  final JvmConstructor newConstructor = TypesFactory.eINSTANCE.createJvmConstructor();
  newConstructor.setVisibility(JvmVisibility.PUBLIC);
  newConstructor.setSimpleName(this.getSimpleName());
  this.getDelegate().getMembers().add(newConstructor);
  MemberDeclaration _memberDeclaration = this.getCompilationUnit().toMemberDeclaration(newConstructor);
  final MutableConstructorDeclaration mutableConstructorDeclaration = ((MutableConstructorDeclaration) _memberDeclaration);
  initializer.apply(mutableConstructorDeclaration);
  return mutableConstructorDeclaration;
}
 
Example 4
Source File: JvmModelCompleter.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public void completeJvmConstructor(JvmConstructor constructor) {
	JvmDeclaredType declaringType = constructor.getDeclaringType();
	if(declaringType != null) {
		String simpleName = declaringType.getSimpleName();
		if(simpleName != null) {
			constructor.setSimpleName(simpleName);
			return;
		}
	}
	constructor.setSimpleName("unset");
}
 
Example 5
Source File: AbstractConstructorScopeTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testGetElementByInstance_01() {
	JvmConstructor constructor = TypesFactory.eINSTANCE.createJvmConstructor();
	JvmGenericType type = TypesFactory.eINSTANCE.createJvmGenericType();
	type.setPackageName("java.lang");
	type.setSimpleName("Object");
	constructor.setSimpleName("Object");
	type.getMembers().add(constructor);
	IEObjectDescription element = getConstructorScope().getSingleElement(constructor);
	assertNotNull(element);
	assertEquals(new IQualifiedNameConverter.DefaultImpl().toQualifiedName("java.lang.Object"), element.getName());
	assertEquals(new IQualifiedNameConverter.DefaultImpl().toQualifiedName("java.lang.Object"), element.getQualifiedName());
}
 
Example 6
Source File: AbstractConstructorScopeTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testGetElementByInstance_01() {
	JvmConstructor constructor = TypesFactory.eINSTANCE.createJvmConstructor();
	JvmGenericType type = TypesFactory.eINSTANCE.createJvmGenericType();
	type.setPackageName("java.lang");
	type.setSimpleName("Object");
	constructor.setSimpleName("Object");
	type.getMembers().add(constructor);
	IEObjectDescription element = getConstructorScope().getSingleElement(constructor);
	assertNotNull(element);
	assertEquals(new IQualifiedNameConverter.DefaultImpl().toQualifiedName("java.lang.Object"), element.getName());
	assertEquals(new IQualifiedNameConverter.DefaultImpl().toQualifiedName("java.lang.Object"), element.getQualifiedName());
}
 
Example 7
Source File: XtendReentrantTypeResolver.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected JvmConstructor inferAnonymousClassConstructor(AnonymousClass anonymousClass, JvmGenericType inferredLocalClass, JvmConstructor superConstructor, boolean visible) {
	JvmConstructor constructor = TypesFactory.eINSTANCE.createJvmConstructor();
	inferredLocalClass.getMembers().add(constructor);
	associator.associatePrimary(anonymousClass.getConstructorCall(), constructor);
	if (visible) {
		constructor.setVisibility(JvmVisibility.DEFAULT);
	} else
		constructor.setVisibility(JvmVisibility.PRIVATE);
	constructor.setSimpleName(inferredLocalClass.getSimpleName());
	constructor.setVarArgs(superConstructor.isVarArgs());
	final List<JvmFormalParameter> parameters = superConstructor.getParameters();
	for(JvmFormalParameter parameter: parameters) {
		parameter.getName(); // trigger name computation
		constructor.getParameters().add(typesBuilder.cloneWithProxies(parameter));
	}
	
	for (JvmTypeReference exception : superConstructor.getExceptions()) 
		constructor.getExceptions().add(typesBuilder.cloneWithProxies(exception));
	
	if (!parameters.isEmpty()) {
		typesBuilder.setBody(constructor, new Procedures.Procedure1<ITreeAppendable>() {
			@Override
			public void apply(ITreeAppendable a) {
				a.append("super(");
				for(int i = 0; i < parameters.size(); i++) {
					if (i != 0) {
						a.append(", ");
					}
					a.append(parameters.get(i).getSimpleName());
				}
				a.append(");");
			}
			
		});
	}
	return constructor;
}
 
Example 8
Source File: XtendReentrantTypeResolver.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected JvmConstructor inferAnonymousClassConstructor(AnonymousClass anonymousClass,
		JvmGenericType inferredLocalClass) {
	XConstructorCall constructorCall = anonymousClass.getConstructorCall();
	JvmConstructor constructor = TypesFactory.eINSTANCE.createJvmConstructor();
	inferredLocalClass.getMembers().add(constructor);
	associator.associatePrimary(constructorCall, constructor);
	constructor.setVisibility(JvmVisibility.DEFAULT);
	constructor.setSimpleName(inferredLocalClass.getSimpleName());
	return constructor;
}
 
Example 9
Source File: XtendJvmModelInferrer.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected void addDefaultConstructor(XtendClass source, JvmGenericType target) {
	for (XtendMember member : source.getMembers()) {
		if (member instanceof XtendConstructor)
			return;
	}
	JvmConstructor constructor = typesFactory.createJvmConstructor();
	target.getMembers().add(constructor);
	associator.associate(source, constructor);
	constructor.setSimpleName(source.getName());
	constructor.setVisibility(JvmVisibility.PUBLIC);
	typeExtensions.setSynthetic(constructor, true);
}