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

com.getperka.cli.classpath.FieldSearch Maven / Gradle / Ivy

There is a newer version: 1.18
Show newest version
/*
 * #%L
 * Common Language Idioms
 * %%
 * Copyright (C) 2012 Perka Inc.
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package com.getperka.cli.classpath;

import java.lang.reflect.Field;

import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.ClassAdapter;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.Type;
import org.objectweb.asm.commons.EmptyVisitor;

class FieldSearch extends Search {
  FieldSearch() {}

  @Override
  protected ClassVisitor getVisitor() {
    return new ClassAdapter(new EmptyVisitor()) {
      private Type currentType;

      @Override
      public void visit(int version, int access, String name, String signature,
          String superName, String[] interfaces) {
        currentType = Type.getObjectType(name);
      }

      @Override
      public FieldVisitor visitField(int access, final String fieldName, String desc,
          String signature, Object value) {
        // Test the field's assignability to the desired type
        if (!testAssignable(Type.getType(desc)) || !testModifiers(access)) {
          return null;
        }
        return new FieldVisitor() {
          private boolean matches = testAnnotation(null);

          @Override
          public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
            // Test for the desired annotation
            matches |= testAnnotation(desc);
            return null;
          }

          @Override
          public void visitAttribute(Attribute arg0) {}

          @Override
          public void visitEnd() {
            if (matches) {
              getAccumulator().add(loadField(currentType, fieldName));
            }
          }
        };
      }
    };
  }

  @Override
  protected FieldSearch newInstance() {
    return new FieldSearch();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy