Home | History | Annotate | Download | only in proguard
      1 /*
      2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
      3  *             of Java bytecode.
      4  *
      5  * Copyright (c) 2002-2009 Eric Lafortune (eric (at) graphics.cornell.edu)
      6  *
      7  * This program is free software; you can redistribute it and/or modify it
      8  * under the terms of the GNU General Public License as published by the Free
      9  * Software Foundation; either version 2 of the License, or (at your option)
     10  * any later version.
     11  *
     12  * This program is distributed in the hope that it will be useful, but WITHOUT
     13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
     15  * more details.
     16  *
     17  * You should have received a copy of the GNU General Public License along
     18  * with this program; if not, write to the Free Software Foundation, Inc.,
     19  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
     20  */
     21 package proguard;
     22 
     23 import proguard.classfile.ClassPool;
     24 import proguard.classfile.util.ClassUtil;
     25 import proguard.classfile.visitor.ClassVersionSetter;
     26 
     27 import java.io.IOException;
     28 import java.util.*;
     29 
     30 /**
     31  * This class sets the target version on program classes.
     32  *
     33  * @author Eric Lafortune
     34  */
     35 public class Targeter
     36 {
     37     private final Configuration configuration;
     38 
     39 
     40     /**
     41      * Creates a new Targeter to set the target version on program classes
     42      * according to the given configuration.
     43      */
     44     public Targeter(Configuration configuration)
     45     {
     46         this.configuration = configuration;
     47     }
     48 
     49 
     50     /**
     51      * Sets the target version on classes in the given program class pool.
     52      */
     53     public void execute(ClassPool programClassPool) throws IOException
     54     {
     55         Set newerClassVersions = configuration.warn != null ? null : new HashSet();
     56 
     57         programClassPool.classesAccept(new ClassVersionSetter(configuration.targetClassVersion,
     58                                                               newerClassVersions));
     59 
     60         if (newerClassVersions != null &&
     61             newerClassVersions.size() > 0)
     62         {
     63             System.err.print("Warning: some classes have more recent versions (");
     64 
     65             Iterator iterator = newerClassVersions.iterator();
     66             while (iterator.hasNext())
     67             {
     68                 Integer classVersion = (Integer)iterator.next();
     69                 System.err.print(ClassUtil.externalClassVersion(classVersion.intValue()));
     70 
     71                 if (iterator.hasNext())
     72                 {
     73                     System.err.print(",");
     74                 }
     75             }
     76 
     77             System.err.println(")");
     78             System.err.println("         than the target version ("+ClassUtil.externalClassVersion(configuration.targetClassVersion)+").");
     79 
     80             if (!configuration.ignoreWarnings)
     81             {
     82                 System.err.println("         If you are sure this is not a problem,");
     83                 System.err.println("         you could try your luck using the '-ignorewarnings' option.");
     84                 throw new IOException("Please correct the above warnings first.");
     85             }
     86         }
     87     }
     88 }
     89