use of org.eclipse.xtext.common.types.JvmExecutable in project xtext-xtend by eclipse.
the class InferredJvmModelTest method testDispatchFunction_00.
@Test
public void testDispatchFunction_00() throws Exception {
XtendFile xtendFile = file("class Foo { def dispatch foo(Object x) {null} def dispatch foo(String x) {null}}");
JvmGenericType inferredType = getInferredType(xtendFile);
// one main dispatch
Iterable<JvmOperation> operations = inferredType.getDeclaredOperations();
JvmOperation dispatch = findByNameAndFirstParameterType(operations, "foo", Object.class);
assertEquals("java.lang.Object", dispatch.getReturnType().getIdentifier());
// two internal case methods
findByNameAndFirstParameterType(operations, "_foo", Object.class);
findByNameAndFirstParameterType(operations, "_foo", String.class);
for (JvmMember member : inferredType.getMembers()) {
if (member instanceof JvmExecutable) {
if (member.getSimpleName().startsWith("_"))
assertEquals(JvmVisibility.PROTECTED, member.getVisibility());
else
assertEquals(JvmVisibility.PUBLIC, member.getVisibility());
} else {
assertEquals(JvmVisibility.PRIVATE, member.getVisibility());
}
}
}
use of org.eclipse.xtext.common.types.JvmExecutable in project xtext-xtend by eclipse.
the class XtendValidator method doCheckDuplicateExecutables.
protected <Executable extends IResolvedExecutable> void doCheckDuplicateExecutables(JvmGenericType inferredType, List<Executable> declaredOperations, Function<String, List<Executable>> bySignature, Set<EObject> flaggedOperations) {
Set<Executable> processed = Sets.newHashSet();
for (Executable declaredExecutable : declaredOperations) {
if (!processed.contains(declaredExecutable)) {
List<Executable> sameErasure = bySignature.apply(declaredExecutable.getResolvedErasureSignature());
if (sameErasure.size() > 1) {
Multimap<String, Executable> perSignature = HashMultimap.create(sameErasure.size(), 2);
outer: for (Executable executable : sameErasure) {
for (LightweightTypeReference parameterType : executable.getResolvedParameterTypes()) {
if (parameterType.isUnknown())
continue outer;
}
perSignature.put(executable.getResolvedSignature(), executable);
}
if (perSignature.size() > 1) {
for (Collection<Executable> sameSignature : perSignature.asMap().values()) {
for (Executable operationWithSameSignature : sameSignature) {
JvmExecutable executable = operationWithSameSignature.getDeclaration();
EObject otherSource = associations.getPrimarySourceElement(executable);
if (flaggedOperations.add(otherSource)) {
if (sameSignature.size() > 1) {
error("Duplicate " + typeLabel(executable) + " " + operationWithSameSignature.getSimpleSignature() + " in type " + inferredType.getSimpleName(), otherSource, nameFeature(otherSource), DUPLICATE_METHOD);
} else {
error("The " + typeLabel(executable) + " " + operationWithSameSignature.getSimpleSignature() + " has the same erasure " + operationWithSameSignature.getResolvedErasureSignature() + " as another " + typeLabel(executable) + " in type " + inferredType.getSimpleName(), otherSource, nameFeature(otherSource), DUPLICATE_METHOD);
}
}
}
}
}
}
}
}
}
use of org.eclipse.xtext.common.types.JvmExecutable in project xtext-xtend by eclipse.
the class XtendGenerator method compileLocalTypeStubs.
public void compileLocalTypeStubs(final JvmFeature feature, final ITreeAppendable appendable, final GeneratorConfig config) {
final Function1<JvmGenericType, Boolean> _function = (JvmGenericType it) -> {
boolean _isAnonymous = it.isAnonymous();
return Boolean.valueOf((!_isAnonymous));
};
final Consumer<JvmGenericType> _function_1 = (JvmGenericType it) -> {
appendable.newLine();
EObject _head = IterableExtensions.<EObject>head(this.getSourceElements(it));
final AnonymousClass anonymousClass = ((AnonymousClass) _head);
final ITreeAppendable childAppendable = appendable.trace(anonymousClass);
childAppendable.append("abstract class ");
this._treeAppendableUtil.traceSignificant(childAppendable, anonymousClass).append(it.getSimpleName());
boolean _isEmpty = it.getTypeParameters().isEmpty();
if (_isEmpty) {
childAppendable.append(" ");
}
this.generateExtendsClause(it, childAppendable, null);
childAppendable.append("{").increaseIndentation();
boolean _needSyntheticThisVariable = this.needSyntheticThisVariable(anonymousClass, it);
if (_needSyntheticThisVariable) {
Pair<String, JvmGenericType> _mappedTo = Pair.<String, JvmGenericType>of("this", it);
String _simpleName = it.getSimpleName();
String _plus = ("_this" + _simpleName);
final String thisName = childAppendable.declareSyntheticVariable(_mappedTo, _plus);
childAppendable.newLine().append("final ").append(it.getSimpleName()).append(" ").append(thisName).append(" = this;").newLine();
}
final Procedure1<LoopParams> _function_2 = (LoopParams it_1) -> {
final Function1<ITreeAppendable, ITreeAppendable> _function_3 = (ITreeAppendable it_2) -> {
return it_2.newLine();
};
it_1.setSeparator(_function_3);
};
final Procedure1<JvmMember> _function_3 = (JvmMember it_1) -> {
final ITreeAppendable memberAppendable = this._treeAppendableUtil.traceWithComments(childAppendable, it_1);
memberAppendable.openScope();
if ((it_1 instanceof JvmOperation)) {
final ITreeAppendable tracedAppendable = childAppendable.trace(it_1);
tracedAppendable.newLine();
this.generateJavaDoc(it_1, tracedAppendable, config);
this.generateVisibilityModifier(it_1, tracedAppendable);
tracedAppendable.append("abstract ");
this.generateTypeParameterDeclaration(((JvmTypeParameterDeclarator) it_1), tracedAppendable, null);
JvmTypeReference _returnType = ((JvmOperation) it_1).getReturnType();
boolean _tripleEquals = (_returnType == null);
if (_tripleEquals) {
tracedAppendable.append("void");
} else {
this._errorSafeExtensions.serializeSafely(((JvmOperation) it_1).getReturnType(), "Object", tracedAppendable);
}
tracedAppendable.append(" ");
this._treeAppendableUtil.traceSignificant(tracedAppendable, it_1).append(((JvmOperation) it_1).getSimpleName());
tracedAppendable.append("(");
this.generateParameters(((JvmExecutable) it_1), tracedAppendable, null);
tracedAppendable.append(")");
this.generateThrowsClause(((JvmExecutable) it_1), tracedAppendable, null);
tracedAppendable.append(";");
} else {
if ((it_1 instanceof JvmField)) {
final ITreeAppendable tracedAppendable_1 = childAppendable.trace(it_1);
tracedAppendable_1.newLine();
this.generateJavaDoc(it_1, tracedAppendable_1, config);
this.generateAnnotations(((JvmField) it_1).getAnnotations(), tracedAppendable_1, true, config);
if ((((JvmField) it_1).isFinal() && ((JvmField) it_1).isStatic())) {
tracedAppendable_1.append("final ");
}
boolean _isStatic = ((JvmField) it_1).isStatic();
if (_isStatic) {
tracedAppendable_1.append("static ");
}
boolean _isTransient = ((JvmField) it_1).isTransient();
if (_isTransient) {
tracedAppendable_1.append("transient ");
}
boolean _isVolatile = ((JvmField) it_1).isVolatile();
if (_isVolatile) {
tracedAppendable_1.append("volatile ");
}
this._errorSafeExtensions.serializeSafely(((JvmField) it_1).getType(), "Object", tracedAppendable_1);
tracedAppendable_1.append(" ");
this._treeAppendableUtil.traceSignificant(tracedAppendable_1, it_1).append(((JvmField) it_1).getSimpleName());
if ((((JvmField) it_1).isFinal() && ((JvmField) it_1).isStatic())) {
Object _constantValue = ((JvmField) it_1).getConstantValue();
boolean _tripleNotEquals = (_constantValue != null);
if (_tripleNotEquals) {
tracedAppendable_1.append(" = ");
this.generateJavaConstant(((JvmField) it_1).getConstantValue(), tracedAppendable_1);
} else {
this.generateInitialization(((JvmField) it_1), tracedAppendable_1, config);
}
}
tracedAppendable_1.append(";");
} else {
this.generateMember(it_1, memberAppendable, config);
}
}
memberAppendable.closeScope();
};
this._loopExtensions.<JvmMember>forEach(childAppendable, this.getAddedDeclarations(it, anonymousClass), _function_2, _function_3);
childAppendable.decreaseIndentation().newLine().append("}");
appendable.newLine();
};
IterableExtensions.<JvmGenericType>filter(feature.getLocalClasses(), _function).forEach(_function_1);
}
use of org.eclipse.xtext.common.types.JvmExecutable in project xtext-xtend by eclipse.
the class MemberFromSuperImplementor method initializeExecutableBuilder.
protected void initializeExecutableBuilder(final AbstractExecutableBuilder builder, final JvmDeclaredType overrider, final IResolvedExecutable overridden) {
final JvmExecutable executable = overridden.getDeclaration();
builder.setContext(overrider);
builder.setVisibility(overridden.getDeclaration().getVisibility());
final Procedure2<LightweightTypeReference, Integer> _function = (LightweightTypeReference it, Integer index) -> {
final JvmFormalParameter declaredParameter = executable.getParameters().get((index).intValue());
final AbstractParameterBuilder parameterBuilder = builder.newParameterBuilder();
parameterBuilder.setName(declaredParameter.getSimpleName());
parameterBuilder.setType(it);
JvmAnnotationReference _findAnnotation = this.annotationLookup.findAnnotation(declaredParameter, Extension.class);
boolean _tripleNotEquals = (_findAnnotation != null);
parameterBuilder.setExtensionFlag(_tripleNotEquals);
};
IterableExtensions.<LightweightTypeReference>forEach(overridden.getResolvedParameterTypes(), _function);
builder.setVarArgsFlag(executable.isVarArgs());
builder.setExceptions(overridden.getResolvedExceptions());
}
use of org.eclipse.xtext.common.types.JvmExecutable in project xtext-xtend by eclipse.
the class ImplementMemberFromSuperAssist method createOverrideMethodProposal.
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;
}
Aggregations