SymbolTable libSymbols = SymbolIo.read( rFile ); libSymbols = libSymbols.rename( packageName, libSymbols.getTableName() ); symbolTables.add( libSymbols ); final SymbolTable mainSymbols = SymbolIo.read( projectR ); RGeneration.generateRForLibraries( mainSymbols, symbolTables, genDirectory.getAbsoluteFile(), false );
/** * Obtains a unique key for a symbol. * * @param symbol the symbol * @return the unique ID */ @NonNull private static String key(@NonNull Symbol symbol) { return key(symbol.getResourceType(), symbol.getName()); }
/** * Merges a list of tables into a single table. The merge is order-sensitive: when multiple * symbols with the same class / name exist in multiple tables, the first one will be used. * * @param tables the tables to merge * @return the table with the result of the merge; this table will have the package / name of * the first table in {@code tables}, or the default one if there are no tables in * {@code tables} */ @NonNull public static SymbolTable merge(@NonNull List<SymbolTable> tables) { SymbolTable.Builder builder = SymbolTable.builder(); boolean first = true; for (SymbolTable t : tables) { if (first) { builder.tablePackage(t.getTablePackage()); builder.tableName(t.getTableName()); first = false; } for (Symbol s : t.allSymbols()) { if (!builder.contains(s)) { builder.add(s); } } } return builder.build(); }
mainRTxt.isFile()? SymbolIo.read(mainRTxt) : SymbolTable.builder().build(); mainSymbols = mainSymbols.rename(mainPackageName, mainSymbols.getTableName()); rFile.exists()? SymbolIo.read(rFile) : SymbolTable.builder().build(); depSymbols = depSymbols.rename(depPackageName, depSymbols.getTableName()); depSymbolTables.add(depSymbols); RGeneration.generateRForLibraries(mainSymbols, depSymbolTables, sourceOut, finalIds);
Pair<String, String> mainP = Pair.of(main.getTablePackage(), main.getTableName()); Map<Pair<String, String>, SymbolTable> toWrite = new HashMap<>(); for (SymbolTable st : libraries) { Pair<String, String> p = Pair.of(st.getTablePackage(), st.getTableName()); if (p.equals(mainP)) { continue; toWrite.put(p, existing.merge(st)); } else { toWrite.put(p, st); st = main.filter(st).rename(st.getTablePackage(), st.getTableName()); toWrite.put(k, st); toWrite.values().forEach(st -> SymbolIo.exportToJava(st, out, finalIds));
Iterable<String> directories = splitter.split(table.getTablePackage()); File file = directory; for (String d : directories) { file = new File(file, table.getTableName() + SdkConstants.DOT_JAVA); table.allSymbols().forEach(s -> resourceTypes.add(s.getResourceType())); pw.println(" */"); if (!table.getTablePackage().isEmpty()) { pw.println("package " + table.getTablePackage() + ";"); pw.println("public final class " + table.getTableName() + " {"); table.allSymbols().forEach(sym -> { if (sym.getResourceType().equals(rt)) { syms.add(sym); + idModifiers + " " + s.getJavaType() + " " + s.getName() + " = " + s.getValue() + ";");
/** * Builds a new symbol table that has the same symbols as this one, but was renamed with * the given package and table name. * * @param tablePackage the table package * @param tableName the table name * @return the new renamed symbol table */ @NonNull public SymbolTable rename(@NonNull String tablePackage, @NonNull String tableName) { return builder() .tablePackage(tablePackage) .tableName(tableName) .addAll(allSymbols()) .build(); }
/** * Writes a symbol table to a symbol file. * * @param table the table * @param file the file where the table should be written * @throws UncheckedIOException I/O error */ public static void write(@NonNull SymbolTable table, @NonNull File file) { List<String> lines = new ArrayList<>(); for (Symbol s : table.allSymbols()) { lines.add( s.getJavaType() + " " + s.getResourceType() + " " + s.getName() + " " + s.getValue()); } try ( FileOutputStream fos = new FileOutputStream(file); PrintWriter pw = new PrintWriter(fos)) { lines.forEach(pw::println); } catch (IOException e) { throw new UncheckedIOException(e); } }
/** * Produces a subset of this symbol table that has the symbols with resource type / name defined * in {@code filter}. In other words, a symbol {@code s} will exist in the result if and only * if {@code s} exists in {@code this} and there is a symbol {@code s1} in {@code table} * such that {@code s.resourceType == s1.resourceType && s.name == s1.name}. * * @param table the filter table * @return the filter result; this table will have the same name and package as this one */ @NonNull public SymbolTable filter(@NonNull SymbolTable table) { SymbolTable.Builder stb = builder(); stb.tableName(tableName); stb.tablePackage(tablePackage); for (Map.Entry<String, Symbol> e : symbols.entrySet()) { if (table.symbols.containsKey(e.getKey())) { stb.symbols.put(e.getKey(), e.getValue()); } } return stb.build(); }
/** * Adds a symbol to the table to be built. The table must not contains a symbol with the * same resource type and name. * * @param symbol the symbol to add * @return {@code this} for use with fluent-style notation */ public Builder add(@NonNull Symbol symbol) { String key = key(symbol); if (symbols.containsKey(key)) { throw new IllegalArgumentException( "Duplicate symbol in table with resource " + "type '" + symbol.getResourceType() + "' and symbol name '" + symbol.getName() + "'"); } symbols.put(key, symbol); return this; }
/** * Builds a symbol table with all symbols added. * * @return the symbol table */ @NonNull public SymbolTable build() { return new SymbolTable(tablePackage, tableName, symbols); } }
/** * Creates a new builder to create a {@link SymbolTable}. * * @return a builder */ @NonNull public static Builder builder() { return new Builder(); }
/** * Checks if the table contains a symbol with the given resource type / name. * * @param resourceType the resource type * @param name the name * @return does the table contain a symbol with the given resource type / name? */ public boolean contains(@NonNull String resourceType, @NonNull String name) { return symbols.containsKey(key(resourceType, name)); }
/** * Short for merging {@code this} and {@code m}. * * @param m the table to add to {@code this} * @return the result of merging {@code this} with {@code m} */ @NonNull public SymbolTable merge(@NonNull SymbolTable m) { return merge(Arrays.asList(this, m)); }