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.java.bytecode.asm;
021    
022    import java.util.Set;
023    
024    import org.objectweb.asm.Opcodes;
025    import org.objectweb.asm.Type;
026    import org.objectweb.asm.signature.SignatureReader;
027    
028    public class AsmField extends AsmUnit {
029    
030      private String name;
031      private AsmType type;
032      private String[] classes;
033    
034      public AsmField(int access, String name, String descriptor, String descriptorWithGenerics) {
035        super(access);
036        this.access = access;
037        this.name = name;
038        this.type = new AsmType(Type.getType(descriptor));
039        if (descriptorWithGenerics == null) {
040          extractClasses(descriptor);
041        } else {
042          extractClasses(descriptorWithGenerics);
043        }
044      }
045    
046      @Override
047      public boolean isStatic() {
048        return (access & Opcodes.ACC_STATIC) != 0;
049      }
050    
051      String getName() {
052        return name;
053      }
054    
055      AsmType getType() {
056        return type;
057      }
058    
059      String getPath() {
060        return type.getPath();
061      }
062    
063      private void extractClasses(String signature) {
064        if (signature != null) {
065          AsmSignatureVisitor signatureVisitor = new AsmSignatureVisitor();
066          new SignatureReader(signature).acceptType(signatureVisitor);
067          Set<String> internalNames = signatureVisitor.getInternalNames();
068          classes = internalNames.toArray(new String[internalNames.size()]);
069        }
070      }
071    
072      String[] getClasses() {
073        return classes;
074      }
075    
076      boolean isArray() {
077        return type.isArray();
078      }
079    
080      boolean isObject() {
081        return type.isObject();
082      }
083    
084      boolean isPrimiteType() {
085        return type.isPrimiteType();
086      }
087    }