5

We have an old project using gwt 2.6 and therefore we need to compile with -source 1.7 option.

The JRE and JDK used are 1.8, but compile with 1.7 source code option.

We want to use the new compute function from ConcurrentHashMap which is only available from java 8. When using eclipse and maven everything compiled well. On the other hand IntelliJ is complaining. My question is, will it work or will we have issues with it?

Will this project run?

Searching in google for ConcurrentHashmap compute, java 8 and source code level 1.7 did not give any info.

David Michael Gang
  • 7,107
  • 8
  • 53
  • 98
  • You may want to have a look at backport. [Here is a (possibly incomplete) list on wikipedia](https://en.wikipedia.org/wiki/Java_backporting_tools) – Lino Jul 23 '18 at 06:33
  • 1
    If you want you use Java 8 features in that old project, you would be better of upgrading to a newer version of GWT first. See https://stackoverflow.com/questions/15693169/java-8-support-in-gwt – Stephen C Jul 23 '18 at 10:53
  • 2
    The simplest thing you could do is to copy the implementation of the [default compute](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentMap.html#compute-K-java.util.function.BiFunction-) method from the `ConcurrentMap` interface into a new static method and add an additional `ConcurrentMap` argument for the `CHM` instance to operate on. Not as efficient as the tailored implementation in CHM but at least functionally equivalent. – Sartorius Jul 23 '18 at 12:38
  • I tried and deployed the resulting jar on a jre8 and everything worked till now. I had just to take care that it won't get to the get parts. But I agree that it may break soon – David Michael Gang Jul 24 '18 at 13:10
  • 1
    JRE8? You mean you 7? Besides: I appreciate the quick comeback! – GhostCat Jul 24 '18 at 13:12

2 Answers2

6

If at all, this would require a lot of careful "manual" work.

When you look at this question for example, you can find that there are various different ideas how people want to enable "stream based" functional programming with Java 7. Maybe, after doing a lot of research, you might be able to find similar things regarding such "enhanced collection" features.

But then, all of that might be quite fragile. The fact that some examples might work fine wouldn't mean that you would be able to run a large production code base on a Java 7 VM.

Thus more of a non-answer here: be careful how to invest your time and energy. Instead of trying to backport libraries to Java 7, rather look into moving your whole project onto Java8 at least. Especially keeping in mind that the release cadence for Java has changed significantly, and going with outdated Java versions for many years is simply even less desirable compared to a few years ago.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

Well, the simple answer is that it will not work. The target runtime will not have the updated API (i.e., the compute*** methods won't be on the version of Map that Java 7 has).

So if you deploy that code, the runtime will understand the class version, but will raise NoSuchMethodError and similar errors.

In addition to this, there are many reasons for upgrading your runtime.

ernest_k
  • 44,416
  • 5
  • 53
  • 99