<commit_before>package me.coley.recaf.metadata;

import org.objectweb.asm.tree.AnnotationNode;
import org.objectweb.asm.tree.MethodNode;

import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;

/**
 * Comments metadata handler.
 *
 * @author Matt
 */
public class Comments {
	public static final String TYPE = "Lme/coley/recaf/metadata/InsnComment;";
	public static final String KEY_PREFIX = "At_";
	private final Map<Integer, String> indexToComment = new TreeMap<>();

	/**
	 * Create an empty comments handler.
	 */
	public Comments() {
	}

	/**
	 * Create a comments handler that populates existing entries from the given method.
	 *
	 * @param method
	 * 		Method with comments.
	 */
	public Comments(MethodNode method) {
		parse(method);
	}

	private void parse(MethodNode method) {
		if (method.visibleAnnotations == null) return;
		for (AnnotationNode anno : method.visibleAnnotations) {
			if (anno.desc.equals(Comments.TYPE)) {
				for (int i = 0; i < anno.values.size(); i += 2) {
					String key = ((String) anno.values.get(i)).substring(Comments.KEY_PREFIX.length());
					String comment = (String) anno.values.get(i + 1);
					if (key.matches("\\d+"))
						indexToComment.put(Integer.parseInt(key), comment);
				}
			}
		}
	}

	/**
	 * Adds a comment at the current instruction offset.
	 *
	 * @param index
	 * 		Method instruction index to insert the comment at.
	 * @param comment
	 * 		Comment string to add.
	 */
	public void addComment(int index, String comment) {<commit_msg>fix: Assembler failing to disassemble methods due to obfuscator-generated malformed Recaf attributes applied to methods<commit_after>package me.coley.recaf.metadata;

import org.objectweb.asm.tree.AnnotationNode;
import org.objectweb.asm.tree.MethodNode;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * Comments metadata handler.
 *
 * @author Matt
 */
public class Comments {
	public static final String TYPE = "Lme/coley/recaf/metadata/InsnComment;";
	public static final String KEY_PREFIX = "At_";
	private final Map<Integer, String> indexToComment = new TreeMap<>();

	/**
	 * Create an empty comments handler.
	 */
	public Comments() {
	}

	/**
	 * Create a comments handler that populates existing entries from the given method.
	 *
	 * @param method
	 * 		Method with comments.
	 */
	public Comments(MethodNode method) {
		parse(method);
	}

	private void parse(MethodNode method) {
		if (method.visibleAnnotations == null) return;
		List<AnnotationNode> invalidAnnos = new ArrayList<>();
		for (AnnotationNode anno : method.visibleAnnotations) {
			if (anno.desc.equals(Comments.TYPE)) {
				for (int i = 0; i < anno.values.size(); i += 2) {
					Object keyInfo = anno.values.get(i);
					Object comment = anno.values.get(i + 1);
					// Skip malformed comments.
					boolean validTypes = keyInfo instanceof String && comment instanceof String;
					if (!validTypes || keyInfo.toString().length() <= KEY_PREFIX.length()) {
						invalidAnnos.add(anno);
						continue;
					}
					String key = ((String)keyInfo).substring(Comments.KEY_PREFIX.length());
					if (key.matches("\\d+"))
						indexToComment.put(Integer.parseInt(key), (String) comment);
				}
			}
		}
		// Prune invalid annos
		method.visibleAnnotations.removeIf(invalidAnnos::contains);
	}

	/**
	 * Adds a comment at the current instruction offset.
	 *
	 * @param index
	 * 		Method instruction index to insert the comment at.
	 * @param comment
	 * 		Comment string to add.
	 */
	public void addComment(int index, String comment) {
{'fraction_non_alphanumeric': 0.12224392966787608, 'fraction_numerical': 0.0016745743790120011, 'mean_word_length': 3.600770218228498, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5758671', 'n_tokens_mistral': 1214, 'n_tokens_neox': 1068, 'n_words': 307}