org.eclipse.xtext.xbase.typesystem.override.IResolvedConstructor Java Examples

The following examples show how to use org.eclipse.xtext.xbase.typesystem.override.IResolvedConstructor. 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: OverrideProposalUtil.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void addConstructorCandidates(ResolvedFeatures resolvedFeatures, IVisibilityHelper visibilityHelper,
		List<IResolvedExecutable> result) {
	LightweightTypeReference typeReference = resolvedFeatures.getType();
	List<LightweightTypeReference> superTypes = typeReference.getSuperTypes();
	for (LightweightTypeReference superType : superTypes) {
		if (!superType.isInterfaceType()) {
			List<IResolvedConstructor> declaredConstructors = resolvedFeatures.getDeclaredConstructors();
			Set<String> erasedSignatures = Sets.<String>newHashSet();
			for (IResolvedConstructor constructor : declaredConstructors) {
				erasedSignatures.add(constructor.getResolvedErasureSignature());
			}
			ResolvedFeatures superClass = overrideHelper.getResolvedFeatures(superType);
			for (IResolvedConstructor superclassConstructor : superClass.getDeclaredConstructors()) {
				IResolvedConstructor overriddenConstructor = new ResolvedConstructor(
						superclassConstructor.getDeclaration(), typeReference);
				if (isCandidate(typeReference, overriddenConstructor, visibilityHelper)) {
					if (erasedSignatures.add(superclassConstructor.getResolvedErasureSignature())) {
						result.add(overriddenConstructor);
					}
				}
			}
			return;
		}
	}
}
 
Example #2
Source File: ResolvedFeaturesTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testSoftReferenceConstructors() {
  final ResolvedFeatures resolvedOperations = this.toResolvedOperations(SoftReference.class);
  Assert.assertEquals(1, resolvedOperations.getDeclaredOperations().size());
  Assert.assertEquals(2, resolvedOperations.getDeclaredConstructors().size());
  final Consumer<IResolvedConstructor> _function = (IResolvedConstructor it) -> {
    int _size = it.getDeclaration().getParameters().size();
    switch (_size) {
      case 1:
        Assert.assertEquals("SoftReference(T)", it.getResolvedSignature());
        Assert.assertEquals("SoftReference(java.lang.Object)", it.getResolvedErasureSignature());
        break;
      case 2:
        Assert.assertEquals("SoftReference(T,java.lang.ref.ReferenceQueue<? super T>)", it.getResolvedSignature());
        Assert.assertEquals("SoftReference(java.lang.Object,java.lang.ref.ReferenceQueue)", it.getResolvedErasureSignature());
        break;
      default:
        Assert.fail(("Unexpected constructor: " + it));
        break;
    }
  };
  resolvedOperations.getDeclaredConstructors().forEach(_function);
}
 
Example #3
Source File: ResolvedFeaturesTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testSoftReferenceOfString() {
  final ResolvedFeatures resolvedOperations = this.toResolvedOperations("null as java.lang.ref.SoftReference<String>");
  Assert.assertEquals(1, resolvedOperations.getDeclaredOperations().size());
  Assert.assertEquals(2, resolvedOperations.getDeclaredConstructors().size());
  final Consumer<IResolvedConstructor> _function = (IResolvedConstructor it) -> {
    int _size = it.getDeclaration().getParameters().size();
    switch (_size) {
      case 1:
        Assert.assertEquals("SoftReference(java.lang.String)", it.getResolvedSignature());
        Assert.assertEquals("SoftReference(java.lang.String)", it.getResolvedErasureSignature());
        break;
      case 2:
        Assert.assertEquals("SoftReference(java.lang.String,java.lang.ref.ReferenceQueue<? super java.lang.String>)", it.getResolvedSignature());
        Assert.assertEquals("SoftReference(java.lang.String,java.lang.ref.ReferenceQueue)", it.getResolvedErasureSignature());
        break;
      default:
        Assert.fail(("Unexpected constructor: " + it));
        break;
    }
  };
  resolvedOperations.getDeclaredConstructors().forEach(_function);
}
 
Example #4
Source File: MemberFromSuperImplementor.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
public void appendConstructorFromSuper(final XtendClass overrider, final IResolvedConstructor superConstructor, final ISourceAppender appendable) {
  final JvmGenericType inferredType = this.associations.getInferredType(overrider);
  final AbstractConstructorBuilder constructorBuilder = this.codeBuilderFactory.createConstructorBuilder(inferredType);
  this.initializeExecutableBuilder(constructorBuilder, inferredType, superConstructor);
  final Procedure1<ISourceAppender> _function = (ISourceAppender it) -> {
    boolean _isEmpty = superConstructor.getResolvedParameterTypes().isEmpty();
    boolean _not = (!_isEmpty);
    if (_not) {
      StringConcatenation _builder = new StringConcatenation();
      _builder.append("super(");
      final Function1<JvmFormalParameter, String> _function_1 = (JvmFormalParameter it_1) -> {
        return it_1.getSimpleName();
      };
      String _join = IterableExtensions.join(ListExtensions.<JvmFormalParameter, String>map(superConstructor.getDeclaration().getParameters(), _function_1), ", ");
      _builder.append(_join);
      _builder.append(")");
      it.append(_builder);
    }
  };
  constructorBuilder.setBodyGenerator(_function);
  boolean _isValid = constructorBuilder.isValid();
  if (_isValid) {
    constructorBuilder.build(appendable);
  }
}
 
Example #5
Source File: XbaseLabelProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @return the {@code ImageDescriptor} for a given {@code obj}
 * @throws NullPointerException
 *             if the passed {@code obj} is null
 */
protected ImageDescriptor imageDescriptor(Object obj) {
	Preconditions.checkNotNull(obj);

	if (obj instanceof JvmConstructor) {
		return _imageDescriptor((JvmConstructor) obj);
	} else if (obj instanceof JvmOperation) {
		return _imageDescriptor((JvmOperation) obj);
	} else if (obj instanceof JvmAnnotationType) {
		return _imageDescriptor((JvmAnnotationType) obj);
	} else if (obj instanceof JvmEnumerationType) {
		return _imageDescriptor((JvmEnumerationType) obj);
	} else if (obj instanceof JvmField) {
		return _imageDescriptor((JvmField) obj);
	} else if (obj instanceof JvmGenericType) {
		return _imageDescriptor((JvmGenericType) obj);
	} else if (obj instanceof JvmTypeParameter) {
		return _imageDescriptor((JvmTypeParameter) obj);
	} else if (obj instanceof JvmFormalParameter) {
		return _imageDescriptor((JvmFormalParameter) obj);
	} else if (obj instanceof XVariableDeclaration) {
		return _imageDescriptor((XVariableDeclaration) obj);
	} else if (obj instanceof IResolvedConstructor) {
		return _imageDescriptor((IResolvedConstructor) obj);
	} else if (obj instanceof IResolvedOperation) {
		return _imageDescriptor((IResolvedOperation) obj);
	} else if (obj instanceof XImportDeclaration) {
		return _imageDescriptor((XImportDeclaration) obj);
	} else if (obj instanceof XImportSection) {
		return _imageDescriptor((XImportSection) obj);
	} else if (obj instanceof IResolvedField) {
		return _imageDescriptor((IResolvedField) obj);
	}
	return _imageDescriptor(obj);
}
 
Example #6
Source File: XbaseCopyQualifiedNameService.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private IResolvedExecutable findDeclaredConstructor(JvmConstructor constructor, LightweightTypeReference actualType) {
	ResolvedFeatures resolvedFeatures = overrideHelper.getResolvedFeatures(actualType);
	for (IResolvedConstructor declaredConstructor : resolvedFeatures.getDeclaredConstructors()) {
		if (declaredConstructor.getDeclaration().equals(constructor)) {
			return declaredConstructor;
		}
	}
	return null;
}
 
Example #7
Source File: CompilationUnitImpl.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public ResolvedConstructor toResolvedConstructor(final IResolvedConstructor delegate) {
  final Function1<IResolvedConstructor, ResolvedConstructorImpl> _function = (IResolvedConstructor it) -> {
    ResolvedConstructorImpl _resolvedConstructorImpl = new ResolvedConstructorImpl();
    final Procedure1<ResolvedConstructorImpl> _function_1 = (ResolvedConstructorImpl it_1) -> {
      it_1.setDelegate(delegate);
      it_1.setCompilationUnit(this);
    };
    return ObjectExtensions.<ResolvedConstructorImpl>operator_doubleArrow(_resolvedConstructorImpl, _function_1);
  };
  return this.<IResolvedConstructor, ResolvedConstructorImpl>getOrCreate(delegate, _function);
}
 
Example #8
Source File: TypeReferenceImpl.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Iterable<? extends ResolvedConstructor> getDeclaredResolvedConstructors() {
  final Function1<IResolvedConstructor, ResolvedConstructor> _function = (IResolvedConstructor it) -> {
    return this.getCompilationUnit().toResolvedConstructor(it);
  };
  return ListExtensions.<IResolvedConstructor, ResolvedConstructor>map(this.getCompilationUnit().getOverrideHelper().getResolvedFeatures(this.getDelegate()).getDeclaredConstructors(), _function);
}
 
Example #9
Source File: XbaseLabelProvider.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
protected ImageDescriptor _imageDescriptor(IResolvedConstructor constructor) {
	return _imageDescriptor(constructor.getDeclaration());
}
 
Example #10
Source File: XbaseLabelProvider.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
protected Object text(IResolvedConstructor constructor) {
	String styledString = constructor.getResolvedParameterTypes().stream().map(type -> type.getHumanReadableName())
			.collect(Collectors.joining(", ", "new(", ")"));
	return new StyledString(styledString);
}
 
Example #11
Source File: ImplementMemberFromSuperAssist.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
protected ICompletionProposal createOverrideMethodProposal(XtendTypeDeclaration model, IResolvedExecutable overrideable,
		final ContentAssistContext context, IProposalConflictHelper conflictHelper) {
	IXtextDocument document = context.getDocument();
	XtextResource resource = (XtextResource) model.eResource();
	int offset = context.getReplaceRegion().getOffset();
	int currentIndentation = appendableFactory.getIndentationLevelAtOffset(offset, document, resource);
	final int indentationLevel = currentIndentation == 0 ? 1 : currentIndentation;
	ReplacingAppendable appendable = appendableFactory.create(document, resource, offset, context.getReplaceRegion().getLength(), new OptionalParameters() {{ 
				ensureEmptyLinesAround = true;
				baseIndentationLevel = indentationLevel;	
			}});
	final String simpleName;
	JvmExecutable declaration = overrideable.getDeclaration();
	if (overrideable instanceof IResolvedOperation) {
		implementor.appendOverrideFunction(model, (IResolvedOperation) overrideable, appendable);
		simpleName = overrideable.getDeclaration().getSimpleName();
	} else if (model instanceof XtendClass) {
		implementor.appendConstructorFromSuper((XtendClass) model, (IResolvedConstructor) overrideable, appendable);
		simpleName = "new";
	} else {
		return null;
	}
	String code = appendable.getCode();
	if (!isValidProposal(code.trim(), context, conflictHelper) && !isValidProposal(simpleName, context, conflictHelper))
		return null;
	ImageDescriptor imageDescriptor = images.forOperation(declaration.getVisibility(), adornments.getOverrideAdornment(declaration));
	ImportOrganizingProposal completionProposal = createCompletionProposal(appendable, context.getReplaceRegion(),
			getLabel(overrideable), imageHelper.getImage(imageDescriptor));
	Matcher matcher = bodyExpressionPattern.matcher(code);
	if (matcher.find()) {
		int bodyExpressionLength = matcher.end(1) - matcher.start(1);
		int bodyExpressionStart = matcher.start(1) + appendable.getTotalOffset() - completionProposal.getReplacementOffset();
		if (bodyExpressionLength == 0) {
			completionProposal.setCursorPosition(bodyExpressionStart);
		} else {
			completionProposal.setSelectionStart(completionProposal.getReplacementOffset() + bodyExpressionStart);
			completionProposal.setSelectionLength(bodyExpressionLength);
			completionProposal.setAutoInsertable(false);
			completionProposal.setCursorPosition(bodyExpressionStart + bodyExpressionLength);
			completionProposal.setSimpleLinkedMode(context.getViewer(), '\t');
		}
	}
	completionProposal.setPriority(getPriority(model, declaration, context));
	completionProposal.setMatcher(new PrefixMatcher() {

		@Override
		public boolean isCandidateMatchingPrefix(String name, String prefix) {
			PrefixMatcher delegate = context.getMatcher();
			boolean result = delegate.isCandidateMatchingPrefix(simpleName, prefix);
			return result;
		}
		
	});
	return completionProposal;
}
 
Example #12
Source File: XtendLabelProvider.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Override
protected ImageDescriptor imageDescriptor(final Object operation) {
  if (operation instanceof JvmConstructor) {
    return _imageDescriptor((JvmConstructor)operation);
  } else if (operation instanceof JvmOperation) {
    return _imageDescriptor((JvmOperation)operation);
  } else if (operation instanceof JvmAnnotationType) {
    return _imageDescriptor((JvmAnnotationType)operation);
  } else if (operation instanceof JvmEnumerationType) {
    return _imageDescriptor((JvmEnumerationType)operation);
  } else if (operation instanceof JvmField) {
    return _imageDescriptor((JvmField)operation);
  } else if (operation instanceof JvmGenericType) {
    return _imageDescriptor((JvmGenericType)operation);
  } else if (operation instanceof AnonymousClass) {
    return _imageDescriptor((AnonymousClass)operation);
  } else if (operation instanceof XtendAnnotationType) {
    return _imageDescriptor((XtendAnnotationType)operation);
  } else if (operation instanceof XtendClass) {
    return _imageDescriptor((XtendClass)operation);
  } else if (operation instanceof XtendConstructor) {
    return _imageDescriptor((XtendConstructor)operation);
  } else if (operation instanceof XtendEnum) {
    return _imageDescriptor((XtendEnum)operation);
  } else if (operation instanceof XtendFunction) {
    return _imageDescriptor((XtendFunction)operation);
  } else if (operation instanceof XtendInterface) {
    return _imageDescriptor((XtendInterface)operation);
  } else if (operation instanceof JvmTypeParameter) {
    return _imageDescriptor((JvmTypeParameter)operation);
  } else if (operation instanceof XtendEnumLiteral) {
    return _imageDescriptor((XtendEnumLiteral)operation);
  } else if (operation instanceof XtendField) {
    return _imageDescriptor((XtendField)operation);
  } else if (operation instanceof JvmFormalParameter) {
    return _imageDescriptor((JvmFormalParameter)operation);
  } else if (operation instanceof XVariableDeclaration) {
    return _imageDescriptor((XVariableDeclaration)operation);
  } else if (operation instanceof XtendFile) {
    return _imageDescriptor((XtendFile)operation);
  } else if (operation instanceof IResolvedConstructor) {
    return _imageDescriptor((IResolvedConstructor)operation);
  } else if (operation instanceof IResolvedOperation) {
    return _imageDescriptor((IResolvedOperation)operation);
  } else if (operation instanceof XImportDeclaration) {
    return _imageDescriptor((XImportDeclaration)operation);
  } else if (operation instanceof XImportSection) {
    return _imageDescriptor((XImportSection)operation);
  } else if (operation instanceof IResolvedField) {
    return _imageDescriptor((IResolvedField)operation);
  } else if (operation != null) {
    return _imageDescriptor(operation);
  } else {
    throw new IllegalArgumentException("Unhandled parameter types: " +
      Arrays.<Object>asList(operation).toString());
  }
}
 
Example #13
Source File: AbstractXtendOutlineTreeBuilder.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
protected void buildInheritedMembers(final JvmDeclaredType inferredType, final IXtendOutlineContext context) {
  ResourceSet _resourceSet = inferredType.eResource().getResourceSet();
  final StandardTypeReferenceOwner owner = new StandardTypeReferenceOwner(this.services, _resourceSet);
  final LightweightTypeReference typeReference = owner.toLightweightTypeReference(inferredType);
  final List<LightweightTypeReference> superTypes = typeReference.getAllSuperTypes();
  IXtendOutlineContext superTypeContext = context;
  for (final LightweightTypeReference superTypeRef : superTypes) {
    {
      superTypeContext = superTypeContext.increaseInheritanceDepth();
      final ResolvedFeatures resolvedFeatures = new ResolvedFeatures(superTypeRef);
      List<IResolvedField> _declaredFields = resolvedFeatures.getDeclaredFields();
      for (final IResolvedField jvmField : _declaredFields) {
        boolean _skipFeature = this.skipFeature(jvmField.getDeclaration());
        boolean _not = (!_skipFeature);
        if (_not) {
          this.xtendOutlineNodeBuilder.buildResolvedFeatureNode(inferredType, jvmField, superTypeContext);
        }
      }
      List<IResolvedConstructor> _declaredConstructors = resolvedFeatures.getDeclaredConstructors();
      for (final IResolvedConstructor constructor : _declaredConstructors) {
        boolean _skipFeature_1 = this.skipFeature(constructor.getDeclaration());
        boolean _not_1 = (!_skipFeature_1);
        if (_not_1) {
          this.xtendOutlineNodeBuilder.buildResolvedFeatureNode(inferredType, constructor, superTypeContext);
        }
      }
      List<IResolvedOperation> _declaredOperations = resolvedFeatures.getDeclaredOperations();
      for (final IResolvedOperation operation : _declaredOperations) {
        if (((!this.skipFeature(operation.getDeclaration())) && (!superTypeContext.isProcessed(operation.getDeclaration())))) {
          this.xtendOutlineNodeBuilder.buildResolvedFeatureNode(inferredType, operation, superTypeContext);
        }
      }
      final JvmType declaredType = superTypeRef.getType();
      if ((declaredType instanceof JvmDeclaredType)) {
        final IXtendOutlineContext nestedTypeContext = superTypeContext.hideInherited();
        final Consumer<JvmDeclaredType> _function = (JvmDeclaredType it) -> {
          this.buildJvmType(it, nestedTypeContext);
        };
        Iterables.<JvmDeclaredType>filter(((JvmDeclaredType)declaredType).getMembers(), JvmDeclaredType.class).forEach(_function);
      }
    }
  }
}