use of org.eclipse.xtext.common.types.JvmOperation in project xtext-xtend by eclipse.
the class DispatchMethodRenameStrategy method initialize.
@Override
public boolean initialize(EObject xtendMethod, IRenameElementContext context) {
Assert.isLegal(xtendMethod instanceof XtendFunction);
Assert.isLegal(((XtendFunction) xtendMethod).isDispatch());
Assert.isLegal(context instanceof DispatchMethodRenameContext);
ResourceSet resourceSet = xtendMethod.eResource().getResourceSet();
Map<URI, IJavaElement> jvm2JavaElements = ((DispatchMethodRenameContext) context).getJvm2JavaElements();
for (URI dispatchOperationURI : jvm2JavaElements.keySet()) {
JvmOperation dispatchOperation = (JvmOperation) resourceSet.getEObject(dispatchOperationURI, true);
XtendFunction xtendDispatchMethod = associations.getXtendFunction(dispatchOperation);
if (xtendDispatchMethod != null) {
if (equal(xtendDispatchMethod.getName(), dispatchOperation.getSimpleName())) {
// synthetic dispatcher
dispatchers.add(dispatchOperation);
} else {
// xtend dispatch method
XtendDispatchMethodChildStrategy xtendChildStrategy = childStrategyProvider.get();
xtendChildStrategy.initialize(xtendDispatchMethod, context);
children.add(xtendChildStrategy);
}
} else {
// a dispatch method form a Java class
JavaDispatchMethodChildStrategy jvmChildStrategy = javaStrategyProvider.get();
jvmChildStrategy.initialize(dispatchOperation, context);
children.add(jvmChildStrategy);
}
}
return !children.isEmpty();
}
use of org.eclipse.xtext.common.types.JvmOperation in project xtext-xtend by eclipse.
the class ExtractMethodRefactoring method appendMethodSignature.
protected void appendMethodSignature(ISourceAppender appendable) {
if (visibility != JvmVisibility.PUBLIC)
appendable.append(getVisibility().getName().toLowerCase()).append(" ");
appendable.append("def ");
if (isStatic)
appendable.append("static ");
if (!neededTypeParameters.isEmpty()) {
JvmOperation operation = associations.getDirectlyInferredOperation(originalMethod);
if (operation != null) {
appendable.append("<");
boolean isFirst = true;
for (JvmTypeParameter typeParameter : operation.getTypeParameters()) {
if (neededTypeParameters.contains(typeParameter)) {
if (!isFirst)
appendable.append(", ");
isFirst = false;
appendable.append(typeParameter);
}
}
appendable.append("> ");
}
}
if (isExplicitlyDeclareReturnType) {
appendable.append(returnType).append(" ");
}
appendable.append(methodName).append("(");
boolean isFirst = true;
for (ParameterInfo parameterInfo : getParameterInfos()) {
if (!isFirst)
appendable.append(", ");
isFirst = false;
appendable.append(parameter2type.get(parameterInfo)).append(" ").append(parameterInfo.getNewName());
}
appendable.append(")");
}
use of org.eclipse.xtext.common.types.JvmOperation in project xtext-xtend by eclipse.
the class AbstractReusableActiveAnnotationTests method testTracing.
@Test
public void testTracing() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("package myannotation");
_builder.newLine();
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.AbstractClassProcessor");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.Active");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.TransformationContext");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration");
_builder.newLine();
_builder.newLine();
_builder.append("@Active(GettersProcessor)");
_builder.newLine();
_builder.append("annotation Getters {");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("class GettersProcessor extends AbstractClassProcessor {");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("override doTransform(MutableClassDeclaration cls, extension TransformationContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("cls.declaredFields.filter[isThePrimaryGeneratedJavaElement].forEach [field|");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("cls.addMethod(\"get\" + field.simpleName.toFirstUpper) [");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("primarySourceElement = field");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("returnType = field.type");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("body = [\"return this.\" + field.simpleName + \";\"]");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("field.markAsRead");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("]");
_builder.newLine();
_builder.append("\t\t");
_builder.append("]");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/Getters.xtend", _builder.toString());
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("package myusercode");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("import myannotation.Getters");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@Getters");
_builder_1.newLine();
_builder_1.append("class Client {");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("val int bar = 1");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("def create new Integer(1) foo() {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("}");
_builder_1.newLine();
Pair<String, String> _mappedTo_1 = Pair.<String, String>of("myusercode/UserCode.xtend", _builder_1.toString());
final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
final MutableClassDeclaration cls = it.getTypeLookup().findClass("myusercode.Client");
final MutableFieldDeclaration barField = cls.findDeclaredField("bar");
final Function1<MutableMethodDeclaration, Boolean> _function_1 = (MutableMethodDeclaration it_1) -> {
return Boolean.valueOf(it_1.getSimpleName().startsWith("get"));
};
final Iterable<? extends MutableMethodDeclaration> getters = IterableExtensions.filter(cls.getDeclaredMethods(), _function_1);
Assert.assertEquals(1, IterableExtensions.size(getters));
final MutableMethodDeclaration getBar = IterableExtensions.head(getters);
Assert.assertEquals("getBar", getBar.getSimpleName());
final JvmField barJvmField = ((MutableJvmFieldDeclarationImpl) barField).getDelegate();
final JvmOperation getBarJvmMethod = ((MutableJvmMethodDeclarationImpl) getBar).getDelegate();
final Set<EObject> elementsAssociatedWithBarField = it.getJvmModelAssociations().getJvmElements(it.getJvmModelAssociations().getPrimarySourceElement(barJvmField));
Assert.assertEquals(2, elementsAssociatedWithBarField.size());
Assert.assertEquals(barJvmField, ((Object[]) Conversions.unwrapArray(elementsAssociatedWithBarField, Object.class))[0]);
Assert.assertEquals(getBarJvmMethod, ((Object[]) Conversions.unwrapArray(elementsAssociatedWithBarField, Object.class))[1]);
};
this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
use of org.eclipse.xtext.common.types.JvmOperation in project xtext-xtend by eclipse.
the class ResourceStorageTest method testUpstreamResourcesAreLoadedFromStorage.
@Test
public void testUpstreamResourcesAreLoadedFromStorage() {
try {
StringConcatenation _builder = new StringConcatenation();
_builder.append("package mypack");
_builder.newLine();
_builder.newLine();
_builder.append("class MyClass {");
_builder.newLine();
_builder.append("}");
_builder.newLine();
this.helper.createFile("mypack/MyClass.xtend", _builder.toString());
final IProject downStreamProject = WorkbenchTestHelper.createPluginProject("downstream", this.helper.getProject().getName());
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("package downstream");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("class SomeClass {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("def void foo(mypack.MyClass myClass) {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("}");
_builder_1.newLine();
_builder_1.append("}");
_builder_1.newLine();
final IFile downstreamFile = this.helper.createFileImpl("/downstream/src/downstream/SomeClass.xtend", _builder_1.toString());
IResourcesSetupUtil.waitForBuild();
final URI downstreamUri = this.uriMapper.getUri(downstreamFile);
final ResourceSet resourceSet = this.resourceSetProvider.get(downStreamProject);
SourceLevelURIsAdapter.setSourceLevelUris(resourceSet, Collections.<URI>unmodifiableList(CollectionLiterals.<URI>newArrayList(downstreamUri)));
Resource _resource = resourceSet.getResource(downstreamUri, true);
final StorageAwareResource downstreamResource = ((StorageAwareResource) _resource);
EObject _get = downstreamResource.getContents().get(1);
final JvmGenericType type = ((JvmGenericType) _get);
final JvmType parameterType = IterableExtensions.<JvmFormalParameter>head(IterableExtensions.<JvmOperation>head(Iterables.<JvmOperation>filter(type.getMembers(), JvmOperation.class)).getParameters()).getParameterType().getType();
Assert.assertNotNull(parameterType);
Resource _eResource = parameterType.eResource();
Assert.assertTrue(((StorageAwareResource) _eResource).isLoadedFromStorage());
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
use of org.eclipse.xtext.common.types.JvmOperation in project xtext-xtend by eclipse.
the class XtendJvmModelInferrer method appendSyntheticDispatchMethods.
protected void appendSyntheticDispatchMethods(XtendTypeDeclaration source, final JvmGenericType target) {
ListMultimap<DispatchHelper.DispatchSignature, JvmOperation> methods = dispatchHelper.getDeclaredOrEnhancedDispatchMethods(target);
for (DispatchHelper.DispatchSignature signature : methods.keySet()) {
List<JvmOperation> operations = methods.get(signature);
Iterable<JvmOperation> localOperations = Iterables.filter(operations, new Predicate<JvmOperation>() {
@Override
public boolean apply(JvmOperation input) {
return target == input.eContainer();
}
});
JvmOperation operation = deriveGenericDispatchOperationSignature(localOperations, target);
if (operation != null) {
dispatchHelper.markAsDispatcherFunction(operation);
operation.setSimpleName(signature.getSimpleName());
operation.setReturnType(jvmTypesBuilder.inferredType());
}
}
}
Aggregations