public static ClassLoader getParent( ClassLoader cl ) { if( cl.getClass().getTypeName().equals( "com.intellij.ide.plugins.cl.PluginClassLoader" ) ) { try { ClassLoader[] parents = (ClassLoader[])ReflectUtil.field( cl, "myParents" ).get(); return parents.length > 0 ? parents[0] : null; } catch( Exception e ) { //## todo: log error e.printStackTrace(); } } return null; } }
protected boolean testForArg( String name, String[] args ) { boolean isPresent = isArgPresent( name, args ); if( !isPresent ) { // maven doesn't like the -Xplugin:"Manifold strings", it doesn't parse "Manifold string" as plugin name and argument, so we do it here: try { String[] rawArgs = (String[])ReflectUtil.field( _javacTask, "args" ).get(); isPresent = Arrays.stream( rawArgs ).anyMatch( arg -> arg.contains( "-Xplugin:" ) && arg.contains( "Manifold" ) && arg.contains( name ) ); } catch( Exception ignore ) { } } return isPresent; }
Map/*<String, ModuleReference>*/nameToModule = (Map)nameToModuleField.get(); for( Object mr: nameToModule.values() )
ReflectUtil.field( type.tsym.owner, "kind" ).get() == ReflectUtil.field( "com.sun.tools.javac.code.Kinds$Kind", "TYP" ).getStatic() )
ReflectUtil.method( ReflectUtil.type( "com.sun.tools.javac.comp.Modules" ), "instance", Context.class ).invokeStatic( context ), "allModules" ).get(); List<Object> requires = (List<Object>)ReflectUtil.field( rootModule, "requires" ).get(); List<Object> newRequires = new ArrayList( requires ); Object addedRequires = ReflectUtil.constructor( "com.sun.tools.javac.code.Directive$RequiresDirective", ReflectUtil.type( "com.sun.tools.javac.code.Symbol$ModuleSymbol" ) ).newInstance( moduleToOpen ); ReflectUtil.field( rootModule, "requires" ).set( requires ); List<Object> exports = new ArrayList<>( (Collection)ReflectUtil.field( moduleToOpen, "exports" ).get() ); for( Symbol pkg : (Iterable<Symbol>)ReflectUtil.field( moduleToOpen, "enclosedPackages" ).get() ) exports.add( exp ); ((Map)ReflectUtil.field( rootModule, "visiblePackages" ).get()).put( ((Symbol.PackageSymbol)pkg).fullname, pkg ); Set readModules = (Set)ReflectUtil.field( moduleToOpen, "readModules" ).get(); readModules.add( rootModule ); ReflectUtil.field( moduleToOpen, "readModules" ).set( readModules );
ReflectUtil.method( ReflectUtil.type( "com.sun.tools.javac.comp.Modules" ), "instance", Context.class ).invokeStatic( context ), "allModules" ).get(); List<Object> requires = (List<Object>)ReflectUtil.field( rootModule, "requires" ).get(); List<Object> newRequires = new ArrayList( requires ); Object addedRequires = ReflectUtil.constructor( "com.sun.tools.javac.code.Directive$RequiresDirective", ReflectUtil.type( "com.sun.tools.javac.code.Symbol$ModuleSymbol" ) ).newInstance( moduleToOpen ); ReflectUtil.field( rootModule, "requires" ).set( requires ); List<Object> exports = new ArrayList<>( (Collection)ReflectUtil.field( moduleToOpen, "exports" ).get() ); for( Symbol pkg : (Iterable<Symbol>)ReflectUtil.field( moduleToOpen, "enclosedPackages" ).get() ) exports.add( exp ); ((Map)ReflectUtil.field( rootModule, "visiblePackages" ).get()).put( ((Symbol.PackageSymbol)pkg).fullname, pkg ); Set readModules = (Set)ReflectUtil.field( moduleToOpen, "readModules" ).get(); readModules.add( rootModule ); ReflectUtil.field( moduleToOpen, "readModules" ).set( readModules );
private void _visitApply( JCTree.JCMethodInvocation tree ) Env<AttrContext> env = (Env<AttrContext>)ReflectUtil.field( this, "env" ).get(); typeargtypes = attribAnyTypes( tree.typeargs, localEnv ); Object resultInfo = ReflectUtil.field( this, "resultInfo" ).get(); Type mpt = newMethodTemplate( (Type)ReflectUtil.field( resultInfo, "pt" ).get(), argtypes, typeargtypes ); ReflectUtil.field( localEnv.info, "pendingResolutionPhase" ).set( null ); Type mtype = (Type)attribTree.invoke( this, tree.meth, localEnv, resultInfoCtor.newInstance( this, kind, mpt, ReflectUtil.field( resultInfo, "checkContext" ).get() ) );
Object classLocation = ReflectUtil.field( m, "classLocation" ).get(); if( classLocation == null )
Symbol module = (Symbol)ReflectUtil.field( compilationUnit, "modle" ).get(); if( module == null )
RelativePath relativePath = (RelativePath)ReflectUtil.field( fileObj, "relativePath" ).get(); return removeExtension( relativePath.getPath() ).replace( File.separatorChar, '.' ).replace( '/', '.' ); FileObject fileObject = (FileObject)ReflectUtil.field( fileObj, "fileObject" ).get(); return fileObject instanceof JavaFileObject ? inferBinaryName( location, (JavaFileObject)fileObject ) : null;
for( Symbol pkg : (Iterable<Symbol>)ReflectUtil.field( ms, "enclosedPackages" ).get() )
private void wrapReaders() { Map/*<ModuleReference, ModuleReader>*/ moduleToReader = (Map)ReflectUtil.field( _loader, "moduleToReader" ).get(); for( Object mr: moduleToReader.keySet() ) { //noinspection unchecked Optional<URI> location = (Optional<URI>)ReflectUtil.method( mr, "location" ).invoke(); URI uri = location.orElse( null ); if( uri == null ) { continue; } //## note: "jmod" files are not supported here because they are currently (2018) supported exclusively at compiler/linker time String scheme = uri.getScheme(); if( scheme.equalsIgnoreCase( "file" ) || scheme.equalsIgnoreCase( "jar" ) ) { Object reader = moduleToReader.get( mr ); Class<?> moduleReaderClass = ReflectUtil.type( "java.lang.module.ModuleReader" ); ManModuleReader wrapper = new ManModuleReader( reader, ReflectUtil.field( _loader, "ucp" ).get() ); Object/*ModuleReader*/ proxy = Proxy.newProxyInstance( moduleReaderClass.getClassLoader(), new Class<?>[]{moduleReaderClass}, new ManModuleReaderInvocationHandler( wrapper ) ); //noinspection unchecked moduleToReader.put( mr, proxy ); } } }
private static LiveMethodRef getURLsMethod( Object receiver ) { LiveMethodRef getURLs = ReflectUtil.WithNull.methodWithReturn( receiver, "getURLs|getUrls", URL[].class ); if( getURLs == null ) { getURLs = ReflectUtil.WithNull.methodWithReturn( receiver, "getURLs|getUrls", List.class ); if( getURLs == null && receiver instanceof ClassLoader ) { ReflectUtil.LiveFieldRef ucpField = ReflectUtil.WithNull.field( receiver, "ucp" ); if( ucpField != null ) { Object ucp = ucpField.get(); if( ucp != null ) { getURLs = getURLsMethod( ucp ); } } } } return getURLs; }
static void addUrlToIntelliJPluginClassLoader( ClassLoader cl, URL url ) { if( cl.getClass().getTypeName().equals( "com.intellij.ide.plugins.cl.PluginClassLoader" ) ) { try { Object classPath = ReflectUtil.method( cl, "getClassPath" ).invoke(); ReflectUtil.field( classPath, "myCanUseCache" ).set( false ); Object urlLoader = makeUrlLoader( cl, url ); ReflectUtil.method( ReflectUtil.field( classPath, "myLoaders" ).get(), "add", Object.class ).invoke( urlLoader ); ReflectUtil.method( ReflectUtil.field( classPath, "myLoadersMap" ).get(), "put", Object.class, Object.class ).invoke( url, urlLoader ); //ReflectUtil.method( classPath, "initLoader", URL.class, ReflectUtil.type( "com.intellij.util.lang.Loader" ) ).invoke( url, urlLoader ); } catch( Exception e ) { //## todo: log error e.printStackTrace(); } } }
public static Log instance( Context ctx ) { Log log = ctx.get( logKey ); if( !(log instanceof ManLog_8) ) { ctx.put( logKey, (Log)null ); log = new ManLog_8( ctx, (DiagnosticHandler)ReflectUtil.field( log, "diagnosticHandler" ).get(), log.currentSource(), (PrintWriter)ReflectUtil.field( log, "errWriter" ).get(), (PrintWriter)ReflectUtil.field( log, "warnWriter" ).get(), (PrintWriter)ReflectUtil.field( log, "noticeWriter" ).get() ); } return log; }
public static Object getField_Object( Object receiver, String name ) { return ReflectUtil.field( receiver, name ).get(); }
private DiagnosticHandler getDiagnosticHandler() { return (DiagnosticHandler)ReflectUtil.field( this, "diagnosticHandler" ).get(); }
public static Object getField_Object( Object receiver, String name ) { return ReflectUtil.field( receiver, name ).get(); }
private static Class defineClass( ClassLoader cl, String fqn ) throws IOException { InputStream stream = cl.getResourceAsStream( fqn ); byte[] bytes = StreamUtil.getContent( stream ); ClassLoader parent = (ClassLoader)Array.get( ReflectUtil.field( cl, "myParents" ).get(), 0 ); return (Class)ReflectUtil.method( parent, "defineClass", byte[].class, int.class, int.class ).invoke( bytes, 0, bytes.length ); }
private Location makeModuleLocation( ManPatchModuleLocation location ) { // Module module = Modules.instance( _ctx ).getObservableModule( Names.instance( _ctx ).fromString( location.getName() ) ); // return module.classLocation; Symbol moduleElement = (Symbol)ReflectUtil.method( Symtab.instance( _ctx ), "getModule", Name.class ) .invoke( Names.instance( _ctx ).fromString( location.getName() ) ); if( moduleElement == null ) { return null; } return (Location)ReflectUtil.field( moduleElement, "classLocation" ).get(); }