Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use reflection to read perprojectinfo.rootPathToResolvedEntries #3124

Merged
merged 6 commits into from
Aug 8, 2024
Merged
Changes from 1 commit
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
@@ -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,9 +11,12 @@
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;

import org.eclipse.core.runtime.IPath;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EStructuralFeature;
Expand All @@ -23,9 +26,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 +51,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 @@ -222,14 +228,38 @@ protected IClasspathEntry getResolvedClasspathEntry(IJavaProject javaProject, /*
IClasspathEntry result = null;
JavaProject castedProject = (JavaProject) javaProject;
castedProject.getResolvedClasspath(); // force the resolved entry cache to be populated
@SuppressWarnings("rawtypes")
Map rootPathToResolvedEntries = castedProject.getPerProjectInfo().rootPathToResolvedEntries;
Map<IPath, IClasspathEntry> rootPathToResolvedEntries = getRootPathToResolvedEntries(castedProject.getPerProjectInfo());
if (rootPathToResolvedEntries != null) {
result = (IClasspathEntry) rootPathToResolvedEntries.get(root.getPath());
result = rootPathToResolvedEntries.get(root.getPath());
if (result == null)
result = (IClasspathEntry) rootPathToResolvedEntries.get(root.getJavaProject().getPath());
result = rootPathToResolvedEntries.get(root.getJavaProject().getPath());
}

return result;
}

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

protected Map<IPath, IClasspathEntry> getRootPathToResolvedEntries(PerProjectInfo info) {
if (jdtCoreVersion.compareTo(new Version(3, 39, 0)) >= 0) {
cdietrich marked this conversation as resolved.
Show resolved Hide resolved
try {
Method m = PerProjectInfo.class.getDeclaredMethod("getRootPathToResolvedEntries");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:
We could you MethodHandles.findGetter and MethodHandles.findVirtual to obtain a MethodHandle once and use that throughout the lifecyle of this class.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

am not familiar with that api

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#3128
something like this?

@SuppressWarnings("unchecked")
Map<IPath, IClasspathEntry> result = (Map<IPath, IClasspathEntry>) m.invoke(info);
return result;
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
} else {
try {
Field f = PerProjectInfo.class.getDeclaredField("rootPathToResolvedEntries");
@SuppressWarnings("unchecked")
Map<IPath, IClasspathEntry> result = (Map<IPath, IClasspathEntry>) f.get(info);
return result;
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
}

}
Loading