|
| 1 | +/* |
| 2 | + * Copyright 2010-2016 JetBrains s.r.o. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.jetbrains.kotlin.jps |
| 18 | + |
| 19 | +import org.jetbrains.jps.cmdline.ClasspathBootstrap |
| 20 | +import java.lang.reflect.Field |
| 21 | +import java.lang.reflect.Modifier |
| 22 | + |
| 23 | +fun disableJava6FileManager() { |
| 24 | + val fileManagerClass = ClasspathBootstrap.getOptimizedFileManagerClass() |
| 25 | + if (fileManagerClass?.simpleName == "OptimizedFileManager") { |
| 26 | + // JPS tests depends on idea.jar and can't be executed under Java 1.6 anymore. But currently TeamCity merges classpath from all |
| 27 | + // dependencies in a one big mess with no differences between JDK and non-JDK jars. Such behaviour causes both |
| 28 | + // JDK 8 and JDK 6 classes present in classpath. Next there's ClasspathBootstrap.OptimizedFileManagerClassHolder in idea |
| 29 | + // that works through reflection and tries to choose correct FileManager that are not the same in JDK 6 and JDK 8. Choosing |
| 30 | + // FileManager for JDK 6 leads to exceptions on Runtime as classes from JDK 8 goes first on teamcity. |
| 31 | + // |
| 32 | + // Here we disable JDK 6 FileManager with brute force. |
| 33 | + val clazz = Class.forName("org.jetbrains.jps.cmdline.ClasspathBootstrap\$OptimizedFileManagerClassHolder") |
| 34 | + setFinalStaticToNull(clazz.getDeclaredField("managerClass")) |
| 35 | + setFinalStaticToNull(clazz.getDeclaredField("directoryCacheClearMethod")) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +private fun setFinalStaticToNull(field: Field) { |
| 40 | + field.isAccessible = true; |
| 41 | + |
| 42 | + val modifiersField = (Field::class.java).getDeclaredField("modifiers") |
| 43 | + modifiersField.isAccessible = true |
| 44 | + modifiersField.setInt(field, field.modifiers and Modifier.FINAL.inv()) |
| 45 | + |
| 46 | + field.set(null, null) |
| 47 | +} |
0 commit comments