Skip to content
This repository has been archived by the owner on Apr 21, 2023. It is now read-only.

[eclipse/xtext-core#517] Moved Class File Constant Calculation to JavaVersion Enum #615

Merged
merged 1 commit into from
Mar 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import org.eclipse.jdt.core.compiler.CategorizedProblem
import org.eclipse.jdt.internal.compiler.Compiler
import org.eclipse.jdt.internal.compiler.DefaultErrorHandlingPolicies
import org.eclipse.jdt.internal.compiler.batch.CompilationUnit
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader
import org.eclipse.jdt.internal.compiler.env.ICompilationUnit
import org.eclipse.jdt.internal.compiler.env.INameEnvironment
Expand All @@ -29,7 +28,6 @@ import org.eclipse.xtend.lib.annotations.Accessors
import org.eclipse.xtend.lib.annotations.Data
import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor
import org.eclipse.xtext.util.JavaVersion
import org.eclipse.xtext.xbase.compiler.InMemoryJavaCompiler.Result

/**
*
Expand Down Expand Up @@ -147,13 +145,7 @@ class InMemoryJavaCompiler {
}

private def long toClassFmt(JavaVersion version) {
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 // ClassFileConstants.JDK1_8
case JAVA9: return ((ClassFileConstants.MAJOR_VERSION_1_7 + 2) << 16) + ClassFileConstants.MINOR_VERSION_0 // ClassFileConstants.JDK1_9
}
version.toJdtClassFileConstant
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.eclipse.jdt.internal.compiler.ICompilerRequestor;
import org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy;
import org.eclipse.jdt.internal.compiler.batch.CompilationUnit;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader;
import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
import org.eclipse.jdt.internal.compiler.env.INameEnvironment;
Expand Down Expand Up @@ -234,23 +233,7 @@ public InMemoryJavaCompiler(final ClassLoader parent, final CompilerOptions comp
}

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;
return version.toJdtClassFileConstant();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*******************************************************************************
* Copyright (c) 2018 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.eclipse.xtext.xbase.ui.tests;

import static org.junit.Assert.*;

import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
import org.eclipse.xtext.util.JavaVersion;
import org.junit.Test;

/**
* @author Christian Dietrich - Initial contribution and API
*/
public class JavaVersionExtendedTest {

@Test
public void testToJdtClassFileConstant() {
assertEquals(ClassFileConstants.JDK1_5, JavaVersion.JAVA5.toJdtClassFileConstant());
assertEquals(ClassFileConstants.JDK1_6, JavaVersion.JAVA6.toJdtClassFileConstant());
assertEquals(ClassFileConstants.JDK1_7, JavaVersion.JAVA7.toJdtClassFileConstant());
try {
long value = ClassFileConstants.class.getField("JDK1_8").getLong(null);
assertEquals(value, JavaVersion.JAVA8.toJdtClassFileConstant());
} catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException | SecurityException e) {
// ok
}
try {
long value = ClassFileConstants.class.getField("JDK9").getLong(null);
assertEquals(value, JavaVersion.JAVA9.toJdtClassFileConstant());
} catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException | SecurityException e) {
// ok
}
}

}