private long toClassFmt(final JavaVersion version) { if (version != null) { switch (version) { case JAVA5: return ClassFileConstants.JDK1_5; case JAVA6: return ClassFileConstants.JDK1_6; case JAVA7: return ClassFileConstants.JDK1_7; case JAVA8: return (((ClassFileConstants.MAJOR_VERSION_1_7 + 1) << 16) + ClassFileConstants.MINOR_VERSION_0); case JAVA9: return (((ClassFileConstants.MAJOR_VERSION_1_7 + 2) << 16) + ClassFileConstants.MINOR_VERSION_0); default: break; } } return 0; }
protected List<IResolvedOperation> computeAllOperations() { JvmType rawType = getRawType(); if (!(rawType instanceof JvmDeclaredType)) { return Collections.emptyList(); } Multimap<String, AbstractResolvedOperation> processedOperations = LinkedHashMultimap.create(); for (IResolvedOperation resolvedOperation : getDeclaredOperations()) { processedOperations.put(resolvedOperation.getDeclaration().getSimpleName(), (AbstractResolvedOperation) resolvedOperation); } if (targetVersion.isAtLeast(JavaVersion.JAVA8)) { computeAllOperationsFromSortedSuperTypes((JvmDeclaredType) rawType, processedOperations); } else { Set<JvmType> processedTypes = Sets.newHashSet(rawType); computeAllOperationsFromSuperTypes((JvmDeclaredType) rawType, processedOperations, processedTypes); } // make sure the declared operations are the first in the list List<IResolvedOperation> result = new ArrayList<IResolvedOperation>(processedOperations.size()); result.addAll(getDeclaredOperations()); for (AbstractResolvedOperation operation : processedOperations.values()) { if (operation.getDeclaration().getDeclaringType() != rawType) { result.add(operation); } } return Collections.unmodifiableList(result); }
protected void computeAllOperations(JvmDeclaredType type, Multimap<String, AbstractResolvedOperation> processedOperations) { for (JvmOperation operation: type.getDeclaredOperations()) { boolean addToResult = true; if (targetVersion.isAtLeast(JavaVersion.JAVA8)) { addToResult = handleOverridesAndConflicts(operation, processedOperations); } else { String simpleName = operation.getSimpleName(); if (processedOperations.containsKey(simpleName)) { addToResult = !isOverridden(operation, processedOperations.get(simpleName)); } } if (addToResult) { BottomResolvedOperation resolvedOperation = createResolvedOperation(operation); processedOperations.put(operation.getSimpleName(), resolvedOperation); } } }
@Test public void testUnderscoreInNumericLiteral() throws Exception { StringConcatenation _builder = new StringConcatenation(); _builder.append("{"); _builder.newLine(); _builder.append("\t"); _builder.append("val x = 123_456_789"); _builder.newLine(); _builder.append("}"); _builder.newLine(); StringConcatenation _builder_1 = new StringConcatenation(); _builder_1.append("final int x = 123_456_789;"); _builder_1.newLine(); Pair<JavaVersion, JavaVersion> _mappedTo = Pair.<JavaVersion, JavaVersion>of(JavaVersion.JAVA7, JavaVersion.JAVA8); this.compilesTo(_builder, _builder_1, _mappedTo); }
@Test public void testNewThread() throws Exception { StringConcatenation _builder = new StringConcatenation(); _builder.append("new Thread [| ]"); _builder.newLine(); StringConcatenation _builder_1 = new StringConcatenation(); _builder_1.append("final Runnable _function = () -> {"); _builder_1.newLine(); _builder_1.append("};"); _builder_1.newLine(); _builder_1.append("Thread _thread = new Thread(_function);"); _builder_1.newLine(); _builder_1.append("return _thread;"); _builder_1.newLine(); this.compilesTo(_builder, _builder_1, JavaVersion.JAVA8); }
@Test public void testBug410797_01() throws Exception { StringConcatenation _builder = new StringConcatenation(); _builder.append("{ val boolean bug = #[true, false, true].reduce[a,b|a && b] }"); _builder.newLine(); StringConcatenation _builder_1 = new StringConcatenation(); _builder_1.append("final org.eclipse.xtext.xbase.lib.Functions.Function2<Boolean, Boolean, Boolean> _function = (Boolean a, Boolean b) -> {"); _builder_1.newLine(); _builder_1.append(" "); _builder_1.append("return Boolean.valueOf(((a).booleanValue() && (b).booleanValue()));"); _builder_1.newLine(); _builder_1.append("};"); _builder_1.newLine(); _builder_1.append("final boolean bug = (boolean) org.eclipse.xtext.xbase.lib.IterableExtensions.<Boolean>reduce(java.util.Collections.<Boolean>unmodifiableList(org.eclipse.xtext.xbase.lib.CollectionLiterals.<Boolean>newArrayList(Boolean.valueOf(true), Boolean.valueOf(false), Boolean.valueOf(true))), _function);"); _builder_1.newLine(); this.compilesTo(_builder, _builder_1, JavaVersion.JAVA8); }
@Test public void testImplicitReferenceToMultitype() throws Exception { StringConcatenation _builder = new StringConcatenation(); _builder.append("((null as Iterable<java.util.List<Object>>) + (null as Iterable<java.util.Set<Object>>)).forEach[ size ]"); _builder.newLine(); StringConcatenation _builder_1 = new StringConcatenation(); _builder_1.append("final java.util.function.Consumer<java.util.Collection<Object>> _function = (java.util.Collection<Object> it) -> {"); _builder_1.newLine(); _builder_1.append(" "); _builder_1.append("it.size();"); _builder_1.newLine(); _builder_1.append("};"); _builder_1.newLine(); _builder_1.append("com.google.common.collect.Iterables.<java.util.Collection<Object>>concat(((Iterable<java.util.List<Object>>) null), ((Iterable<java.util.Set<Object>>) null)).forEach(_function);"); _builder_1.newLine(); this.compilesTo(_builder, _builder_1, JavaVersion.JAVA8); }
@Test public void testImplicitReferenceToSynonym_01() throws Exception { StringConcatenation _builder = new StringConcatenation(); _builder.append("(null as Iterable<String[]>).forEach[ reverse ]"); _builder.newLine(); StringConcatenation _builder_1 = new StringConcatenation(); _builder_1.append("final java.util.function.Consumer<String[]> _function = (String[] it) -> {"); _builder_1.newLine(); _builder_1.append(" "); _builder_1.append("org.eclipse.xtext.xbase.lib.ListExtensions.<String>reverse(((java.util.List<String>)org.eclipse.xtext.xbase.lib.Conversions.doWrapArray(it)));"); _builder_1.newLine(); _builder_1.append("};"); _builder_1.newLine(); _builder_1.append("((Iterable<String[]>) null).forEach(_function);"); _builder_1.newLine(); this.compilesTo(_builder, _builder_1, JavaVersion.JAVA8); }
@Test public void testBlockHasNoSuperfluousBraces_03() throws Exception { StringConcatenation _builder = new StringConcatenation(); _builder.append("{ var (int)=>void fun = [ int i | new Object() new Object() ] }"); _builder.newLine(); StringConcatenation _builder_1 = new StringConcatenation(); _builder_1.append("final org.eclipse.xtext.xbase.lib.Procedures.Procedure1<Integer> _function = (Integer i) -> {"); _builder_1.newLine(); _builder_1.append(" "); _builder_1.append("new Object();"); _builder_1.newLine(); _builder_1.append(" "); _builder_1.append("new Object();"); _builder_1.newLine(); _builder_1.append("};"); _builder_1.newLine(); _builder_1.append("org.eclipse.xtext.xbase.lib.Procedures.Procedure1<? super Integer> fun = _function;"); _builder_1.newLine(); this.compilesTo(_builder, _builder_1, JavaVersion.JAVA8); }
protected String getComplianceLevelArg() { JavaVersion javaVersion = JavaVersion.JAVA5; if (generatorConfigProvider != null) { GeneratorConfig generatorConfig = generatorConfigProvider.get(null); javaVersion = generatorConfig.getJavaSourceVersion(); } return javaVersion.getComplianceLevelArg(); }
public InMemoryJavaCompiler(final ClassLoader parent, final JavaVersion javaVersion) { InMemoryJavaCompiler.ClassLoaderBasedNameEnvironment _classLoaderBasedNameEnvironment = new InMemoryJavaCompiler.ClassLoaderBasedNameEnvironment(parent); this.nameEnv = _classLoaderBasedNameEnvironment; this.parentClassLoader = parent; CompilerOptions _compilerOptions = new CompilerOptions(); this.compilerOptions = _compilerOptions; this.setJavaVersion(javaVersion); this.compilerOptions.inlineJsrBytecode = true; this.compilerOptions.preserveAllLocalVariables = true; }
/** * @since 2.11 */ public long setJavaVersion(final JavaVersion javaVersion) { long _xblockexpression = (long) 0; { final long classFmt = this.toClassFmt(javaVersion); this.setSourceLevel(classFmt); this.setComplianceLevel(classFmt); _xblockexpression = this.compilerOptions.targetJDK = classFmt; } return _xblockexpression; }
protected ITreeAppendable _generateModifier(final JvmOperation it, final ITreeAppendable appendable, final GeneratorConfig config) { ITreeAppendable _xblockexpression = null; { this.generateVisibilityModifier(it, appendable); boolean _isAbstract = it.isAbstract(); if (_isAbstract) { appendable.append("abstract "); } boolean _isStatic = it.isStatic(); if (_isStatic) { appendable.append("static "); } if ((((((!it.isAbstract()) && (!it.isStatic())) && config.getJavaSourceVersion().isAtLeast(JavaVersion.JAVA8)) && (it.eContainer() instanceof JvmGenericType)) && ((JvmGenericType) it.eContainer()).isInterface())) { appendable.append("default "); } boolean _isFinal = it.isFinal(); if (_isFinal) { appendable.append("final "); } boolean _isSynchronized = it.isSynchronized(); if (_isSynchronized) { appendable.append("synchronized "); } boolean _isStrictFloatingPoint = it.isStrictFloatingPoint(); if (_isStrictFloatingPoint) { appendable.append("strictfp "); } ITreeAppendable _xifexpression = null; boolean _isNative = it.isNative(); if (_isNative) { _xifexpression = appendable.append("native "); } _xblockexpression = _xifexpression; } return _xblockexpression; }
public List<IResolvedExecutable> getImplementationCandidates(final JvmDeclaredType type, final boolean isAnonymous) { if (((type == null) || (!(type instanceof JvmGenericType)))) { return Collections.<IResolvedExecutable>emptyList(); } JavaVersion sourceVersion = this.generatorConfigProvider.get(type).getJavaSourceVersion(); ResolvedFeatures resolvedFeatures = this.overrideHelper.getResolvedFeatures(type, sourceVersion); List<IResolvedExecutable> result = Lists.<IResolvedExecutable>newArrayList(); LightweightTypeReference _type = resolvedFeatures.getType(); ContextualVisibilityHelper contextualVisibilityHelper = new ContextualVisibilityHelper(this.visibilityHelper, _type); this.addOperationCandidates(resolvedFeatures, contextualVisibilityHelper, result); if (((!isAnonymous) && (!((JvmGenericType) type).isInterface()))) { this.addConstructorCandidates(resolvedFeatures, contextualVisibilityHelper, result); } return result; }
@Test public void testBug472265_01_lamda() throws Exception { GeneratorConfig generatorConfig = generatorConfigProvider.get(null); generatorConfig.setJavaSourceVersion(JavaVersion.JAVA8); assertCompilesTo("final closures.IAcceptors.IAcceptor _function = (String x) -> {\n" + "};\n" + "closures.IAcceptors.IAcceptor a = _function;", "{var closures.IAcceptors.IAcceptor a = [x|]}", generatorConfig); }
@Test public void testBug472265_02_lamda() throws Exception { GeneratorConfig generatorConfig = generatorConfigProvider.get(null); generatorConfig.setJavaSourceVersion(JavaVersion.JAVA8); assertCompilesTo("final closures.IAcceptors.IAcceptor2 _function = (String[] x) -> {\n" + "};\n" + "closures.IAcceptors.IAcceptor2 a = _function;", "{var closures.IAcceptors.IAcceptor2 a = [x|]}", generatorConfig); }
@Test public void testBug472265_03_lamda() throws Exception { GeneratorConfig generatorConfig = generatorConfigProvider.get(null); generatorConfig.setJavaSourceVersion(JavaVersion.JAVA8); assertCompilesTo("final closures.IAcceptors.IAcceptor3 _function = (String... x) -> {\n" + "};\n" + "closures.IAcceptors.IAcceptor3 a = _function;", "{var closures.IAcceptors.IAcceptor3 a = [x|]}", generatorConfig); }
@Test public void testBug472265_04_lamda() throws Exception { GeneratorConfig generatorConfig = generatorConfigProvider.get(null); generatorConfig.setJavaSourceVersion(JavaVersion.JAVA8); assertCompilesTo("final closures.IAcceptors.IAcceptor4 _function = (String x, String[] y) -> {\n" + "};\n" + "closures.IAcceptors.IAcceptor4 a = _function;", "{var closures.IAcceptors.IAcceptor4 a = [x,y|]}", generatorConfig); }
@Test public void testBug472265_05_lamda() throws Exception { GeneratorConfig generatorConfig = generatorConfigProvider.get(null); generatorConfig.setJavaSourceVersion(JavaVersion.JAVA8); assertCompilesTo("final closures.IAcceptors.IAcceptor5 _function = (String x, String... y) -> {\n" + "};\n" + "closures.IAcceptors.IAcceptor5 a = _function;", "{var closures.IAcceptors.IAcceptor5 a = [x,y|]}", generatorConfig); }
@Test public void testBug472265_06_lamda() throws Exception { GeneratorConfig generatorConfig = generatorConfigProvider.get(null); generatorConfig.setJavaSourceVersion(JavaVersion.JAVA8); assertCompilesTo("final closures.IAcceptors.IAcceptor6 _function = (String[] x, String[] y) -> {\n" + "};\n" + "closures.IAcceptors.IAcceptor6 a = _function;", "{var closures.IAcceptors.IAcceptor6 a = [x,y|]}", generatorConfig); }
@Test public void testBug472265_07_lamda() throws Exception { GeneratorConfig generatorConfig = generatorConfigProvider.get(null); generatorConfig.setJavaSourceVersion(JavaVersion.JAVA8); assertCompilesTo("final closures.IAcceptors.IAcceptor7 _function = (String[] x, String... y) -> {\n" + "};\n" + "closures.IAcceptors.IAcceptor7 a = _function;", "{var closures.IAcceptors.IAcceptor7 a = [x,y|]}", generatorConfig); }
protected void compilesTo(final CharSequence xbaseCode, final CharSequence result, Pair<JavaVersion, JavaVersion> minAndMaxVersion) throws Exception { int min = minAndMaxVersion.getKey().ordinal(); int max = minAndMaxVersion.getValue().ordinal(); if (min > max) throw new IllegalArgumentException(); for (int i = min; i <= max; i++) { JavaVersion version = JavaVersion.values()[i]; assertCompilesTo(result, xbaseCode, version); } }
@Test public void testOverride() throws Exception { StringConcatenation _builder = new StringConcatenation(); _builder.append("{ val x = #[\'a\', \'\', \'c\'].filter[!empty] }"); _builder.newLine(); StringConcatenation _builder_1 = new StringConcatenation(); _builder_1.append("final org.eclipse.xtext.xbase.lib.Functions.Function1<String, Boolean> _function = new org.eclipse.xtext.xbase.lib.Functions.Function1<String, Boolean>() {"); _builder_1.newLine(); _builder_1.append(" "); _builder_1.append("@Override"); _builder_1.newLine(); _builder_1.append(" "); _builder_1.append("public Boolean apply(final String it) {"); _builder_1.newLine(); _builder_1.append(" "); _builder_1.append("boolean _isEmpty = it.isEmpty();"); _builder_1.newLine(); _builder_1.append(" "); _builder_1.append("return Boolean.valueOf((!_isEmpty));"); _builder_1.newLine(); _builder_1.append(" "); _builder_1.append("}"); _builder_1.newLine(); _builder_1.append("};"); _builder_1.newLine(); _builder_1.append("final Iterable<String> x = org.eclipse.xtext.xbase.lib.IterableExtensions.<String>filter(java.util.Collections.<String>unmodifiableList(org.eclipse.xtext.xbase.lib.CollectionLiterals.<String>newArrayList(\"a\", \"\", \"c\")), _function);"); _builder_1.newLine(); Pair<JavaVersion, JavaVersion> _mappedTo = Pair.<JavaVersion, JavaVersion>of(JavaVersion.JAVA6, JavaVersion.JAVA7); this.compilesTo(_builder, _builder_1, _mappedTo); }
@Test public void testBasicForLoop_8() { try { StringConcatenation _builder = new StringConcatenation(); _builder.append("{"); _builder.newLine(); _builder.append("\t"); _builder.append("[| for (;;) {} ]"); _builder.newLine(); _builder.append("}"); _builder.newLine(); StringConcatenation _builder_1 = new StringConcatenation(); _builder_1.append("final org.eclipse.xtext.xbase.lib.Procedures.Procedure0 _function = () -> {"); _builder_1.newLine(); _builder_1.append(" "); _builder_1.append("for (;;) {"); _builder_1.newLine(); _builder_1.append(" "); _builder_1.append("}"); _builder_1.newLine(); _builder_1.append("};"); _builder_1.newLine(); _builder_1.append("return _function;"); _builder_1.newLine(); this.compilesTo(_builder, _builder_1, JavaVersion.JAVA8); } catch (Throwable _e) { throw Exceptions.sneakyThrow(_e); } }
@Test public void testExceptionOnClosure() throws Exception { StringConcatenation _builder = new StringConcatenation(); _builder.append("{val java.beans.VetoableChangeListener x = []}"); _builder.newLine(); StringConcatenation _builder_1 = new StringConcatenation(); _builder_1.append("final java.beans.VetoableChangeListener _function = (java.beans.PropertyChangeEvent it) -> {"); _builder_1.newLine(); _builder_1.append("};"); _builder_1.newLine(); _builder_1.append("final java.beans.VetoableChangeListener x = _function;"); _builder_1.newLine(); this.compilesTo(_builder, _builder_1, JavaVersion.JAVA8); }
@Test public void testJavaVersion7() { StringConcatenation _builder = new StringConcatenation(); _builder.append("java.util.List<String> list = new java.util.LinkedList<>();"); _builder.newLine(); this.assertJavaCompilation(_builder, JavaVersion.JAVA7); }
@Test public void testJavaVersion8() { StringConcatenation _builder = new StringConcatenation(); _builder.append("Runnable r = () -> {};"); _builder.newLine(); this.assertJavaCompilation(_builder, JavaVersion.JAVA8); }
private Class<?> assertJavaCompilation(final CharSequence input, final JavaVersion javaVersion) { Class<?> _xblockexpression = null; { if ((javaVersion != null)) { this.javaCompiler.setJavaVersion(javaVersion); } StringConcatenation _builder = new StringConcatenation(); _builder.append("package tests;"); _builder.newLine(); _builder.newLine(); _builder.append("public class Main {"); _builder.newLine(); _builder.append("\t"); _builder.append("public static void main(String args[]) {"); _builder.newLine(); _builder.append("\t\t"); _builder.append(input, "\t\t"); _builder.newLineIfNotEmpty(); _builder.append("\t"); _builder.append("}"); _builder.newLine(); _builder.append("}"); _builder.newLine(); _xblockexpression = this.javaCompiler.compileToClass("tests.Main", _builder.toString()); } return _xblockexpression; }
/** * @since 2.9 */ public JavaVersionModule(JavaVersion version) { this.targetVersion = version; }
/** * Creates a new OnTheFlyCompiler that accepts Java6 compliant code. */ @Inject public OnTheFlyJavaCompiler2(final ClassLoader scope) { this(scope, JavaVersion.JAVA6); }
public OnTheFlyJavaCompiler2(final ClassLoader scope, final JavaVersion version) { InMemoryJavaCompiler _inMemoryJavaCompiler = new InMemoryJavaCompiler(scope, version); this.inMemoryCompiler = _inMemoryJavaCompiler; this.javaVersion = version; }
/** * @since 2.11 */ public void setJavaVersion(final JavaVersion version) { this.inMemoryCompiler.setJavaVersion(version); this.javaVersion = version; }
/** * @since 2.11 */ public JavaVersion getJavaVersion() { return this.javaVersion; }
public ResolvedFeatures(LightweightTypeReference type, OverrideTester overrideTester, JavaVersion targetVersion) { super(type, overrideTester); this.targetVersion = targetVersion; }
/** * Returns the resolved features targeting a specific Java version in order to support new language features. */ public ResolvedFeatures getResolvedFeatures(JvmDeclaredType type, JavaVersion targetVersion) { ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, type.eResource().getResourceSet()); LightweightTypeReference contextType = owner.toLightweightTypeReference(type); return getResolvedFeatures(contextType, targetVersion); }
/** * Returns the resolved features targeting a specific Java version in order to support new language features. */ public ResolvedFeatures getResolvedFeatures(JvmTypeReference contextType, JavaVersion targetVersion) { ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, contextType.eResource().getResourceSet()); return getResolvedFeatures(owner.toLightweightTypeReference(contextType), targetVersion); }
/** * Returns the resolved features targeting a specific Java version in order to support new language features. */ public ResolvedFeatures getResolvedFeatures(LightweightTypeReference contextType, JavaVersion targetVersion) { return new ResolvedFeatures(contextType, overrideTester, targetVersion); }
@Pure public JavaVersion getJavaSourceVersion() { return this.javaSourceVersion; }
public void setJavaSourceVersion(final JavaVersion javaSourceVersion) { this.javaSourceVersion = javaSourceVersion; }
private String reassignSuperType(final ITreeAppendable b, final JvmDeclaredType declaredType, final GeneratorConfig config) { String _xblockexpression = null; { JvmTypeReference _extendedClass = declaredType.getExtendedClass(); JvmType _type = null; if (_extendedClass!=null) { _type=_extendedClass.getType(); } final JvmType superType = _type; boolean _hasObject = b.hasObject("super"); if (_hasObject) { final Object element = b.getObject("this"); if ((element instanceof JvmDeclaredType)) { final Object superElement = b.getObject("super"); final String superVariable = b.getName(superElement); boolean _equals = "super".equals(superVariable); if (_equals) { String _simpleName = ((JvmDeclaredType)element).getSimpleName(); final String proposedName = (_simpleName + ".super"); b.declareVariable(superElement, proposedName); } } } boolean _isAtLeast = config.getJavaSourceVersion().isAtLeast(JavaVersion.JAVA8); if (_isAtLeast) { Iterable<JvmTypeReference> _extendedInterfaces = declaredType.getExtendedInterfaces(); for (final JvmTypeReference interfaceRef : _extendedInterfaces) { { final JvmType interfaze = interfaceRef.getType(); String _simpleName_1 = interfaze.getSimpleName(); final String simpleVarName = (_simpleName_1 + ".super"); boolean _hasObject_1 = b.hasObject(simpleVarName); if (_hasObject_1) { final Object element_1 = b.getObject(simpleVarName); boolean _notEquals = (!Objects.equal(element_1, interfaceRef)); if (_notEquals) { String _qualifiedName = interfaze.getQualifiedName(); final String qualifiedVarName = (_qualifiedName + ".super"); b.declareVariable(interfaze, qualifiedVarName); } } else { b.declareVariable(interfaze, simpleVarName); } } } } String _xifexpression = null; if ((superType != null)) { _xifexpression = b.declareVariable(superType, "super"); } _xblockexpression = _xifexpression; } return _xblockexpression; }