PageRenderTime 18ms CodeModel.GetById 8ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/maven-amps-plugin/src/main/java/com/atlassian/maven/plugins/amps/util/ClassUtils.java

https://bitbucket.org/mmeinhold/amps
Java | 60 lines | 56 code | 4 blank | 0 comment | 1 complexity | 1ae6aa064c0bff0bc7f59ddd32091214 MD5 | raw file
 1package com.atlassian.maven.plugins.amps.util;
 2
 3import java.io.File;
 4import java.io.FileInputStream;
 5import java.io.FileNotFoundException;
 6import java.io.IOException;
 7
 8import org.apache.commons.io.IOUtils;
 9import org.apache.commons.lang.StringUtils;
10import org.apache.commons.io.FileUtils;
11import org.objectweb.asm.ClassReader;
12
13public class ClassUtils
14{
15    public static String getClassnameFromFile(File classFile, String removePrefix)
16    {
17        String regex = "/";
18
19        if(OSUtils.OS.equals(OSUtils.OS.WINDOWS))
20        {
21            regex = "\\\\";
22        }
23        return StringUtils.removeEnd(
24                StringUtils.removeStart(
25                        StringUtils.removeStart(
26                                classFile.getAbsolutePath(), removePrefix)
27                                   .replaceAll(regex, ".")
28                        , ".")
29                , ".class");
30    }
31    
32    public static boolean isWiredPluginTestClass(File classFile)
33    {
34        FileInputStream fis = null;
35        boolean isWiredClass = false;
36        try
37        {
38            TestClassVisitor visitor = new TestClassVisitor();
39            fis = new FileInputStream(classFile);
40            ClassReader reader = new ClassReader(fis);
41            
42            reader.accept(visitor, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
43            isWiredClass = visitor.isWiredTest();
44        }
45        catch (FileNotFoundException e)
46        {
47            isWiredClass = false;
48        }
49        catch (IOException e)
50        {
51            isWiredClass = false;
52        }
53        finally
54        {
55            IOUtils.closeQuietly(fis);
56        }
57        
58        return isWiredClass;
59    }
60}