Java Code Examples for com.sun.tools.javac.code.Symbol#PackageSymbol

The following examples show how to use com.sun.tools.javac.code.Symbol#PackageSymbol . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavacProcessingEnvironment.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public Set<Symbol.PackageSymbol> getSpecifiedPackages() {
    return specifiedPackages;
}
 
Example 2
Source File: NecessaryEvilUtil.java    From manifold with Apache License 2.0 4 votes vote down vote up
public static void openModule( Context context, String moduleName )
{
  try
  {
    Symbol moduleToOpen = (Symbol)ReflectUtil.method( Symtab.instance( context ), "getModule", Name.class )
      .invoke( Names.instance( context ).fromString( moduleName ) );

    if( moduleToOpen == null )
    {
      // not modular java 9+
      return;
    }

    moduleToOpen.complete();

    Set<Symbol> rootModules = (Set<Symbol>)ReflectUtil.field(
      ReflectUtil.method( ReflectUtil.type( "com.sun.tools.javac.comp.Modules" ), "instance", Context.class ).invokeStatic( context ), "allModules" ).get();

    for( Symbol rootModule: rootModules )
    {
      rootModule.complete();

      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 );
      newRequires.add( addedRequires );
      requires = com.sun.tools.javac.util.List.from( newRequires );
      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() )
      {
        if( pkg instanceof Symbol.PackageSymbol )
        {
          //System.err.println( "PACKAGE: " + pkg );
          Object exp = ReflectUtil.constructor( "com.sun.tools.javac.code.Directive$ExportsDirective", Symbol.PackageSymbol.class, com.sun.tools.javac.util.List.class ).newInstance( pkg,
            com.sun.tools.javac.util.List.of( rootModule ) );
          exports.add( exp );

          ((Map)ReflectUtil.field( rootModule, "visiblePackages" ).get()).put( ((Symbol.PackageSymbol)pkg).fullname, pkg );
        }
      }
      ReflectUtil.field( moduleToOpen, "exports" ).set( com.sun.tools.javac.util.List.from( exports ) );

      Set readModules = (Set)ReflectUtil.field( moduleToOpen, "readModules" ).get();
      readModules.add( rootModule );
      ReflectUtil.field( moduleToOpen, "readModules" ).set( readModules );
    }

  }
  catch( Throwable e )
  {
    System.err.println( "Failed to reflectively add-exports " + moduleName + "/* to root module[s], you must add the following argument to jave.exe:\n" +
                        "  --add-exports=" + moduleName + "/*=<root-module>\n" );
    throw new RuntimeException( e );
  }
}
 
Example 3
Source File: ManPatchLocation.java    From manifold with Apache License 2.0 4 votes vote down vote up
/**
 * Overrides Location#inferModuleName in Java 9+
 *
 * @noinspection WeakerAccess
 */
public String inferModuleName( Context ctx )
{
  Names names = Names.instance( ctx );
  GeneratedJavaStubFileObject fo = _fo;
  String packageName = getPackageName( fo );

  if( ReflectUtil.field(
    ReflectUtil.method( "com.sun.tools.javac.comp.Modules", "instance", Context.class )
      .invokeStatic( ctx ), "allModules" ).get() == null )
  {
    // If using JavaParser.compile(), say from Lab, we don't care about modules, otherwise we run into trouble
    return null;
  }

  JavacElements elementUtils = JavacElements.instance( ctx );
  for( Object /*ModuleElement*/ ms : (Iterable)ReflectUtil.method( elementUtils, "getAllModuleElements" ).invoke() )
  {
    if( (boolean)ReflectUtil.method( ms, "isUnnamed" ).invoke() )
    {
      continue;
    }

    if( ms.getClass().getSimpleName().equals( "ModuleSymbol" ) )
    {
      //noinspection unchecked
      for( Symbol pkg : (Iterable<Symbol>)ReflectUtil.field( ms, "enclosedPackages" ).get() )
      {
        if( !(pkg instanceof Symbol.PackageSymbol) )
        {
          continue;
        }
        if( pkg.toString().equals( packageName ) )
        {
          //noinspection unchecked
          Iterable<Symbol> symbolsByName = (Iterable<Symbol>)ReflectUtil.method( ReflectUtil.method( pkg, "members" ).invoke(), "getSymbolsByName", Name.class ).invoke( names.fromString( getPhysicalClassName( fo ) ) );
          if( symbolsByName.iterator().hasNext() )
          {
            return ReflectUtil.method( ms, "getQualifiedName" ).invoke().toString();
          }
        }
      }
    }
  }
  return null;
}
 
Example 4
Source File: SrcClassUtil.java    From manifold with Apache License 2.0 4 votes vote down vote up
private SrcClass makeSrcClass( String fqn, SrcClass enclosing, Symbol.ClassSymbol classSymbol, CompilationUnitTree compilationUnit, BasicJavacTask javacTask, IModule module, JavaFileManager.Location location, DiagnosticListener<JavaFileObject> errorHandler, boolean withMembers )
  {
    SrcClass srcClass;
    if( enclosing == null )
    {
      srcClass = new SrcClass( fqn, SrcClass.Kind.from( classSymbol.getKind() ), location, module, errorHandler )
        .modifiers( classSymbol.getModifiers() );
    }
    else
    {
      srcClass = new SrcClass( fqn, enclosing, SrcClass.Kind.from( classSymbol.getKind() ) )
        .modifiers( classSymbol.getModifiers() );
    }
    if( classSymbol.getEnclosingElement() instanceof Symbol.PackageSymbol && compilationUnit != null )
    {
      for( ImportTree imp: compilationUnit.getImports() )
      {
        if( imp.isStatic() )
        {
          srcClass.addStaticImport( imp.getQualifiedIdentifier().toString() );
        }
        else
        {
          srcClass.addImport( imp.getQualifiedIdentifier().toString() );
        }
      }
    }
    addAnnotations( srcClass, classSymbol );
    for( Symbol.TypeVariableSymbol typeVar: classSymbol.getTypeParameters() )
    {
      srcClass.addTypeVar( makeTypeVarType( typeVar ) );
    }
    Type superclass = classSymbol.getSuperclass();
    if( !(superclass instanceof NoType) )
    {
      srcClass.superClass( makeNestedType( superclass ) );
    }
    for( Type iface: classSymbol.getInterfaces() )
    {
      srcClass.addInterface( makeNestedType( iface ) );
    }
    if( withMembers )
    {
      java.util.List<Symbol> members = classSymbol.getEnclosedElements();
      for( Symbol sym: members )
      {
// include private members because:
// 1. @Jailbreak can expose private members
// 2. Compiler error messages are better when referencing an inaccessible method vs. a non-existent one
//        long modifiers = SrcAnnotated.modifiersFrom( sym.getModifiers() );
//        if( Modifier.isPrivate( (int)modifiers ) )
//        {
//          continue;
//        }

        if( sym instanceof Symbol.ClassSymbol )
        {
          addInnerClass( module, srcClass, sym, javacTask );
        }
        else if( sym instanceof Symbol.VarSymbol )
        {
          addField( srcClass, sym );
        }
        else if( sym instanceof Symbol.MethodSymbol )
        {
          if( !isEnumMethod( sym ) )
          {
            addMethod( module, srcClass, (Symbol.MethodSymbol)sym, javacTask );
          }
        }
      }

      addDefaultCtorForEnum( classSymbol, srcClass, members );
    }
    return srcClass;
  }