All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yworks.yshrink.model.ClassDescriptor Maven / Gradle / Ivy

Go to download

The open-source Java obfuscation tool working with Ant and Gradle by yWorks - the diagramming experts

There is a newer version: 4.1.1
Show newest version
package com.yworks.yshrink.model;

import org.objectweb.asm.Opcodes;
import com.yworks.util.graph.Node;

import java.io.File;
import java.util.*;

/**
 * @author Michael Schroeder, yWorks GmbH http://www.yworks.com
 */
public class ClassDescriptor extends AbstractDescriptor {

  private String name;
  private String superName;
  private String[] interfaces;
  private String enclosingClass;
  private AbstractMap.SimpleEntry enclosingMethod;

  private Map, MethodDescriptor> methods;
  private Map fields;

  private Set allInterfaces;
  private Set allAncestors;

  private Node newNode;

  private boolean hasExternalAncestors = false;

  private Set attributesToKeep = new HashSet();

  protected ClassDescriptor( final String name, final int access, Node newNode, File sourceJar ) {

    super( access, sourceJar );

    this.name = name;
    this.newNode = newNode;
    methods = new HashMap<>();
    fields = new HashMap<>();
  }

  protected ClassDescriptor( final String name, final String superName, final String[] interfaces, final int access, Node newNode, File sourceJar ) {

    this( name, access, newNode, sourceJar );
    this.superName = superName;
    this.interfaces = interfaces;
  }

  public void setEnclosingClass( final String enclosingClass ) {
    this.enclosingClass = enclosingClass;
  }

  public void setEnclosingMethod( final String methodName, final String methodDesc ) {
    this.enclosingMethod = new AbstractMap.SimpleEntry( methodName, methodDesc );
  }

  public String getEnclosingClass() {
    return enclosingClass;
  }

  public AbstractMap.SimpleEntry getEnclosingMethod() {
    return enclosingMethod;
  }

  public void addMethod( final MethodDescriptor method ) {
    methods.put( new AbstractMap.SimpleEntry( method.getName(), method.getDesc() ), method );
  }

  public void addField( final FieldDescriptor field ) {
    fields.put( field.getName(), field );
  }

  public boolean hasExternalAncestors() {
    return hasExternalAncestors;
  }

  public void setHasExternalAncestors( final boolean hasExternalAncestors ) {
    this.hasExternalAncestors = hasExternalAncestors;
  }

  public String getName() {
    return name;
  }

  public String getShortName() {
    final int i = name.lastIndexOf( '/' );
    if ( i != -1 ) {
      return name.substring( i + 1, name.length() );
    } else {
      return name;
    }
  }

  public String getSuperName() {
    return superName;
  }

  public void setSuperName( final String superName ) {
    this.superName = superName;
  }

  public void setInterfaces( final String[] interfaces ) {
    this.interfaces = interfaces;
  }

  public String[] getInterfaces() {
    return interfaces;
  }

  public MethodDescriptor getMethod( final String name, final String desc ) {
    return methods.get( new AbstractMap.SimpleEntry( name, desc ));
  }

  public MethodDescriptor getMethod( final AbstractMap.SimpleEntry method ) {
    return methods.get( method );
  }

  public FieldDescriptor getField( final String name ) {
    return fields.get( name );
  }

  public Collection getMethods() {
    return methods.values();
  }

  public boolean isInterface() {
    return ( super.access & Opcodes.ACC_INTERFACE ) != 0;
  }

  public boolean isEnum() {
    return ( super.access & Opcodes.ACC_ENUM ) != 0;
  }

  public boolean isAnnotation() {
    return ( super.access & Opcodes.ACC_ANNOTATION ) != 0;
  }

  public boolean isInnerClass() {
    return enclosingClass != null;
  }

  public boolean implementsMethod( final String methodName, final String methodDesc ) {
    return methods.containsKey( new AbstractMap.SimpleEntry( methodName, methodDesc ));
  }

  public boolean declaresField( final String fieldName ) {
    return fields.containsKey( fieldName );
  }

  public Collection getFields() {
    return fields.values();
  }

  public Set getAllImplementedInterfaces( Model model ) {
    if ( null != allInterfaces ) {
      return allInterfaces;
    } else {
      allInterfaces = new HashSet( 3 );
      model.getAllImplementedInterfaces( getName(), allInterfaces );
    }
    return allInterfaces;
  }

  public Set getAllAncestorClasses( Model model ) {
    if ( null != allAncestors ) {
      return allAncestors;
    } else {
      allAncestors = new HashSet( 3 );
      model.getAllAncestorClasses( getName(), allAncestors );
    }
    return allAncestors;
  }

  public String toString() {
    return "ClassDescriptor{" +
        "name='" + name + '\'' +
        ", enclosingClass='" + enclosingClass + '\'' +
        ", enclosingMethod=" + enclosingMethod +
        '}';
  }

  //  public String toString() {
////    final StringBuilder s = new StringBuilder();
////    s.append( "class " + name + " extends " + superName );
////
////    if ( interfaces.length != 0 ) {
////      s.append( " implements " );
////      for ( int i = 0; i < interfaces.length; i++ ) {
////        s.append( interfaces[ i ] + " " );
////      }
////    }
////
////    s.append( "\n\n" );
////
////    for ( FieldDescriptor field : fields.values() ) {
////      s.append( "\t" + field.getType() + " " + field.getName() + "\n" );
////    }
////
////    s.append( "\n" );
////
////    for ( MethodDescriptor method : methods.values() ) {
////      s.append( "\t" + method.getDesc() + " " + method.getName() + "\n" );
////    }
////
////    s.append( "\n\n\n" );
////
////    return s.toString();
//  }

  public Node getNewNode() {
    return this.newNode;
  }

  public Set getAttributesToKeep() {
    return attributesToKeep;
  }

  public void setRetainAttribute( String attr ) {
    attributesToKeep.add( attr );
  }

  public boolean getRetainAttribute( String attr ) {
    return attributesToKeep.contains( attr );
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy