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

Fix mapSource invocation #45

Merged
merged 2 commits into from Nov 6, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -36,6 +36,7 @@
import org.sf.feeling.decompiler.util.DecompileUtil;
import org.sf.feeling.decompiler.util.DecompilerOutputUtil;
import org.sf.feeling.decompiler.util.Logger;
import org.sf.feeling.decompiler.util.SourceMapperUtil;
import org.sf.feeling.decompiler.util.ReflectionUtils;
import org.sf.feeling.decompiler.util.SortMemberUtil;
import org.sf.feeling.decompiler.util.UIUtil;
Expand Down Expand Up @@ -96,7 +97,7 @@ public char[] findSource( IType type, IBinaryType info )
updateSourceRanges( type, attachedSource );
isAttachedSource = true;
mapSourceSwitch( type, attachedSource, true );
DecompileUtil.mapSource( ( (PackageFragmentRoot) root ).getSourceMapper( ), type, attachedSource, info );
SourceMapperUtil.mapSource( ( (PackageFragmentRoot) root ).getSourceMapper( ), type, attachedSource, info );
return attachedSource;
}
}
Expand Down Expand Up @@ -132,7 +133,7 @@ public char[] findSource( IType type, IBinaryType info )
updateSourceRanges( type, attachedSource );
isAttachedSource = true;
mapSourceSwitch( type, attachedSource, true );
DecompileUtil.mapSource( ( (PackageFragmentRoot) root ).getSourceMapper( ), type, attachedSource, info );
SourceMapperUtil.mapSource( ( (PackageFragmentRoot) root ).getSourceMapper( ), type, attachedSource, info );
return attachedSource;
}
}
Expand Down Expand Up @@ -239,7 +240,7 @@ public char[] findSource( IType type, IBinaryType info )
SourceMapper rootSourceMapper = originalSourceMapper.get( root );
if ( rootSourceMapper.findSource( type, info ) == null )
{
DecompileUtil.mapSource( rootSourceMapper, type, sourceAsCharArray, info );
SourceMapperUtil.mapSource( rootSourceMapper, type, sourceAsCharArray, info );
}
}

Expand Down
Expand Up @@ -19,6 +19,7 @@
import org.eclipse.jdt.internal.core.ClassFile;
import org.eclipse.jdt.internal.core.SourceMapper;
import org.sf.feeling.decompiler.util.DecompileUtil;
import org.sf.feeling.decompiler.util.SourceMapperUtil;

public class ClassFileSourceMap
{
Expand Down Expand Up @@ -61,7 +62,7 @@ private static void mapSource( JavaDecompilerBufferManager bufferManager, ClassF
// buffer.addBufferChangedListener( cf );

// do the source mapping
DecompileUtil.mapSource( mapper, getOuterMostEnclosingType( cf ), contents, info );
SourceMapperUtil.mapSource( mapper, getOuterMostEnclosingType( cf ), contents, info );

return;
}
Expand Down
Expand Up @@ -9,15 +9,12 @@
package org.sf.feeling.decompiler.editor;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.IParent;
Expand All @@ -29,7 +26,6 @@
import org.eclipse.jdt.internal.compiler.env.IBinaryType;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
import org.eclipse.jdt.internal.core.BinaryType;
import org.eclipse.jdt.internal.core.NamedMember;
import org.eclipse.jdt.internal.core.SourceMapper;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.BadLocationException;
Expand All @@ -38,7 +34,7 @@
import org.eclipse.text.edits.TextEdit;
import org.sf.feeling.decompiler.JavaDecompilerPlugin;
import org.sf.feeling.decompiler.util.DecompilerOutputUtil;
import org.sf.feeling.decompiler.util.ReflectionUtils;
import org.sf.feeling.decompiler.util.SourceMapperUtil;

public abstract class DecompilerSourceMapper extends SourceMapper
{
Expand Down Expand Up @@ -91,19 +87,9 @@ public void mapSourceSwitch( IType type, char[] contents, boolean force )
sourceRanges.remove( type );
}


try {
Method mapSource = getClass( ).getMethod(
"mapSource",
new Class[] { IType.class, char[].class, IBinaryType.class } );

mapSource.invoke( this, new Object[] { type, contents, null} );
} catch (NoSuchMethodException e) {
// API changed with Java 9 support (#daa227e4f5b7af888572a286c4f973b7a167ff2e)
ReflectionUtils.invokeMethod( this, "mapSource", new Class[]{ //$NON-NLS-1$
NamedMember.class, char[].class, IBinaryType.class
}, new Object[]{
type, contents, null
} );
SourceMapperUtil.mapSource( this, type, contents, null );
} catch (Exception e) {
// Method was found but invocation failed, this shouldn't happen.
}
Expand Down
Expand Up @@ -26,19 +26,18 @@
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.internal.compiler.env.IBinaryType;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
import org.eclipse.jdt.internal.core.BinaryType;
import org.eclipse.jdt.internal.core.ClassFile;
import org.eclipse.jdt.internal.core.ImportContainer;
import org.eclipse.jdt.internal.core.ImportContainerInfo;
import org.eclipse.jdt.internal.core.ImportDeclaration;
import org.eclipse.jdt.internal.core.ImportDeclarationElementInfo;
import org.eclipse.jdt.internal.core.JavaElement;
import org.eclipse.jdt.internal.core.JavaModelManager;
import org.eclipse.jdt.internal.core.NamedMember;
import org.eclipse.jdt.internal.core.OpenableElementInfo;
import org.eclipse.jdt.internal.core.SourceMapper;
import org.sf.feeling.decompiler.util.DecompilerOutputUtil;
import org.sf.feeling.decompiler.util.Logger;
import org.sf.feeling.decompiler.util.SourceMapperUtil;
import org.sf.feeling.decompiler.util.ReflectionUtils;

public class ImportSourceMapper extends SourceMapper
Expand Down Expand Up @@ -106,18 +105,7 @@ public ISourceRange mapSourceSwitch ( IType type, char[] contents, IBinaryType i
}

try {
Method mapSource = getClass( ).getMethod(
"mapSource",
new Class[] { IType.class, char[].class, IBinaryType.class, IJavaElement.class } );

return (ISourceRange) mapSource.invoke( this, new Object[] { type, contents, info, elementToFind } );
} catch (NoSuchMethodException e) {
// API changed with Java 9 support (#daa227e4f5b7af888572a286c4f973b7a167ff2e)
return (ISourceRange) ReflectionUtils.invokeMethod( this, "mapSource", new Class[]{ //$NON-NLS-1$
NamedMember.class, char[].class, IBinaryType.class, IJavaElement.class
}, new Object[]{
type, contents, info, elementToFind
} );
SourceMapperUtil.mapSource( this, type, contents, info, elementToFind );
} catch (Exception e) {
// Method was found but invocation failed, this shouldn't happen.
}
Expand Down
Expand Up @@ -22,7 +22,6 @@
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.compiler.env.IBinaryType;
import org.eclipse.jdt.internal.core.ClassFile;
import org.eclipse.jdt.internal.core.NamedMember;
import org.eclipse.jdt.internal.core.SourceMapper;
import org.eclipse.ui.ide.FileStoreEditorInput;
import org.sf.feeling.decompiler.editor.DecompilerSourceMapper;
Expand Down Expand Up @@ -105,7 +104,7 @@ public static void updateSourceRanges( IClassFile cf, String contents ) throws J
new Object[0] );
HashMap sourceRange = (HashMap) ReflectionUtils.getFieldValue( mapper, "sourceRanges" ); //$NON-NLS-1$
sourceRange.remove( type );
DecompileUtil.mapSource( mapper, type, contents.toCharArray( ), typeInfo );
SourceMapperUtil.mapSource( mapper, type, contents.toCharArray( ), typeInfo );

// List rootPaths = (List) ReflectionUtils.getFieldValue( mapper,
// "rootPaths" );
Expand Down Expand Up @@ -169,22 +168,4 @@ public static String deleteOneEmptyLine( String origSrc )
}
return null;
}

public static void mapSource(SourceMapper sourceMapper, IType type, char[] source, IBinaryType info)
{
try {
ReflectionUtils.invokeMethod( sourceMapper, "mapSource", new Class[]{ //$NON-NLS-1$
IType.class, char[].class, IBinaryType.class
}, new Object[]{
type, source, info
} );
} catch (final NoSuchMethodError e) {
// API changed with Java 9 support (#daa227e4f5b7af888572a286c4f973b7a167ff2e)
ReflectionUtils.invokeMethod( sourceMapper, "mapSourceSwitch", new Class[]{ //$NON-NLS-1$
NamedMember.class, char[].class, IBinaryType.class
}, new Object[]{
type, source, info
} );
}
}
}
Expand Up @@ -62,6 +62,11 @@ public static Object invokeMethod( Object object, String methodName, Class[] par
return null;

Method method = getDeclaredMethod( object, methodName, parameterTypes );
return invokeMethod(method, object, parameters);
}

public static Object invokeMethod( Method method, Object object, Object[] parameters )
{
try
{
if ( null != method )
Expand Down
@@ -0,0 +1,49 @@
package org.sf.feeling.decompiler.util;

import java.lang.reflect.Method;

import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.internal.compiler.env.IBinaryType;
import org.eclipse.jdt.internal.core.NamedMember;
import org.eclipse.jdt.internal.core.SourceMapper;

public class SourceMapperUtil
{
private static final String MAP_SOURCE_METHOD_NAME = "mapSource"; //$NON-NLS-1$

public static void mapSource(SourceMapper sourceMapper, IType type, char[] source, IBinaryType info)
{
mapSource(sourceMapper, type, source, info, null);
}

public static void mapSource(SourceMapper sourceMapper, IType type, char[] source, IBinaryType info, IJavaElement elementToFind) {
Method mapSourceMethod = getMapSourceMethod( sourceMapper );

// API changed with Java 9 support (#daa227e4f5b7af888572a286c4f973b7a167ff2e)
if (mapSourceMethod == null) {
mapSourceMethod = getLegacyMapSourceMethod( sourceMapper );
}

if (mapSourceMethod != null) {
Object[] parameters = new Object[] { type, source, info, elementToFind };
ReflectionUtils.invokeMethod( mapSourceMethod, sourceMapper, parameters );
} else {
throw new IllegalStateException( "Unable to invoke mapSource method on sourceMapper" ); //$NON-NLS-1$
}
}

private static Method getMapSourceMethod( SourceMapper sourceMapper )
{
return ReflectionUtils.getDeclaredMethod( sourceMapper, MAP_SOURCE_METHOD_NAME, new Class[]{
NamedMember.class, char[].class, IBinaryType.class, IJavaElement.class
});
}

private static Method getLegacyMapSourceMethod( SourceMapper sourceMapper )
{
return ReflectionUtils.getDeclaredMethod( sourceMapper, MAP_SOURCE_METHOD_NAME, new Class[]{
IType.class, char[].class, IBinaryType.class, IJavaElement.class
});
}
}