MethodReuseFinder.java

/*******************************************************************************
 * Copyright (c) 2025 Carsten Hammer.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Carsten Hammer
 *******************************************************************************/
package org.sandbox.jdt.internal.corext.fix.helper;

import java.util.List;

import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.sandbox.jdt.internal.corext.fix.helper.lib.AbstractMethodReuse;
import org.sandbox.jdt.internal.corext.fix.helper.lib.InlineCodeSequenceFinder;
import org.sandbox.jdt.internal.corext.fix.helper.lib.InlineCodeSequenceFinder.InlineSequenceMatch;

/**
 * Method Reuse Finder - Searches for similar methods in the codebase
 * 
 * This class analyzes methods to find potential reuse opportunities
 * by comparing code structures, token sequences, and control flow.
 * It also finds inline code sequences that match method bodies.
 */
public class MethodReuseFinder extends AbstractMethodReuse<ClassInstanceCreation>{
	
	/**
	 * Find similar methods in the project
	 * 
	 * @param method The method to analyze
	 * @return List of similar methods found (placeholder)
	 */
	public static void findSimilarMethods(MethodDeclaration method) {
		// TODO: Implement similarity search
		// Algorithm steps:
		// 1. Extract method signature and body
		// 2. Normalize variable names
		// 3. Generate token sequence
		// 4. Search project for similar token sequences
		// 5. Use AST comparison for structural similarity
		// 6. Calculate similarity score
		// 7. Return methods above threshold
	}
	
	/**
	 * Find inline code sequences that match the given method body
	 * 
	 * @param cu The compilation unit to search
	 * @param method The method whose body to search for
	 * @return List of inline sequence matches
	 */
	public static List<InlineSequenceMatch> findInlineCodeSequences(CompilationUnit cu, MethodDeclaration method) {
		// Use the InlineCodeSequenceFinder to find matching sequences
		return InlineCodeSequenceFinder.findInlineSequences(cu, method);
	}
	
	/**
	 * Compute similarity score between two methods
	 * 
	 * @param method1 First method
	 * @param method2 Second method
	 * @return Similarity score (0.0 to 1.0)
	 */
	public static double computeSimilarity(MethodDeclaration method1, MethodDeclaration method2) {
		// TODO: Implement similarity calculation
		// Factors to consider:
		// - Token-based similarity (Levenshtein distance on token sequences)
		// - AST structural similarity
		// - Control flow similarity
		// - Variable usage patterns
		return 0.0;
	}
	
	/**
	 * Check if a method is a good candidate for refactoring
	 * 
	 * @param method The method to check
	 * @return true if method should be analyzed
	 */
	public static boolean isReusable(MethodDeclaration method) {
		// TODO: Implement candidate filtering
		// Skip methods that are:
		// - Too short (< 3 statements)
		// - Trivial getters/setters
		// - Already extracted utilities
		return method != null && method.getBody() != null;
	}
	
	@Override
	public void find(Object fixcore, org.eclipse.jdt.core.dom.CompilationUnit compilationUnit,
			java.util.Set<?> operations, java.util.Set<org.eclipse.jdt.core.dom.ASTNode> nodesprocessed) throws org.eclipse.core.runtime.CoreException {
		// Default empty implementation - this finder is for discovery, not cleanup operations
	}
	
	@Override
	public String getPreview(boolean afterRefactoring) {
		if (afterRefactoring) {
			return "IStatus status = Status.info(message);\n"; //$NON-NLS-1$
		}
		return "IStatus status = new Status(IStatus.INFO, UIPlugin.PLUGIN_ID, IStatus.OK, message, null));\n"; //$NON-NLS-1$
	}
}