Java Code Examples for org.eclipse.xtext.util.JavaVersion#fromQualifier()

The following examples show how to use org.eclipse.xtext.util.JavaVersion#fromQualifier() . 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: JavaVersionCheckExtension.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
	// Check if the minimal version of Java is used for running the tests.
	final JavaVersion cVersion = JavaVersion.fromQualifier(System.getProperty("java.specification.version"));
	if (cVersion == null) {
		return ConditionEvaluationResult.disabled("You must use JDK " + SARLVersion.MINIMAL_JDK_VERSION_FOR_SARL_COMPILATION_ENVIRONMENT + " or higher for running the tests.");
	}
	final JavaVersion mVersion = JavaVersion.fromQualifier(SARLVersion.MINIMAL_JDK_VERSION_FOR_SARL_COMPILATION_ENVIRONMENT);
	if (mVersion == null || !cVersion.isAtLeast(mVersion)) {
		return ConditionEvaluationResult.disabled("You must use JDK " + SARLVersion.MINIMAL_JDK_VERSION_FOR_SARL_COMPILATION_ENVIRONMENT + " or higher for running the tests.");
	}
	final JavaVersion xVersion = JavaVersion.fromQualifier(SARLVersion.INCOMPATIBLE_JDK_VERSION_FOR_SARL_COMPILATION_ENVIRONMENT);
	// If null the max version that is specified into the SARL configuration is not yey supported by Xtext enumeration
	if (xVersion != null && cVersion.isAtLeast(xVersion)) {
		return ConditionEvaluationResult.disabled("You must use JDK strictly below " + SARLVersion.INCOMPATIBLE_JDK_VERSION_FOR_SARL_COMPILATION_ENVIRONMENT + " for running the tests.");
	}
	return ConditionEvaluationResult.enabled("supported version of JDK");
}
 
Example 2
Source File: AbstractXtendCompilerMojo.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
private String getBootClassPath() {
	Toolchain toolchain = toolchainManager.getToolchainFromBuildContext("jdk", session);
	if (toolchain instanceof DefaultJavaToolChain) {
		DefaultJavaToolChain javaToolChain = (DefaultJavaToolChain) toolchain;
		getLog().info("Using toolchain " + javaToolChain);

		if (javaSourceVersion != null) {
			JavaVersion version = JavaVersion.fromQualifier(javaSourceVersion);
			if (version.isAtLeast(JavaVersion.JAVA9)) {
				return ""; // bootclasspath only supported on Java8 and older
			}
		}

		String[] includes = { "jre/lib/*", "jre/lib/ext/*", "jre/lib/endorsed/*" };
		String[] excludes = new String[0];
		Xpp3Dom config = (Xpp3Dom) javaToolChain.getModel().getConfiguration();
		if (config != null) {
			Xpp3Dom bootClassPath = config.getChild("bootClassPath");
			if (bootClassPath != null) {
				Xpp3Dom includeParent = bootClassPath.getChild("includes");
				if (includeParent != null) {
					includes = getValues(includeParent.getChildren("include"));
				}
				Xpp3Dom excludeParent = bootClassPath.getChild("excludes");
				if (excludeParent != null) {
					excludes = getValues(excludeParent.getChildren("exclude"));
				}
			}
		}

		return scanBootclasspath(javaToolChain.getJavaHome(), includes, excludes);
	}
	return "";
}
 
Example 3
Source File: XtendBatchCompiler.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * This option is only supported on JDK 8 and older and will be ignored when source level is 9 or newer.
 * @since 2.7
 * @see <a href="https://www.oracle.com/technetwork/java/javase/9-relnote-issues-3704069.html">Java 9 Release Notes</a>
 */
public void setBootClassPath(String bootClassPath) {
	JavaVersion version = JavaVersion.fromQualifier(getJavaSourceVersion());
	if (version.isAtLeast(JavaVersion.JAVA9)) {
		if (!bootClassPath.isEmpty()) {
			log.warn("Option bootClassPath is only valid for Java 8 and lower. The value '"+bootClassPath+"' will be ignored.");
		}
	} else {
		this.bootClassPath = bootClassPath;
	}
}
 
Example 4
Source File: XtendBatchCompiler.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.8
 */
public void setJavaSourceVersion(final String javaSourceVersion) {
	JavaVersion javaVersion = JavaVersion.fromQualifier(javaSourceVersion);
	if(javaVersion == null) {
		List<String> qualifiers = Lists.newArrayList();
		for (JavaVersion version : JavaVersion.values())
			qualifiers.addAll(version.getAllQualifiers());
		
		throw new IllegalArgumentException("Unknown Java Version Qualifier: '" + javaSourceVersion + "'. Valid values are: '" + Joiner.on(", ").join(qualifiers) + "'");
	}
	generatorConfig.setJavaSourceVersion(javaVersion);
}
 
Example 5
Source File: SARLGeneratorConfigProvider.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Invoked for creating the default generator configuration.
 *
 * @return the configuration.
 */
protected GeneratorConfig createDefaultGeneratorConfig() {
	final GeneratorConfig config = new GeneratorConfig();
	if (this.defaultVersion == null) {
		this.defaultVersion = JavaVersion.fromQualifier(System.getProperty("java.specification.version")); //$NON-NLS-1$
		if (this.defaultVersion != null) {
			config.setJavaSourceVersion(this.defaultVersion);
		}
	} else {
		config.setJavaSourceVersion(this.defaultVersion);
	}
	return config;
}
 
Example 6
Source File: AbstractSarlBatchCompilerMojo.java    From sarl with Apache License 2.0 5 votes vote down vote up
private String getBootClassPath() throws MojoExecutionException {
	// Bootclasspath is only supported on Java8 and older
	final String sourceVersionStr = getSourceVersion();
	if (!Strings.isEmpty(sourceVersionStr)) {
		final JavaVersion sourceVersion = JavaVersion.fromQualifier(sourceVersionStr);
		if (sourceVersion != null && sourceVersion.isAtLeast(JavaVersion.JAVA9)) {
			return ""; //$NON-NLS-1$
		}
	}
	final Toolchain toolchain = this.toolchainManager.getToolchainFromBuildContext("jdk", this.mavenHelper.getSession()); //$NON-NLS-1$
	if (toolchain instanceof JavaToolchain && toolchain instanceof ToolchainPrivate) {
		final JavaToolchain javaToolChain = (JavaToolchain) toolchain;
		final ToolchainPrivate privateJavaToolChain = (ToolchainPrivate) toolchain;
		getLog().info(MessageFormat.format(Messages.AbstractSarlBatchCompilerMojo_5, javaToolChain));

		String[] includes = {"jre/lib/*", "jre/lib/ext/*", "jre/lib/endorsed/*"}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		String[] excludes = new String[0];
		final Xpp3Dom config = (Xpp3Dom) privateJavaToolChain.getModel().getConfiguration();
		if (config != null) {
			final Xpp3Dom bootClassPath = config.getChild("bootClassPath"); //$NON-NLS-1$
			if (bootClassPath != null) {
				final Xpp3Dom includeParent = bootClassPath.getChild("includes"); //$NON-NLS-1$
				if (includeParent != null) {
					includes = getValues(includeParent.getChildren("include")); //$NON-NLS-1$
				}
				final Xpp3Dom excludeParent = bootClassPath.getChild("excludes"); //$NON-NLS-1$
				if (excludeParent != null) {
					excludes = getValues(excludeParent.getChildren("exclude")); //$NON-NLS-1$
				}
			}
		}

		try {
			return scanBootclasspath(Objects.toString(this.reflect.invoke(javaToolChain, "getJavaHome")), includes, excludes); //$NON-NLS-1$
		} catch (Exception e) {
			throw new MojoExecutionException(e.getLocalizedMessage(), e);
		}
	}
	return ""; //$NON-NLS-1$
}
 
Example 7
Source File: SarlBatchCompiler.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Change the boot classpath.
 * This option is only supported on JDK 8 and older and will be ignored when source level is 9 or newer.
 *
 * <p>The boot classpath is a list the names of folders or jar files that are separated by {@link File#pathSeparator}.
 *
 * @param bootClasspath the new boot classpath.
 * @see "https://www.oracle.com/technetwork/java/javase/9-relnote-issues-3704069.html"
 */
public void setBootClassPath(String bootClasspath) {
	final JavaVersion version = JavaVersion.fromQualifier(getJavaSourceVersion());
	if (version.isAtLeast(JavaVersion.JAVA9)) {
		reportInternalWarning(MessageFormat.format(Messages.SarlBatchCompiler_63, bootClasspath));
	}
	if (Strings.isEmpty(bootClasspath)) {
		this.bootClasspath = null;
	} else {
		this.bootClasspath = new ArrayList<>();
		for (final String path : Strings.split(bootClasspath, File.pathSeparator)) {
			this.bootClasspath.add(normalizeFile(path));
		}
	}
}
 
Example 8
Source File: SarlBatchCompiler.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Change the boot classpath.
 * This option is only supported on JDK 8 and older and will be ignored when source level is 9 or newer.
 *
 * @param bootClasspath the new boot classpath.
 * @see "https://www.oracle.com/technetwork/java/javase/9-relnote-issues-3704069.html"
 */
public void setBootClassPath(Collection<File> bootClasspath) {
	final JavaVersion version = JavaVersion.fromQualifier(getJavaSourceVersion());
	if (version.isAtLeast(JavaVersion.JAVA9)) {
		reportInternalWarning(MessageFormat.format(Messages.SarlBatchCompiler_63,
				Joiner.on(File.pathSeparator).join(bootClasspath)));
	}
	if (bootClasspath == null || bootClasspath.isEmpty()) {
		this.bootClasspath = null;
	} else {
		this.bootClasspath = new ArrayList<>(bootClasspath);
	}
}
 
Example 9
Source File: SarlBatchCompiler.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Change the version of the Java source to be used for the generated Java files.
 *
 * @param version the Java version.
 */
public void setJavaSourceVersion(String version) {
	final JavaVersion javaVersion = JavaVersion.fromQualifier(version);
	if (javaVersion == null) {
		final List<String> qualifiers = new ArrayList<>();
		for (final JavaVersion vers : JavaVersion.values()) {
			qualifiers.addAll(vers.getAllQualifiers());
		}

		throw new RuntimeException(MessageFormat.format(
				Messages.SarlBatchCompiler_0, version, Joiner.on(Messages.SarlBatchCompiler_1).join(qualifiers)));
	}
	getGeneratorConfig().setJavaSourceVersion(javaVersion);
}
 
Example 10
Source File: ExtendedSARLInjectorProvider.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Override
public OnTheFlyJavaCompiler2 get() {
	final String minJava = SARLVersion.MINIMAL_JDK_VERSION_FOR_SARL_COMPILATION_ENVIRONMENT;
	final JavaVersion jversion = JavaVersion.fromQualifier(minJava);
	return new OnTheFlyJavaCompiler2(
			SARLInjectorProvider.class.getClassLoader(),
			jversion);
}
 
Example 11
Source File: XbaseBuilderPreferenceAccess.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public JavaVersion fromCompilerSourceLevel(String compilerSource) {
	JavaVersion javaVersion = JavaVersion.fromQualifier(compilerSource);
	if (javaVersion == null)
		javaVersion = JavaVersion.JAVA5;
	return javaVersion;
}
 
Example 12
Source File: XtendBatchCompiler.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @since 2.8
 */
protected boolean preCompile(File classDirectory, Iterable<String> sourcePathDirectories, Iterable<String> classPathEntries) {
	List<String> commandLine = Lists.newArrayList();
	// todo args
	if (isVerbose()) {
		commandLine.add("-verbose");
	}
	if (getJavaSourceVersion() != null) {
		JavaVersion version = JavaVersion.fromQualifier(getJavaSourceVersion());
		if (!version.isAtLeast(JavaVersion.JAVA9)) {
			List<String> bootClassPathEntries = getBootClassPathEntries();
			if (!isEmpty(bootClassPathEntries)) {
				commandLine.add("-bootclasspath \"" + concat(File.pathSeparator, bootClassPathEntries) + "\"");
			}
		}
	}
	if (!isEmpty(classPathEntries)) {
		commandLine.add("-cp \"" + Joiner.on(File.pathSeparator).join(classPathEntries) + "\"");
	}
	commandLine.add("-d \"" + classDirectory.toString() + "\"");
	commandLine.add("-" + getComplianceLevel());
	commandLine.add("-proceedOnError");
	if (encodingProvider.getDefaultEncoding() != null) {
		commandLine.add("-encoding \"" + encodingProvider.getDefaultEncoding() + "\"");
	}
	if (additionalPreCompileArgs != null) {
		commandLine.add(additionalPreCompileArgs);
	}
	List<String> sourceDirectories = newArrayList(sourcePathDirectories);
	commandLine.add(concat(" ", transform(sourceDirectories, new Function<String, String>() {
		@Override
		public String apply(String path) {
			return "\"" + path + "\"";
		}
	})));
	if (log.isDebugEnabled()) {
		log.debug("invoke batch compiler with '" + concat(" ", commandLine) + "'");
	}
	PrintWriter outWriter = getStubCompilerOutputWriter();
	return BatchCompiler.compile(concat(" ", commandLine), outWriter, outWriter, null);
}
 
Example 13
Source File: AbstractDocumentationMojo.java    From sarl with Apache License 2.0 4 votes vote down vote up
/** Create a parser for the given file.
 *
 * @param inputFile the file to be parsed.
 * @return the parser.
 * @throws MojoExecutionException if the parser cannot be created.
 * @throws IOException if a classpath entry cannot be found.
 */
@SuppressWarnings("checkstyle:npathcomplexity")
protected AbstractMarkerLanguageParser createLanguageParser(File inputFile) throws MojoExecutionException, IOException {
	final AbstractMarkerLanguageParser parser;
	if (isFileExtension(inputFile, MarkdownParser.MARKDOWN_FILE_EXTENSIONS)) {
		parser = this.injector.getInstance(MarkdownParser.class);
	} else {
		throw new MojoExecutionException(MessageFormat.format(Messages.AbstractDocumentationMojo_3, inputFile));
	}
	parser.setGithubExtensionEnable(this.githubExtension);

	final SarlDocumentationParser internalParser = parser.getDocumentParser();

	if (this.isLineContinuationEnable) {
		internalParser.setLineContinuation(SarlDocumentationParser.DEFAULT_LINE_CONTINUATION);
	} else {
		internalParser.addLowPropertyProvider(createProjectProperties());
	}

	final ScriptExecutor scriptExecutor = internalParser.getScriptExecutor();
	final StringBuilder cp = new StringBuilder();
	final List<File> fullCp = getClassPath();
	for (final File cpElement : fullCp) {
		if (cp.length() > 0) {
			cp.append(File.pathSeparator);
		}
		cp.append(cpElement.getAbsolutePath());
	}
	scriptExecutor.setClassPath(cp.toString());
	scriptExecutor.setClassLoaderBuilder(it -> getProjectClassLoader(it, fullCp, fullCp.size()));
	final String bootPath = getBootClassPath();
	if (!Strings.isEmpty(bootPath)) {
		scriptExecutor.setBootClassPath(bootPath);
	}
	JavaVersion version = null;
	if (!Strings.isEmpty(this.source)) {
		version = JavaVersion.fromQualifier(this.source);
	}
	if (version == null) {
		version = JavaVersion.JAVA8;
	}
	scriptExecutor.setJavaSourceVersion(version.getQualifier());
	scriptExecutor.setTempFolder(this.tempDirectory.getAbsoluteFile());

	internalParser.addLowPropertyProvider(createProjectProperties());
	internalParser.addLowPropertyProvider(this.session.getCurrentProject().getProperties());
	internalParser.addLowPropertyProvider(this.session.getUserProperties());
	internalParser.addLowPropertyProvider(this.session.getSystemProperties());
	internalParser.addLowPropertyProvider(createGeneratorProperties());
	final Properties defaultValues = createDefaultValueProperties();
	if (defaultValues != null) {
		internalParser.addLowPropertyProvider(defaultValues);
	}
	return parser;
}