Skip to content

Commit

Permalink
use reflection to read perprojectinfo.rootPathToResolvedEntries
Browse files Browse the repository at this point in the history
  • Loading branch information
cdietrich committed Aug 6, 2024
1 parent 87b6681 commit d90c16c
Showing 1 changed file with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2012 itemis AG (http://www.itemis.eu) and others.
* Copyright (c) 2012, 2024 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
Expand All @@ -11,6 +11,8 @@
import static com.google.common.collect.Lists.*;
import static org.eclipse.xtext.xbase.validation.IssueCodes.*;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

Expand All @@ -23,9 +25,11 @@
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.internal.core.ClasspathAccessRule;
import org.eclipse.jdt.internal.core.JavaModelManager.PerProjectInfo;
import org.eclipse.jdt.internal.core.JavaProject;
import org.eclipse.xtext.common.types.JvmConstructor;
import org.eclipse.xtext.common.types.JvmDeclaredType;
Expand All @@ -46,6 +50,7 @@
import org.eclipse.xtext.xbase.validation.IssueCodes;
import org.eclipse.xtext.xtype.XImportDeclaration;
import org.eclipse.xtext.xtype.XtypePackage;
import org.osgi.framework.Version;

import com.google.common.collect.Maps;
import com.google.inject.Inject;
Expand Down Expand Up @@ -223,7 +228,7 @@ protected IClasspathEntry getResolvedClasspathEntry(IJavaProject javaProject, /*
JavaProject castedProject = (JavaProject) javaProject;
castedProject.getResolvedClasspath(); // force the resolved entry cache to be populated
@SuppressWarnings("rawtypes")
Map rootPathToResolvedEntries = castedProject.getPerProjectInfo().rootPathToResolvedEntries;
Map rootPathToResolvedEntries = getRootPathToResolvedEntries(castedProject.getPerProjectInfo());
if (rootPathToResolvedEntries != null) {
result = (IClasspathEntry) rootPathToResolvedEntries.get(root.getPath());
if (result == null)
Expand All @@ -232,4 +237,25 @@ protected IClasspathEntry getResolvedClasspathEntry(IJavaProject javaProject, /*

return result;
}

final Version jdtCoreVersion = JavaCore.getPlugin().getBundle().getVersion();

protected Map getRootPathToResolvedEntries(PerProjectInfo info) {
if (jdtCoreVersion.compareTo(new Version(3, 39, 0)) >= 0) {
try {
Method m = PerProjectInfo.class.getDeclaredMethod("getRootPathToResolvedEntries");
return (Map) m.invoke(info);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
} else {
try {
Field f = PerProjectInfo.class.getDeclaredField("rootPathToResolvedEntries");
return (Map) f.get(info);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
}

}

0 comments on commit d90c16c

Please sign in to comment.