Java Code Examples for org.eclipse.xtext.xbase.lib.Conversions#unwrapArray()

The following examples show how to use org.eclipse.xtext.xbase.lib.Conversions#unwrapArray() . 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: XbaseInterpreter.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected Object _doEvaluate(XListLiteral literal, IEvaluationContext context, CancelIndicator indicator) {
	IResolvedTypes resolveTypes = typeResolver.resolveTypes(literal);
	LightweightTypeReference type = resolveTypes.getActualType(literal);
	List<Object> list = newArrayList();
	for(XExpression element: literal.getElements()) {
		if (indicator.isCanceled())
			throw new InterpreterCanceledException();
		list.add(internalEvaluate(element, context, indicator));
	}
	if(type != null && type.isArray()) {
		try {
			LightweightTypeReference componentType = type.getComponentType();
			return Conversions.unwrapArray(list, getJavaType(componentType.getType()));
		} catch (ClassNotFoundException e) {
		}
	}
	return Collections.unmodifiableList(list);
}
 
Example 2
Source File: ASTFlattenerUtils.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
private IMethodBinding internalFindOverride(final IMethodBinding method, final ITypeBinding superType, final boolean onlyPrimarylevel) {
  final Function1<IMethodBinding, Boolean> _function = (IMethodBinding it) -> {
    return Boolean.valueOf(method.overrides(it));
  };
  final Iterable<IMethodBinding> superClassOverride = IterableExtensions.<IMethodBinding>filter(((Iterable<IMethodBinding>)Conversions.doWrapArray(superType.getDeclaredMethods())), _function);
  int _size = IterableExtensions.size(superClassOverride);
  boolean _equals = (_size == 1);
  if (_equals) {
    return ((IMethodBinding[])Conversions.unwrapArray(superClassOverride, IMethodBinding.class))[0];
  } else {
    if ((!onlyPrimarylevel)) {
      return this.findOverride(method, superType);
    } else {
      return null;
    }
  }
}
 
Example 3
Source File: CompilationUnitImpl.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected Object translateAnnotationValue(final XExpression value, final JvmTypeReference expectedType) {
  Object _xblockexpression = null;
  {
    if ((value instanceof XAnnotation)) {
      return this.translateAnnotation(((XAnnotation)value));
    }
    if ((value instanceof XListLiteral)) {
      final Iterable<XAnnotation> annotations = Iterables.<XAnnotation>filter(((XListLiteral)value).getElements(), XAnnotation.class);
      int _size = ((XListLiteral)value).getElements().size();
      int _size_1 = IterableExtensions.size(annotations);
      boolean _equals = (_size == _size_1);
      if (_equals) {
        final ArrayList<AnnotationReference> annotationReferences = CollectionLiterals.<AnnotationReference>newArrayList();
        for (final XAnnotation annotation : annotations) {
          AnnotationReference _translateAnnotation = this.translateAnnotation(annotation);
          annotationReferences.add(_translateAnnotation);
        }
        final AnnotationReference[] result = ((AnnotationReference[])Conversions.unwrapArray(annotationReferences, AnnotationReference.class));
        return result;
      }
    }
    _xblockexpression = this.translate(this.evaluate(value, expectedType));
  }
  return _xblockexpression;
}
 
Example 4
Source File: ConstantExpressionsInterpreterTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testConstants_WithStaticImport() {
  try {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("import static test.Constants1.* ");
    _builder.newLine();
    _builder.append("class C { ");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("val someString = STRING_CONSTANT");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("val someInt = INT_CONSTANT");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final XtendFile file = this.file(_builder.toString());
    final XtendField stringField = IterableExtensions.<XtendField>head(Iterables.<XtendField>filter(IterableExtensions.<XtendTypeDeclaration>head(file.getXtendTypes()).getMembers(), XtendField.class));
    final XtendField intField = ((XtendField[])Conversions.unwrapArray((Iterables.<XtendField>filter(IterableExtensions.<XtendTypeDeclaration>head(file.getXtendTypes()).getMembers(), XtendField.class)), XtendField.class))[1];
    Assert.assertEquals(Constants1.STRING_CONSTANT, this.interpreter.evaluate(stringField.getInitialValue(), null));
    Assert.assertEquals(Integer.valueOf(Constants1.INT_CONSTANT), this.interpreter.evaluate(intField.getInitialValue(), null));
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example 5
Source File: JdtBasedProcessorProvider.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Construct a Classloader with the classpathentries from the provided and all upstream-projects,
 * except the output folders of the local project.
 */
protected URLClassLoader createClassLoaderForJavaProject(final IJavaProject projectToUse) {
  final LinkedHashSet<URL> urls = CollectionLiterals.<URL>newLinkedHashSet();
  try {
    this.collectClasspathURLs(projectToUse, urls, this.isOutputFolderIncluded(), CollectionLiterals.<IJavaProject>newHashSet());
  } catch (final Throwable _t) {
    if (_t instanceof JavaModelException) {
      final JavaModelException e = (JavaModelException)_t;
      boolean _isDoesNotExist = e.isDoesNotExist();
      boolean _not = (!_isDoesNotExist);
      if (_not) {
        JdtBasedProcessorProvider.LOG.error(e.getMessage(), e);
      }
    } else {
      throw Exceptions.sneakyThrow(_t);
    }
  }
  ClassLoader _parentClassLoader = this.getParentClassLoader();
  return new URLClassLoader(((URL[])Conversions.unwrapArray(urls, URL.class)), _parentClassLoader);
}
 
Example 6
Source File: JavaFileAccess.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected JavaFileAccess(final TypeReference typeRef, final CodeConfig codeConfig) {
  int _length = ((Object[])Conversions.unwrapArray(typeRef.getSimpleNames(), Object.class)).length;
  boolean _greaterThan = (_length > 1);
  if (_greaterThan) {
    throw new IllegalArgumentException(("Nested type cannot be serialized: " + typeRef));
  }
  this.javaType = typeRef;
  this.codeConfig = codeConfig;
  String _path = typeRef.getPath();
  String _plus = (_path + ".");
  String _fileExtension = this.getFileExtension();
  String _plus_1 = (_plus + _fileExtension);
  this.setPath(_plus_1);
}
 
Example 7
Source File: ResourceDescriptionProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testStubGenerationWithTypeParamers_01() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("package my.pack;");
  _builder.newLine();
  _builder.newLine();
  _builder.append("public class MyTest<T extends CharSequence> {");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("interface InnerInterface<A,B> {");
  _builder.newLine();
  _builder.append("\t\t");
  _builder.append("static class DeepNested<X> {}");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("}");
  _builder.newLine();
  _builder.append("}");
  _builder.newLine();
  final Procedure1<IResourceDescription> _function = (IResourceDescription it) -> {
    final IEObjectDescription mytest = IterableExtensions.<IEObjectDescription>head(it.getExportedObjects());
    Assert.assertEquals("my.pack.MyTest", mytest.getQualifiedName().toString());
    Assert.assertEquals("<T>", mytest.getUserData(JvmTypesResourceDescriptionStrategy.TYPE_PARAMETERS));
    final IEObjectDescription innerinterface = ((IEObjectDescription[])Conversions.unwrapArray(it.getExportedObjects(), IEObjectDescription.class))[1];
    Assert.assertEquals("my.pack.MyTest.InnerInterface", innerinterface.getQualifiedName().toString());
    Assert.assertEquals("<A,B>", innerinterface.getUserData(JvmTypesResourceDescriptionStrategy.TYPE_PARAMETERS));
    Assert.assertEquals(Boolean.TRUE.toString(), innerinterface.getUserData(JvmTypesResourceDescriptionStrategy.IS_INTERFACE));
    final IEObjectDescription deepNested = ((IEObjectDescription[])Conversions.unwrapArray(it.getExportedObjects(), IEObjectDescription.class))[2];
    Assert.assertEquals("my.pack.MyTest.InnerInterface.DeepNested", deepNested.getQualifiedName().toString());
    Assert.assertEquals("<X>", deepNested.getUserData(JvmTypesResourceDescriptionStrategy.TYPE_PARAMETERS));
  };
  this.resultsIn(_builder, _function);
}
 
Example 8
Source File: TypeReference.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
private static String getPackageName(final String qualifiedName, final boolean strict) {
  final List<String> segments = IterableExtensions.<String>toList(Splitter.on(".").split(qualifiedName));
  int _size = segments.size();
  boolean _equals = (_size == 1);
  if (_equals) {
    return "";
  }
  if (strict) {
    int _length = ((Object[])Conversions.unwrapArray(segments, Object.class)).length;
    int _minus = (_length - 1);
    final List<String> packageSegments = segments.subList(0, _minus);
    final Function1<String, Boolean> _function = (String it) -> {
      return Boolean.valueOf(Character.isUpperCase(it.charAt(0)));
    };
    boolean _isEmpty = IterableExtensions.isEmpty(IterableExtensions.<String>filter(packageSegments, _function));
    boolean _not = (!_isEmpty);
    if (_not) {
      throw new IllegalArgumentException((("Cannot determine the package name of \'" + qualifiedName) + "\'. Please use the TypeReference(packageName, className) constructor"));
    }
    return IterableExtensions.join(packageSegments, ".");
  } else {
    int _length_1 = ((Object[])Conversions.unwrapArray(segments, Object.class)).length;
    int _minus_1 = (_length_1 - 1);
    List<String> packageSegments_1 = segments.subList(0, _minus_1);
    while ((!packageSegments_1.isEmpty())) {
      boolean _isUpperCase = Character.isUpperCase(IterableExtensions.<String>last(packageSegments_1).charAt(0));
      if (_isUpperCase) {
        final List<String> _converted_packageSegments_1 = (List<String>)packageSegments_1;
        int _length_2 = ((Object[])Conversions.unwrapArray(_converted_packageSegments_1, Object.class)).length;
        int _minus_2 = (_length_2 - 1);
        packageSegments_1 = packageSegments_1.subList(0, _minus_2);
      } else {
        return IterableExtensions.join(packageSegments_1, ".");
      }
    }
    return "";
  }
}
 
Example 9
Source File: AbstractAntlrGeneratorFragment2.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public String[] getAntlrParams() {
  ArrayList<String> params = Lists.<String>newArrayList(this.antlrParams);
  boolean _contains = params.contains("-Xconversiontimeout");
  boolean _not = (!_contains);
  if (_not) {
    params.add(0, "-Xconversiontimeout");
    params.add(1, "100000");
  }
  return ((String[])Conversions.unwrapArray(params, String.class));
}
 
Example 10
Source File: ContentAssistFragment.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public String[] getRequiredBundlesUi(final Grammar grammar) {
  List<String> _xifexpression = null;
  if (this.generateStub) {
    _xifexpression = CollectionLiterals.<String>newArrayList("org.eclipse.xtext.ui", "org.eclipse.xtext.xbase.lib");
  } else {
    _xifexpression = Collections.<String>singletonList("org.eclipse.xtext.ui");
  }
  return ((String[])Conversions.unwrapArray(_xifexpression, String.class));
}
 
Example 11
Source File: XbaseInterpreter.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected Object wrapOrUnwrapArray(Object result, LightweightTypeReference expectedType) {
	if (expectedType.isArray() && !(result instanceof Object[])) {
		Class<?> arrayType;
		try {
			arrayType = getJavaType(expectedType.getComponentType().getType());
			return Conversions.unwrapArray(result, arrayType);
		} catch (ClassNotFoundException e) {
			return result;
		}
	} else if (!expectedType.isArray() && expectedType.isSubtypeOf(Iterable.class)) {
		return Conversions.doWrapArray(result);
	}
	return result;
}
 
Example 12
Source File: AnnotationWithNestedAnnotations.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
public Annotation1Config(final AnnotationReference ann) {
  final Function1<AnnotationReference, AnnotationWithNestedAnnotations.Annotation2Config> _function = (AnnotationReference it) -> {
    return new AnnotationWithNestedAnnotations.Annotation2Config(it);
  };
  this.ann2Configs = ((AnnotationWithNestedAnnotations.Annotation2Config[])Conversions.unwrapArray(ListExtensions.<AnnotationReference, AnnotationWithNestedAnnotations.Annotation2Config>map(((List<AnnotationReference>)Conversions.doWrapArray(ann.getAnnotationArrayValue("ann2"))), _function), AnnotationWithNestedAnnotations.Annotation2Config.class));
}
 
Example 13
Source File: AnnotationWithNestedAnnotations.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
public Annotation2Config(final AnnotationReference ann) {
  final Function1<AnnotationReference, AnnotationWithNestedAnnotations.Annotation3Config> _function = (AnnotationReference it) -> {
    return new AnnotationWithNestedAnnotations.Annotation3Config(it);
  };
  this.ann3Configs = ((AnnotationWithNestedAnnotations.Annotation3Config[])Conversions.unwrapArray(ListExtensions.<AnnotationReference, AnnotationWithNestedAnnotations.Annotation3Config>map(((List<AnnotationReference>)Conversions.doWrapArray(ann.getAnnotationArrayValue("ann3"))), _function), AnnotationWithNestedAnnotations.Annotation3Config.class));
}
 
Example 14
Source File: DeclarationsTest.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testXtendInterfaceWithDefaultMethod() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("package p");
  _builder.newLine();
  _builder.newLine();
  _builder.append("interface MyIntf {");
  _builder.newLine();
  _builder.append("\t");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("def String dflt() { \"\" }");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("def String abstrct()");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("def static String statc() { \"\" }");
  _builder.newLine();
  _builder.append("\t");
  _builder.newLine();
  _builder.append("}");
  _builder.newLine();
  final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
    Assert.assertEquals("p", it.getPackageName());
    TypeDeclaration _head = IterableExtensions.head(it.getSourceTypeDeclarations());
    final InterfaceDeclaration intf = ((InterfaceDeclaration) _head);
    Assert.assertEquals("p.MyIntf", intf.getQualifiedName());
    Assert.assertTrue(IterableExtensions.isEmpty(intf.getExtendedInterfaces()));
    Assert.assertEquals(3, IterableExtensions.size(intf.getDeclaredMembers()));
    final MethodDeclaration dflt = IterableExtensions.head(intf.getDeclaredMethods());
    Assert.assertTrue(dflt.isDefault());
    Assert.assertFalse(dflt.isAbstract());
    final MethodDeclaration abstract_ = ((MethodDeclaration[])Conversions.unwrapArray(intf.getDeclaredMethods(), MethodDeclaration.class))[1];
    Assert.assertFalse(abstract_.isDefault());
    Assert.assertTrue(abstract_.isAbstract());
    final MethodDeclaration static_ = ((MethodDeclaration[])Conversions.unwrapArray(intf.getDeclaredMethods(), MethodDeclaration.class))[2];
    Assert.assertFalse(static_.isDefault());
    Assert.assertFalse(static_.isAbstract());
    Assert.assertTrue(static_.isStatic());
  };
  this.asCompilationUnit(this.validFile(_builder), _function);
}
 
Example 15
Source File: DeclarationsTest.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testIsAssignable() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("package foo");
  _builder.newLine();
  _builder.newLine();
  _builder.append("class BaseClass implements InterfaceA {");
  _builder.newLine();
  _builder.append("}");
  _builder.newLine();
  _builder.append("class SubType extends BaseClass implements InterfaceA {");
  _builder.newLine();
  _builder.append("}");
  _builder.newLine();
  _builder.append("interface InterfaceA {}");
  _builder.newLine();
  _builder.append("interface InterfaceB {}");
  _builder.newLine();
  final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
    TypeDeclaration _get = ((TypeDeclaration[])Conversions.unwrapArray(it.getSourceTypeDeclarations(), TypeDeclaration.class))[0];
    final ClassDeclaration baseClass = ((ClassDeclaration) _get);
    final MutableClassDeclaration subClass = it.getTypeLookup().findClass("foo.SubType");
    TypeDeclaration _get_1 = ((TypeDeclaration[])Conversions.unwrapArray(it.getSourceTypeDeclarations(), TypeDeclaration.class))[2];
    final InterfaceDeclaration interfaceA = ((InterfaceDeclaration) _get_1);
    TypeDeclaration _get_2 = ((TypeDeclaration[])Conversions.unwrapArray(it.getSourceTypeDeclarations(), TypeDeclaration.class))[3];
    final InterfaceDeclaration interfaceB = ((InterfaceDeclaration) _get_2);
    final Type object = it.getTypeReferenceProvider().getObject().getType();
    Assert.assertTrue(object.isAssignableFrom(baseClass));
    Assert.assertTrue(object.isAssignableFrom(subClass));
    Assert.assertTrue(object.isAssignableFrom(interfaceA));
    Assert.assertTrue(object.isAssignableFrom(interfaceB));
    Assert.assertTrue(baseClass.isAssignableFrom(baseClass));
    Assert.assertTrue(baseClass.isAssignableFrom(subClass));
    Assert.assertFalse(baseClass.isAssignableFrom(interfaceB));
    Assert.assertFalse(baseClass.isAssignableFrom(interfaceA));
    Assert.assertFalse(baseClass.isAssignableFrom(object));
    Assert.assertTrue(interfaceA.isAssignableFrom(baseClass));
    Assert.assertTrue(interfaceA.isAssignableFrom(subClass));
    Assert.assertTrue(interfaceA.isAssignableFrom(interfaceA));
    Assert.assertFalse(interfaceA.isAssignableFrom(interfaceB));
    Assert.assertFalse(interfaceA.isAssignableFrom(object));
  };
  this.asCompilationUnit(this.validFile(_builder), _function);
}
 
Example 16
Source File: ConstantExpressionsInterpreter.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
protected Object _internalEvaluate(final XListLiteral it, final Context ctx) {
  try {
    JvmTypeReference _switchResult = null;
    JvmTypeReference _expectedType = ctx.getExpectedType();
    final JvmTypeReference exp = _expectedType;
    boolean _matched = false;
    if (exp instanceof JvmGenericArrayTypeReference) {
      _matched=true;
      _switchResult = ((JvmGenericArrayTypeReference)exp).getComponentType();
    }
    final JvmTypeReference expectedComponentType = _switchResult;
    final Function1<XExpression, Object> _function = (XExpression it_1) -> {
      return this.evaluate(it_1, ctx.cloneWithExpectation(expectedComponentType));
    };
    final List<Object> elements = ListExtensions.<XExpression, Object>map(it.getElements(), _function);
    Class<?> _xifexpression = null;
    if ((expectedComponentType != null)) {
      _xifexpression = this.getJavaType(expectedComponentType.getType(), ctx.getClassFinder());
    } else {
      Class<?> _xifexpression_1 = null;
      boolean _isEmpty = elements.isEmpty();
      boolean _not = (!_isEmpty);
      if (_not) {
        Class<?> _switchResult_1 = null;
        Class<?> _class = IterableExtensions.<Object>head(elements).getClass();
        final Class<?> cl = _class;
        boolean _matched_1 = false;
        if (Objects.equal(cl, Integer.class)) {
          _matched_1=true;
          _switchResult_1 = Integer.TYPE;
        }
        if (!_matched_1) {
          if (Objects.equal(cl, Long.class)) {
            _matched_1=true;
            _switchResult_1 = Long.TYPE;
          }
        }
        if (!_matched_1) {
          if (Objects.equal(cl, Short.class)) {
            _matched_1=true;
            _switchResult_1 = Short.TYPE;
          }
        }
        if (!_matched_1) {
          if (Objects.equal(cl, Boolean.class)) {
            _matched_1=true;
            _switchResult_1 = Boolean.TYPE;
          }
        }
        if (!_matched_1) {
          if (Objects.equal(cl, Double.class)) {
            _matched_1=true;
            _switchResult_1 = Double.TYPE;
          }
        }
        if (!_matched_1) {
          if (Objects.equal(cl, Byte.class)) {
            _matched_1=true;
            _switchResult_1 = Byte.TYPE;
          }
        }
        if (!_matched_1) {
          if (Objects.equal(cl, Float.class)) {
            _matched_1=true;
            _switchResult_1 = Float.TYPE;
          }
        }
        if (!_matched_1) {
          _switchResult_1 = cl;
        }
        _xifexpression_1 = _switchResult_1;
      } else {
        _xifexpression_1 = Object.class;
      }
      _xifexpression = _xifexpression_1;
    }
    final Class<?> componentType = _xifexpression;
    return Conversions.unwrapArray(elements, componentType);
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example 17
Source File: BracePairMatcher.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
@Inject
public void setBracePairProvider(IBracePairProvider bracePairProvider) {
	List<Character> chars = getChars(bracePairProvider);
	characterPairMatcher = new DefaultCharacterPairMatcher(((char[])Conversions.unwrapArray(chars, char.class)));
}
 
Example 18
Source File: ContentAssistFragment.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public String[] getExportedPackagesUi(final Grammar grammar) {
  return ((String[])Conversions.unwrapArray(Collections.<String>singletonList(this._naming.packageName(this.getProposalProviderName(grammar))), String.class));
}
 
Example 19
Source File: ContentAssistFragment.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public String[] getImportedPackagesUi(final Grammar grammar) {
  return ((String[])Conversions.unwrapArray(Collections.<String>singleton("org.apache.log4j"), String.class));
}