/** * Searches the class loader of this class for the specified name, if not found searches * the current thread's context class loader. * * @param fqn The qualified name of the type e.g., {@code "java.lang.String"} * * @return The {@code Class} corresponding with {@code fqn} or null if not found */ public static Class<?> type( String fqn ) { try { //openPackage( fqn, null ); return Class.forName( fqn ); } catch( ClassNotFoundException e ) { return type( fqn, Thread.currentThread().getContextClassLoader() ); } }
/** * Get a {@link FieldRef} to the specified field. Typical use: * <p> * <pre> field("java.time.LocalTime", "hour").get(); </pre> * * @param fqn The qualified name of the class having the field * @param name The name of the field or a '|' separated list of names, where the first found is used * * @return A reference to the specified field or null if not found */ public static FieldRef field( String fqn, String name ) { return field( type( fqn ), name ); }
/** * Get a {@link ConstructorRef} to the specified constructor. Typical use: * <p> * <pre> constructor("java.util.ArrayList", int.class).newInstance(32) </pre> * * @param fqn The qualified name of the class to construct * @param params A list of parameter types for the constructor * * @return A reference to the constructor or null if not found */ public static ConstructorRef constructor( String fqn, Class<?>... params ) { return constructor( type( fqn ), params ); }
/** * Get a {@link MethodRef} to the specified method. Typical use: * <p> * <pre> method("java.time.LocalTime", "of", int.class, int.class).invokeStatic(5, 30) </pre> * * @param fqn The qualified name of the class containing the method * @param name The name of the method or a '|' separated list of names, where the first found is used * @param params The types of the method's parameters * * @return A reference to the specified method or null if not found */ public static MethodRef method( String fqn, String name, Class... params ) { return method( type( fqn ), name, params ); }
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 ManLog_8( Context ctx, DiagnosticHandler diagnosticHandler, DiagnosticSource source, PrintWriter errWriter, PrintWriter warnWriter, PrintWriter noticeWriter ) { super( ctx, errWriter, warnWriter, noticeWriter ); ReflectUtil.field( this, "diagnosticHandler" ).set( diagnosticHandler ); ReflectUtil.field( this, "source" ).set( source ); _suspendedIssues = new HashMap<>(); _extensionTransformerClass = LocklessLazyVar.make( () -> ReflectUtil.type( "manifold.ext.ExtensionTransformer" ) ); reassignAllEarlyHolders( ctx ); }
Object modulesUtil = ReflectUtil.method( ReflectUtil.type( "com.sun.tools.javac.comp.Modules" ), "instance", Context.class ).invokeStatic( getContext() );
public static boolean isJava9Modular_compiler( Object/*Context*/ ctx ) { if( _modular == null ) { if( isJava8() ) { _modular = false; } else { //noinspection ConstantConditions Object modulesUtil = ReflectUtil.method( ReflectUtil.type( "com.sun.tools.javac.comp.Modules" ), "instance", ReflectUtil.type( "com.sun.tools.javac.util.Context" ) ).invokeStatic( ctx ); Object defModule = ReflectUtil.method( modulesUtil, "getDefaultModule" ).invoke(); _modular = defModule != null && !(boolean)ReflectUtil.method( defModule, "isNoModule" ).invoke() && !(boolean)ReflectUtil.method( defModule, "isUnnamed" ).invoke(); } } return _modular; }
: ReflectUtil.method( type, "cloneWithMetadata", ReflectUtil.type( "com.sun.tools.javac.code.TypeMetadata" ) ) .invoke( ReflectUtil.field( "com.sun.tools.javac.code.TypeMetadata", "EMPTY" ).getStatic() ).toString(); srcType = new SrcType( unannotatedType );
ReflectUtil.method( ReflectUtil.type( "com.sun.tools.javac.comp.Modules" ), "instance", Context.class ).invokeStatic( context ), "allModules" ).get(); Object addedRequires = ReflectUtil.constructor( "com.sun.tools.javac.code.Directive$RequiresDirective", ReflectUtil.type( "com.sun.tools.javac.code.Symbol$ModuleSymbol" ) ).newInstance( moduleToOpen ); newRequires.add( addedRequires ); requires = com.sun.tools.javac.util.List.from( newRequires );
ReflectUtil.method( ReflectUtil.type( "com.sun.tools.javac.comp.Modules" ), "instance", Context.class ).invokeStatic( context ), "allModules" ).get(); Object addedRequires = ReflectUtil.constructor( "com.sun.tools.javac.code.Directive$RequiresDirective", ReflectUtil.type( "com.sun.tools.javac.code.Symbol$ModuleSymbol" ) ).newInstance( moduleToOpen ); newRequires.add( addedRequires ); requires = com.sun.tools.javac.util.List.from( newRequires );
Class<?> classModule = ReflectUtil.type( "java.lang.Module" ); ReflectUtil.MethodRef addExportsOrOpens = ReflectUtil.method( classModule, "implAddExportsOrOpens", String.class, classModule, boolean.class, boolean.class ); Object /*Module*/ jdkCompilerModule = ReflectUtil.method( Class.class, "getModule" ).invoke( ReflectUtil.type( "com.sun.tools.javac.code.Symbol" ) ); addExportsOrOpens.invoke( jdkCompilerModule, "com.sun.tools.javac.api", manifoldModule, true, true ); addExportsOrOpens.invoke( jdkCompilerModule, "com.sun.tools.javac.code", manifoldModule, true, true ); Object /*Module*/ jdkJavadoc = ReflectUtil.method( Class.class, "getModule" ).invoke( ReflectUtil.type( "jdk.javadoc.internal.doclets.formats.html.HtmlDoclet" ) ); addExportsOrOpens.invoke( jdkJavadoc, "jdk.javadoc.internal.doclets.formats.html", manifoldModule, true, true ); addExportsOrOpens.invoke( jdkJavadoc, "com.sun.tools.doclets.standard", manifoldModule, true, true );
ReflectUtil.method( ReflectUtil.type( "manifold.internal.javac.ManClassFinder_9" ), "instance", Context.class ).invokeStatic( getContext() );
private void reassignEarlyHolders( Context context ) { ReflectUtil.field( _attr, "rs" ).set( this ); ReflectUtil.field( DeferredAttr.instance( context ), "rs" ).set( this ); ReflectUtil.field( Check.instance( context ), "rs" ).set( this ); ReflectUtil.field( Infer.instance( context ), "rs" ).set( this ); ReflectUtil.field( Flow.instance( context ), "rs" ).set( this ); ReflectUtil.field( LambdaToMethod.instance( context ), "rs" ).set( this ); ReflectUtil.field( Lower.instance( context ), "rs" ).set( this ); ReflectUtil.field( Gen.instance( context ), "rs" ).set( this ); ReflectUtil.field( ReflectUtil.method( ReflectUtil.type( "com.sun.tools.javac.jvm.StringConcat" ), "instance", Context.class ) .invokeStatic( context ), "rs" ) .set( this ); ReflectUtil.field( JavacTrees.instance( context ), "resolve" ).set( this ); ReflectUtil.field( Annotate.instance( context ), "resolve" ).set( this ); ReflectUtil.field( TransTypes.instance( context ), "resolve" ).set( this ); ReflectUtil.field( JavacElements.instance( context ), "resolve" ).set( this ); }