001/*******************************************************************************
002 * Copyright (C) 2009-2011 FuseSource Corp.
003 * Copyright (c) 2004, 2007 IBM Corporation and others.
004 *
005 * All rights reserved. This program and the accompanying materials
006 * are made available under the terms of the Eclipse Public License v1.0
007 * which accompanies this distribution, and is available at
008 * http://www.eclipse.org/legal/epl-v10.html
009 *
010 *******************************************************************************/
011package org.fusesource.hawtjni.generator;
012
013import java.util.*;
014import java.io.File;
015import java.lang.reflect.*;
016
017import org.fusesource.hawtjni.generator.model.JNIClass;
018import org.fusesource.hawtjni.generator.model.JNIMethod;
019import org.fusesource.hawtjni.generator.model.ReflectClass;
020
021/**
022 * 
023 * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
024 */
025public class CleanupNatives extends CleanupClass {
026
027    public CleanupNatives() {
028    }
029
030    public void generate(JNIClass clazz) {
031        unusedCount = usedCount = 0;
032        super.generate(clazz);
033        List<JNIMethod> methods = clazz.getDeclaredMethods();
034        generate(methods);
035        output("used=" + usedCount + " unused=" + unusedCount + " total=" + (unusedCount + usedCount));
036    }
037
038    public void generate(List<JNIMethod> methods) {
039        sortMethods(methods);
040        for (JNIMethod method : methods) {
041            if ((method.getModifiers() & Modifier.NATIVE) == 0)
042                continue;
043            generate(method);
044        }
045    }
046
047    public void generate(JNIMethod method) {
048        String name = method.getName();
049        Set<File> keys = files.keySet();
050        for (File key : keys) {
051            String str = (String) files.get(key);
052            if (str.indexOf(name) != -1) {
053                // int modifiers = method.getModifiers();
054                // Class clazz = method.getDeclaringClass();
055                // String modifiersStr = Modifier.toString(modifiers);
056                // output(modifiersStr);
057                // if (modifiersStr.length() > 0) output(" ");
058                // output(getTypeSignature3(method.getReturnType()));
059                // output(" " );
060                // output(method.getName());
061                // output("(");
062                // Class[] paramTypes = method.getParameterTypes();
063                // String[] paramNames = getArgNames(method);
064                // for (int i = 0; i < paramTypes.length; i++) {
065                // Class paramType = paramTypes[i];
066                // if (i != 0) output(", ");
067                // String sig = getTypeSignature3(paramType);
068                // if (clazz.getPackage().equals(paramType.getPackage())) sig =
069                // getClassName(paramType);
070                // output(sig);
071                // output(" ");
072                // output(paramNames[i]);
073                // }
074                // outputln(");");
075                usedCount++;
076                return;
077            }
078        }
079        unusedCount++;
080        output("NOT USED=" + method.toString() + "\n");
081    }
082
083    public static void main(String[] args) {
084        if (args.length < 2) {
085            System.out.println("Usage: java CleanupNatives <OS className> <OS class source> <src path0> <src path1>");
086            return;
087        }
088        try {
089            CleanupNatives gen = new CleanupNatives();
090            String clazzName = args[0];
091            String classSource = args[1];
092            String[] sourcePath = new String[args.length - 2];
093            System.arraycopy(args, 2, sourcePath, 0, sourcePath.length);
094            Class<?> clazz = Class.forName(clazzName);
095            gen.setSourcePath(sourcePath);
096            gen.setClassSourcePath(classSource);
097            gen.generate(new ReflectClass(clazz));
098        } catch (Exception e) {
099            System.out.println("Problem");
100            e.printStackTrace(System.out);
101        }
102    }
103
104}