List of usage examples for com.google.common.collect ImmutableBiMap builder
public static <K, V> Builder<K, V> builder()
From source file:com.google.devtools.build.lib.rules.java.JavaImport.java
@Override public ConfiguredTarget create(RuleContext ruleContext) throws InterruptedException, RuleErrorException { ImmutableList<Artifact> srcJars = ImmutableList.of(); ImmutableList<Artifact> jars = collectJars(ruleContext); Artifact srcJar = ruleContext.getPrerequisiteArtifact("srcjar", Mode.TARGET); if (ruleContext.hasErrors()) { return null; }//from ww w .j a va2 s.c o m ImmutableList<TransitiveInfoCollection> targets = ImmutableList.<TransitiveInfoCollection>builder() .addAll(ruleContext.getPrerequisites("deps", Mode.TARGET)) .addAll(ruleContext.getPrerequisites("exports", Mode.TARGET)).build(); final JavaCommon common = new JavaCommon(ruleContext, semantics, /*srcs=*/ImmutableList.<Artifact>of(), targets, targets, targets); semantics.checkRule(ruleContext, common); // No need for javac options - no compilation happening here. ImmutableBiMap.Builder<Artifact, Artifact> compilationToRuntimeJarMapBuilder = ImmutableBiMap.builder(); ImmutableList<Artifact> interfaceJars = processWithIjar(jars, ruleContext, compilationToRuntimeJarMapBuilder); JavaCompilationArtifacts javaArtifacts = collectJavaArtifacts(jars, interfaceJars); common.setJavaCompilationArtifacts(javaArtifacts); CppCompilationContext transitiveCppDeps = common.collectTransitiveCppDeps(); NestedSet<LinkerInput> transitiveJavaNativeLibraries = common.collectTransitiveJavaNativeLibraries(); boolean neverLink = JavaCommon.isNeverLink(ruleContext); JavaCompilationArgs javaCompilationArgs = common.collectJavaCompilationArgs(false, neverLink, false); JavaCompilationArgs recursiveJavaCompilationArgs = common.collectJavaCompilationArgs(true, neverLink, false); NestedSet<Artifact> transitiveJavaSourceJars = collectTransitiveJavaSourceJars(ruleContext, srcJar); if (srcJar != null) { srcJars = ImmutableList.of(srcJar); } // The "neverlink" attribute is transitive, so if it is enabled, we don't add any // runfiles from this target or its dependencies. Runfiles runfiles = neverLink ? Runfiles.EMPTY : new Runfiles.Builder(ruleContext.getWorkspaceName(), ruleContext.getConfiguration().legacyExternalRunfiles()) // add the jars to the runfiles .addArtifacts(javaArtifacts.getRuntimeJars()) .addTargets(targets, RunfilesProvider.DEFAULT_RUNFILES) .addRunfiles(ruleContext, RunfilesProvider.DEFAULT_RUNFILES) .addTargets(targets, JavaRunfilesProvider.TO_RUNFILES) .add(ruleContext, JavaRunfilesProvider.TO_RUNFILES).build(); CcLinkParamsStore ccLinkParamsStore = new CcLinkParamsStore() { @Override protected void collect(CcLinkParams.Builder builder, boolean linkingStatically, boolean linkShared) { builder.addTransitiveTargets(common.targetsTreatedAsDeps(ClasspathType.BOTH), JavaCcLinkParamsProvider.TO_LINK_PARAMS, CcLinkParamsProvider.TO_LINK_PARAMS); } }; RuleConfiguredTargetBuilder ruleBuilder = new RuleConfiguredTargetBuilder(ruleContext); NestedSetBuilder<Artifact> filesBuilder = NestedSetBuilder.stableOrder(); filesBuilder.addAll(jars); ImmutableBiMap<Artifact, Artifact> compilationToRuntimeJarMap = compilationToRuntimeJarMapBuilder.build(); semantics.addProviders(ruleContext, common, ImmutableList.<String>of(), null /* classJar */, srcJar /* srcJar */, null /* genJar */, null /* gensrcJar */, compilationToRuntimeJarMap, filesBuilder, ruleBuilder); NestedSet<Artifact> filesToBuild = filesBuilder.build(); JavaSourceInfoProvider javaSourceInfoProvider = new JavaSourceInfoProvider.Builder().setJarFiles(jars) .setSourceJarsForJarFiles(srcJars).build(); JavaRuleOutputJarsProvider.Builder ruleOutputJarsProviderBuilder = JavaRuleOutputJarsProvider.builder(); for (Artifact jar : jars) { ruleOutputJarsProviderBuilder.addOutputJar(jar, compilationToRuntimeJarMap.inverse().get(jar), srcJar); } NestedSet<Artifact> proguardSpecs = new ProguardLibrary(ruleContext).collectProguardSpecs(); JavaRuleOutputJarsProvider ruleOutputJarsProvider = ruleOutputJarsProviderBuilder.build(); JavaSourceJarsProvider sourceJarsProvider = JavaSourceJarsProvider.create(transitiveJavaSourceJars, srcJars); JavaCompilationArgsProvider compilationArgsProvider = JavaCompilationArgsProvider .create(javaCompilationArgs, recursiveJavaCompilationArgs); JavaSkylarkApiProvider.Builder skylarkApiProvider = JavaSkylarkApiProvider.builder() .setRuleOutputJarsProvider(ruleOutputJarsProvider).setSourceJarsProvider(sourceJarsProvider) .setCompilationArgsProvider(compilationArgsProvider); common.addTransitiveInfoProviders(ruleBuilder, skylarkApiProvider, filesToBuild, null); return ruleBuilder.setFilesToBuild(filesToBuild) .addSkylarkTransitiveInfo(JavaSkylarkApiProvider.NAME, skylarkApiProvider.build()) .add(JavaRuleOutputJarsProvider.class, ruleOutputJarsProvider) .add(JavaRuntimeJarProvider.class, new JavaRuntimeJarProvider(javaArtifacts.getRuntimeJars())) .add(JavaNeverlinkInfoProvider.class, new JavaNeverlinkInfoProvider(neverLink)) .add(RunfilesProvider.class, RunfilesProvider.simple(runfiles)) .add(CcLinkParamsProvider.class, new CcLinkParamsProvider(ccLinkParamsStore)) .add(JavaCompilationArgsProvider.class, compilationArgsProvider) .add(JavaNativeLibraryProvider.class, new JavaNativeLibraryProvider(transitiveJavaNativeLibraries)) .add(CppCompilationContext.class, transitiveCppDeps) .add(JavaSourceInfoProvider.class, javaSourceInfoProvider) .add(JavaSourceJarsProvider.class, sourceJarsProvider) .add(ProguardSpecProvider.class, new ProguardSpecProvider(proguardSpecs)) .addOutputGroup(JavaSemantics.SOURCE_JARS_OUTPUT_GROUP, transitiveJavaSourceJars) .addOutputGroup(OutputGroupProvider.HIDDEN_TOP_LEVEL, proguardSpecs).build(); }
From source file:net.minecrell.ice.launch.transformers.DeobfuscationTransformer.java
public DeobfuscationTransformer() throws Exception { Path path = (Path) Launch.blackboard.get("ice.deobf-srg"); String name = path.getFileName().toString(); boolean gzip = name.endsWith(".gz"); ImmutableBiMap.Builder<String, String> classes = ImmutableBiMap.builder(); ImmutableTable.Builder<String, String, String> fields = ImmutableTable.builder(); ImmutableTable.Builder<String, String, String> methods = ImmutableTable.builder(); try (BufferedReader reader = new BufferedReader(new InputStreamReader( gzip ? new GZIPInputStream(Files.newInputStream(path)) : Files.newInputStream(path), StandardCharsets.UTF_8))) { String line;/*from w w w . ja va2 s . c om*/ while ((line = reader.readLine()) != null) { if ((line = line.trim()).isEmpty()) continue; String[] parts = StringUtils.split(line, ' '); if (parts.length < 3) { System.out.println("Invalid line: " + line); continue; } MappingType type = MappingType.of(parts[0]); if (type == null) { System.out.println("Invalid mapping: " + line); continue; } String[] source, dest; switch (type) { case CLASS: classes.put(parts[1], parts[2]); break; case FIELD: source = getSignature(parts[1]); dest = getSignature(parts[2]); String fieldType = getFieldType(source[0], source[1]); fields.put(source[0], source[1] + ':' + fieldType, dest[1]); if (fieldType != null) fields.put(source[0], source[1] + ":null", dest[1]); break; case METHOD: source = getSignature(parts[1]); dest = getSignature(parts[3]); methods.put(source[0], source[1] + parts[2], dest[1]); break; } } } this.classes = classes.build(); this.rawFields = fields.build(); this.rawMethods = methods.build(); this.fields = Maps.newHashMapWithExpectedSize(rawFields.size()); this.methods = Maps.newHashMapWithExpectedSize(rawMethods.size()); }
From source file:org.neptunepowered.vanilla.launch.transformers.DeobfuscationTransformer.java
public DeobfuscationTransformer() throws Exception { URL mappings = (URL) Launch.blackboard.get("vanilla.mappings"); final ImmutableBiMap.Builder<String, String> classes = ImmutableBiMap.builder(); final ImmutableTable.Builder<String, String, String> fields = ImmutableTable.builder(); final ImmutableTable.Builder<String, String, String> methods = ImmutableTable.builder(); readLines(mappings, Charsets.UTF_8, new LineProcessor<Void>() { @Override//from w w w .ja v a 2s. com public boolean processLine(String line) throws IOException { if ((line = line.trim()).isEmpty()) { return true; } String[] parts = StringUtils.split(line, ' '); if (parts.length < 3) { NeptuneServerTweaker.getLogger().warn("Invalid deobfuscation mapping line: {}", line); return true; } MappingType type = MappingType.of(parts[0]); if (type == null) { NeptuneServerTweaker.getLogger().warn("Invalid deobfuscation mapping type: {}", line); return true; } String[] source; String[] dest; switch (type) { case CLASS: classes.put(parts[1], parts[2]); break; case FIELD: source = getSignature(parts[1]); dest = getSignature(parts[2]); String fieldType = getFieldType(source[0], source[1]); fields.put(source[0], source[1] + ':' + fieldType, dest[1]); if (fieldType != null) { fields.put(source[0], source[1] + ":null", dest[1]); } break; case METHOD: source = getSignature(parts[1]); dest = getSignature(parts[3]); methods.put(source[0], source[1] + parts[2], dest[1]); break; default: } return true; } @Override public Void getResult() { return null; } }); this.classes = classes.build(); this.rawFields = fields.build(); this.rawMethods = methods.build(); this.fields = Maps.newHashMapWithExpectedSize(this.rawFields.size()); this.methods = Maps.newHashMapWithExpectedSize(this.rawMethods.size()); }
From source file:org.spongepowered.server.launch.transformer.DeobfuscationTransformer.java
public DeobfuscationTransformer() throws Exception { URL mappings = (URL) Launch.blackboard.get("vanilla.mappings"); final ImmutableBiMap.Builder<String, String> classes = ImmutableBiMap.builder(); final ImmutableTable.Builder<String, String, String> fields = ImmutableTable.builder(); final ImmutableTable.Builder<String, String, String> methods = ImmutableTable.builder(); readLines(mappings, Charsets.UTF_8, new LineProcessor<Void>() { @Override/* www . ja v a 2 s. co m*/ public boolean processLine(String line) throws IOException { if ((line = line.trim()).isEmpty()) { return true; } String[] parts = StringUtils.split(line, ' '); if (parts.length < 3) { VanillaServerTweaker.getLogger().warn("Invalid deobfuscation mapping line: {}", line); return true; } MappingType type = MappingType.of(parts[0]); if (type == null) { VanillaServerTweaker.getLogger().warn("Invalid deobfuscation mapping type: {}", line); return true; } String[] source; String[] dest; switch (type) { case CLASS: classes.put(parts[1], parts[2]); break; case FIELD: source = getSignature(parts[1]); dest = getSignature(parts[2]); String fieldType = getFieldType(source[0], source[1]); fields.put(source[0], source[1] + ':' + fieldType, dest[1]); if (fieldType != null) { fields.put(source[0], source[1] + ":null", dest[1]); } break; case METHOD: source = getSignature(parts[1]); dest = getSignature(parts[3]); methods.put(source[0], source[1] + parts[2], dest[1]); break; default: } return true; } @Override public Void getResult() { return null; } }); this.classes = classes.build(); this.rawFields = fields.build(); this.rawMethods = methods.build(); this.fields = Maps.newHashMapWithExpectedSize(this.rawFields.size()); this.methods = Maps.newHashMapWithExpectedSize(this.rawMethods.size()); }
From source file:org.neptunepowered.vanilla.launch.transformer.DeobfuscationTransformer.java
public DeobfuscationTransformer() throws Exception { URL mappings = (URL) Launch.blackboard.get("vanilla.mappings"); final ImmutableBiMap.Builder<String, String> classes = ImmutableBiMap.builder(); final ImmutableTable.Builder<String, String, String> fields = ImmutableTable.builder(); final ImmutableTable.Builder<String, String, String> methods = ImmutableTable.builder(); readLines(mappings, Charsets.UTF_8, new LineProcessor<Void>() { @Override/*from ww w . j av a 2s . co m*/ public boolean processLine(String line) throws IOException { if ((line = line.trim()).isEmpty()) { return true; } String[] parts = StringUtils.split(line, ' '); if (parts.length < 3) { NeptuneServerTweaker.log.warn("Invalid deobfuscation mapping line: {}", line); return true; } MappingType type = MappingType.of(parts[0]); if (type == null) { NeptuneServerTweaker.log.warn("Invalid deobfuscation mapping type: {}", line); return true; } String[] source; String[] dest; switch (type) { case CLASS: classes.put(parts[1], parts[2]); break; case FIELD: source = getSignature(parts[1]); dest = getSignature(parts[2]); String fieldType = getFieldType(source[0], source[1]); fields.put(source[0], source[1] + ':' + fieldType, dest[1]); if (fieldType != null) { fields.put(source[0], source[1] + ":null", dest[1]); } break; case METHOD: source = getSignature(parts[1]); dest = getSignature(parts[3]); methods.put(source[0], source[1] + parts[2], dest[1]); break; default: } return true; } @Override public Void getResult() { return null; } }); this.classes = classes.build(); this.rawFields = fields.build(); this.rawMethods = methods.build(); this.fields = Maps.newHashMapWithExpectedSize(this.rawFields.size()); this.methods = Maps.newHashMapWithExpectedSize(this.rawMethods.size()); }
From source file:org.spongepowered.granite.launch.transformers.DeobfuscationTransformer.java
public DeobfuscationTransformer() throws Exception { Path path = (Path) Launch.blackboard.get("granite.deobf-srg"); String name = path.getFileName().toString(); boolean gzip = name.endsWith(".gz"); ImmutableBiMap.Builder<String, String> classes = ImmutableBiMap.builder(); ImmutableTable.Builder<String, String, String> fields = ImmutableTable.builder(); ImmutableTable.Builder<String, String, String> methods = ImmutableTable.builder(); try (BufferedReader reader = new BufferedReader(new InputStreamReader( gzip ? new GZIPInputStream(Files.newInputStream(path)) : Files.newInputStream(path), StandardCharsets.UTF_8))) { String line;//from ww w . j ava 2s . c o m while ((line = reader.readLine()) != null) { if ((line = line.trim()).isEmpty()) { continue; } String[] parts = StringUtils.split(line, ' '); if (parts.length < 3) { System.out.println("Invalid line: " + line); continue; } MappingType type = MappingType.of(parts[0]); if (type == null) { System.out.println("Invalid mapping: " + line); continue; } String[] source; String[] dest; switch (type) { case CLASS: classes.put(parts[1], parts[2]); break; case FIELD: source = getSignature(parts[1]); dest = getSignature(parts[2]); String fieldType = getFieldType(source[0], source[1]); fields.put(source[0], source[1] + ':' + fieldType, dest[1]); if (fieldType != null) { fields.put(source[0], source[1] + ":null", dest[1]); } break; case METHOD: source = getSignature(parts[1]); dest = getSignature(parts[3]); methods.put(source[0], source[1] + parts[2], dest[1]); break; default: } } } this.classes = classes.build(); this.rawFields = fields.build(); this.rawMethods = methods.build(); this.fields = Maps.newHashMapWithExpectedSize(this.rawFields.size()); this.methods = Maps.newHashMapWithExpectedSize(this.rawMethods.size()); }
From source file:net.minecrell.quartz.launch.mappings.Mappings.java
protected Mappings(List<ClassNode> mappingClasses) { requireNonNull(mappingClasses, "mappingClasses"); if (mappingClasses.isEmpty()) { this.classes = ImmutableBiMap.of(); this.methods = ImmutableTable.of(); this.fields = ImmutableTable.of(); this.constructors = ImmutableMultimap.of(); this.accessMappings = ImmutableTable.of(); return;// w w w . ja va 2 s .c o m } ImmutableBiMap.Builder<String, String> classes = ImmutableBiMap.builder(); for (ClassNode classNode : mappingClasses) { ParsedAnnotation annotation = MappingsParser.getAnnotation(classNode, MAPPING); checkState(annotation != null, "Class %s is missing the @Mapping annotation", classNode.name); String mapping = annotation.getString("value", ""); if (!mapping.isEmpty()) { classes.put(mapping, classNode.name); } } this.classes = classes.build(); // We need to remap the descriptors of the fields and methods, use ASM for convenience Remapper remapper = new Remapper() { @Override public String map(String className) { return unmap(className); } }; // Load field, method and access mappings ImmutableTable.Builder<String, String, String> methods = ImmutableTable.builder(); ImmutableTable.Builder<String, String, String> fields = ImmutableTable.builder(); ImmutableMultimap.Builder<String, MethodNode> constructors = ImmutableMultimap.builder(); ImmutableTable.Builder<String, String, AccessMapping> accessMappings = ImmutableTable.builder(); for (ClassNode classNode : mappingClasses) { String className = classNode.name.replace('/', '.'); String internalName = unmap(classNode.name); ParsedAnnotation annotation = MappingsParser.getAnnotation(classNode, ACCESSIBLE); if (annotation != null) { accessMappings.put(className, "", AccessMapping.of(classNode)); } for (MethodNode methodNode : classNode.methods) { Map<String, ParsedAnnotation> annotations = MappingsParser.getAnnotations(methodNode); annotation = annotations.get(CONSTRUCTOR); if (annotation != null) { // Generate constructor call code Methods.visitConstructor(methodNode, classNode.name); constructors.put(className, methodNode); continue; } // TODO: Validation annotation = annotations.get(MAPPING); if (annotation != null) { String mapping = annotation.getString("value", ""); if (!mapping.isEmpty()) { methods.put(internalName, mapping + remapper.mapMethodDesc(methodNode.desc), methodNode.name); } } annotation = annotations.get(ACCESSIBLE); if (annotation != null) { accessMappings.put(className, methodNode.name + methodNode.desc, AccessMapping.of(methodNode)); } } for (FieldNode fieldNode : classNode.fields) { Map<String, ParsedAnnotation> annotations = MappingsParser.getAnnotations(fieldNode); // TODO: Validation annotation = annotations.get(MAPPING); if (annotation != null) { String mapping = annotation.getString("value", ""); if (!mapping.isEmpty()) { fields.put(internalName, mapping + ':' + remapper.mapDesc(fieldNode.desc), fieldNode.name); } } annotation = annotations.get(ACCESSIBLE); if (annotation != null) { accessMappings.put(className, fieldNode.name, AccessMapping.of(fieldNode)); } } } this.methods = methods.build(); this.fields = fields.build(); this.constructors = constructors.build(); this.accessMappings = accessMappings.build(); }
From source file:nallar.tickthreading.patcher.remapping.Deobfuscator.java
public void setup(File mapData) { try {//from ww w. ja v a 2 s .co m mapData = mapData.getCanonicalFile(); //noinspection IOResourceOpenedButNotSafelyClosed ZipFile mapZip = new ZipFile(mapData); ZipEntry classData = mapZip.getEntry("joined.srg"); ZipInputSupplier zis = new ZipInputSupplier(mapZip, classData); InputSupplier<InputStreamReader> srgSupplier = CharStreams.newReaderSupplier(zis, Charsets.UTF_8); List<String> srgList = CharStreams.readLines(srgSupplier); rawMethodMaps = Maps.newHashMap(); rawFieldMaps = Maps.newHashMap(); Builder<String, String> builder = ImmutableBiMap.builder(); Builder<String, String> mcpBuilder = ImmutableBiMap.builder(); Splitter splitter = Splitter.on(CharMatcher.anyOf(": ")).omitEmptyStrings().trimResults(); for (String line : srgList) { String[] parts = Iterables.toArray(splitter.split(line), String.class); String typ = parts[0]; if ("CL".equals(typ)) { parseClass(builder, parts); parseMCPClass(mcpBuilder, parts); } else if ("MD".equals(typ)) { parseMethod(parts); } else if ("FD".equals(typ)) { parseField(parts); } } classNameBiMap = builder.build(); // Special case some mappings for modloader mods mcpBuilder.put("BaseMod", "net/minecraft/src/BaseMod"); mcpBuilder.put("ModLoader", "net/minecraft/src/ModLoader"); mcpBuilder.put("EntityRendererProxy", "net/minecraft/src/EntityRendererProxy"); mcpBuilder.put("MLProp", "net/minecraft/src/MLProp"); mcpBuilder.put("TradeEntry", "net/minecraft/src/TradeEntry"); mcpNameBiMap = mcpBuilder.build(); } catch (IOException ioe) { Log.severe("An error occurred loading the deobfuscation map data", ioe); } methodNameMaps = Maps.newHashMapWithExpectedSize(rawMethodMaps.size()); fieldNameMaps = Maps.newHashMapWithExpectedSize(rawFieldMaps.size()); }
From source file:com.tyler.collectors.GuavaCollectors.java
public static <T, K, V> Collector<T, ?, ImmutableBiMap<K, V>> toImmutableBiMap(final Function<T, K> keyFunction, final Function<T, V> valueFunction) { return Collector.of(ImmutableBiMap::<K, V>builder, (builder, value) -> builder.put(keyFunction.apply(value), valueFunction.apply(value)), (l, r) -> l.putAll(r.build()), ImmutableBiMap.Builder<K, V>::build); }
From source file:net.minecrell.workbench.tools.mapping.BaseMappings.java
@SuppressWarnings("unchecked") private <T extends Name> ImmutableBiMap<T, T> apply(ImmutableBiMap<T, T> mappings, ImmutableMap<String, BaseName> base, Consumer<T> consumer) { ImmutableBiMap.Builder<T, T> builder = ImmutableBiMap.builder(); mappings.forEach((original, mapped) -> { BaseName name = base.get(original.getName()); if (name != null) { T result = (T) original.apply(context, name); if (consumer != null) { consumer.accept(result); }//from w w w . ja v a2 s. c om builder.put(original, result); } }); return builder.build(); }