001    /*
002    w * Sonar, open source software quality management tool.
003     * Copyright (C) 2009 SonarSource SA
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * Sonar is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.squid.bytecode.asm;
021    
022    import org.objectweb.asm.Opcodes;
023    import org.objectweb.asm.Type;
024    import org.objectweb.asm.signature.SignatureReader;
025    
026    public class AsmField extends AsmUnit {
027    
028      private String   name;
029      private AsmType  type;
030      private String[] classes;
031    
032      public AsmField(int access, String name, String descriptor, String descriptorWithGenerics) {
033        super(access);
034        this.access = access;
035        this.name = name;
036        this.type = new AsmType(Type.getType(descriptor));
037        if (descriptorWithGenerics == null) {
038          extractClasses(descriptor);
039        } else {
040          extractClasses(descriptorWithGenerics);
041        }
042      }
043    
044      @Override
045      public boolean isStatic() {
046        return (access & Opcodes.ACC_STATIC) != 0;
047      }
048    
049      public String getName() {
050        return name;
051      }
052    
053      public AsmType getType() {
054        return type;
055      }
056    
057      public String getPath() {
058        return type.getPath();
059      }
060    
061      private void extractClasses(String signature) {
062        if (signature != null) {
063          AsmSignatureVisitor signatureVisitor = new AsmSignatureVisitor();
064          new SignatureReader(signature).acceptType(signatureVisitor);
065          classes = signatureVisitor.getInternalNames().toArray(new String[0]);
066        }
067      }
068    
069      public String[] getClasses() {
070        return classes;
071      }
072    
073      public boolean isArray() {
074        return type.isArray();
075      }
076    
077      public boolean isObject() {
078        return type.isObject();
079      }
080    
081      public boolean isPrimiteType() {
082        return type.isPrimiteType();
083      }
084    }