Search in sources :

Example 26 with ClassFile

use of javassist.bytecode.ClassFile in project powermock by powermock.

the class ClassFinalModifierMockTransformer method transform.

@Override
public CtClass transform(final CtClass clazz) {
    if (clazz.isInterface()) {
        return clazz;
    }
    if (getStrategy() != INST_REDEFINE) {
        if (Modifier.isFinal(clazz.getModifiers())) {
            clazz.setModifiers(clazz.getModifiers() ^ Modifier.FINAL);
        }
        ClassFile classFile = clazz.getClassFile2();
        AttributeInfo attribute = classFile.getAttribute(InnerClassesAttribute.tag);
        if (attribute != null && attribute instanceof InnerClassesAttribute) {
            InnerClassesAttribute ica = (InnerClassesAttribute) attribute;
            String name = classFile.getName();
            int n = ica.tableLength();
            for (int i = 0; i < n; ++i) {
                if (name.equals(ica.innerClass(i))) {
                    int accessFlags = ica.accessFlags(i);
                    if (Modifier.isFinal(accessFlags)) {
                        ica.setAccessFlags(i, accessFlags ^ Modifier.FINAL);
                    }
                }
            }
        }
    }
    return clazz;
}
Also used : AttributeInfo(javassist.bytecode.AttributeInfo) ClassFile(javassist.bytecode.ClassFile) InnerClassesAttribute(javassist.bytecode.InnerClassesAttribute)

Example 27 with ClassFile

use of javassist.bytecode.ClassFile in project reflections by ronmamo.

the class Reflections method scan.

protected Map<String, Map<String, Set<String>>> scan() {
    long start = System.currentTimeMillis();
    Map<String, Set<Map.Entry<String, String>>> collect = configuration.getScanners().stream().map(Scanner::index).distinct().collect(Collectors.toMap(s -> s, s -> Collections.synchronizedSet(new HashSet<>())));
    Set<URL> urls = configuration.getUrls();
    (configuration.isParallel() ? urls.stream().parallel() : urls.stream()).forEach(url -> {
        Vfs.Dir dir = null;
        try {
            dir = Vfs.fromURL(url);
            for (Vfs.File file : dir.getFiles()) {
                if (doFilter(file, configuration.getInputsFilter())) {
                    ClassFile classFile = null;
                    for (Scanner scanner : configuration.getScanners()) {
                        try {
                            if (doFilter(file, scanner::acceptsInput)) {
                                List<Map.Entry<String, String>> entries = scanner.scan(file);
                                if (entries == null) {
                                    if (classFile == null)
                                        classFile = getClassFile(file);
                                    entries = scanner.scan(classFile);
                                }
                                if (entries != null)
                                    collect.get(scanner.index()).addAll(entries);
                            }
                        } catch (Exception e) {
                            if (log != null)
                                log.trace("could not scan file {} with scanner {}", file.getRelativePath(), scanner.getClass().getSimpleName(), e);
                        }
                    }
                }
            }
        } catch (Exception e) {
            if (log != null)
                log.warn("could not create Vfs.Dir from url. ignoring the exception and continuing", e);
        } finally {
            if (dir != null)
                dir.close();
        }
    });
    // merge
    Map<String, Map<String, Set<String>>> storeMap = collect.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().stream().filter(e -> e.getKey() != null).collect(Collectors.groupingBy(Map.Entry::getKey, HashMap::new, Collectors.mapping(Map.Entry::getValue, Collectors.toSet())))));
    if (log != null) {
        int keys = 0, values = 0;
        for (Map<String, Set<String>> map : storeMap.values()) {
            keys += map.size();
            values += map.values().stream().mapToLong(Set::size).sum();
        }
        log.info(format("Reflections took %d ms to scan %d urls, producing %d keys and %d values", System.currentTimeMillis() - start, urls.size(), keys, values));
    }
    return storeMap;
}
Also used : ReflectionUtils.withAnyParameterAnnotation(org.reflections.ReflectionUtils.withAnyParameterAnnotation) Vfs(org.reflections.vfs.Vfs) DataInputStream(java.io.DataInputStream) BufferedInputStream(java.io.BufferedInputStream) URL(java.net.URL) ReflectionUtils.withAnnotation(org.reflections.ReflectionUtils.withAnnotation) LoggerFactory(org.slf4j.LoggerFactory) Inherited(java.lang.annotation.Inherited) Scanners(org.reflections.scanners.Scanners) HashMap(java.util.HashMap) Constructor(java.lang.reflect.Constructor) HashSet(java.util.HashSet) MemberUsageScanner(org.reflections.scanners.MemberUsageScanner) NameHelper(org.reflections.util.NameHelper) FilterBuilder(org.reflections.util.FilterBuilder) Map(java.util.Map) StreamSupport(java.util.stream.StreamSupport) XmlSerializer(org.reflections.serializers.XmlSerializer) Method(java.lang.reflect.Method) ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) Nullable(javax.annotation.Nullable) LinkedHashSet(java.util.LinkedHashSet) Logger(org.slf4j.Logger) ClassFile(javassist.bytecode.ClassFile) Scanner(org.reflections.scanners.Scanner) Predicate(java.util.function.Predicate) Member(java.lang.reflect.Member) Collection(java.util.Collection) Set(java.util.Set) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) ClasspathHelper(org.reflections.util.ClasspathHelper) File(java.io.File) String.format(java.lang.String.format) QueryFunction(org.reflections.util.QueryFunction) List(java.util.List) MethodParameterNamesScanner(org.reflections.scanners.MethodParameterNamesScanner) Stream(java.util.stream.Stream) Annotation(java.lang.annotation.Annotation) Serializer(org.reflections.serializers.Serializer) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) InputStream(java.io.InputStream) AnnotatedElement(java.lang.reflect.AnnotatedElement) MemberUsageScanner(org.reflections.scanners.MemberUsageScanner) Scanner(org.reflections.scanners.Scanner) MethodParameterNamesScanner(org.reflections.scanners.MethodParameterNamesScanner) Vfs(org.reflections.vfs.Vfs) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) ClassFile(javassist.bytecode.ClassFile) URL(java.net.URL) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 28 with ClassFile

use of javassist.bytecode.ClassFile in project hibernate-orm by hibernate.

the class BulkAccessorFactory method make.

private ClassFile make(Method[] getters, Method[] setters) throws CannotCompileException {
    String className = targetBean.getName();
    // set the name of bulk accessor.
    className = className + "_$$_bulkaccess_" + counter++;
    if (className.startsWith("java.")) {
        className = PACKAGE_NAME_PREFIX + className;
    }
    final ClassFile classfile = new ClassFile(false, className, BULKACESSOR_CLASS_NAME);
    classfile.setAccessFlags(AccessFlag.PUBLIC);
    addDefaultConstructor(classfile);
    addGetter(classfile, getters);
    addSetter(classfile, setters);
    return classfile;
}
Also used : ClassFile(javassist.bytecode.ClassFile)

Example 29 with ClassFile

use of javassist.bytecode.ClassFile in project powermock by powermock.

the class AbstractMainMockTransformer method removeFinalModifierFromClass.

protected void removeFinalModifierFromClass(final CtClass clazz) {
    if (strategy != INST_REDEFINE) {
        if (Modifier.isFinal(clazz.getModifiers())) {
            clazz.setModifiers(clazz.getModifiers() ^ Modifier.FINAL);
        }
        ClassFile classFile = clazz.getClassFile2();
        AttributeInfo attribute = classFile.getAttribute(InnerClassesAttribute.tag);
        if (attribute != null && attribute instanceof InnerClassesAttribute) {
            InnerClassesAttribute ica = (InnerClassesAttribute) attribute;
            String name = classFile.getName();
            int n = ica.tableLength();
            for (int i = 0; i < n; ++i) {
                if (name.equals(ica.innerClass(i))) {
                    int accessFlags = ica.accessFlags(i);
                    if (Modifier.isFinal(accessFlags)) {
                        ica.setAccessFlags(i, accessFlags ^ Modifier.FINAL);
                    }
                }
            }
        }
    }
}
Also used : AttributeInfo(javassist.bytecode.AttributeInfo) ClassFile(javassist.bytecode.ClassFile) InnerClassesAttribute(javassist.bytecode.InnerClassesAttribute)

Example 30 with ClassFile

use of javassist.bytecode.ClassFile in project ratpack by ratpack.

the class ClosureBackedHandler method describeTo.

@Override
public void describeTo(StringBuilder stringBuilder) {
    ClosureUtil.SourceInfo sourceInfo = ClosureUtil.getSourceInfo(invoker.getClosure());
    if (sourceInfo == null) {
        ClassPool pool = ClassPool.getDefault();
        try {
            CtClass ctClass = pool.get(invoker.getClosure().getClass().getName());
            CtMethod ctMethod = ctClass.getDeclaredMethod("doCall");
            int lineNumber = ctMethod.getMethodInfo().getLineNumber(0);
            ClassFile classFile = ctClass.getClassFile();
            String sourceFile = classFile.getSourceFile();
            if (lineNumber != -1 && sourceFile != null) {
                stringBuilder.append("closure at line ").append(lineNumber).append(" of ").append(sourceFile);
            } else {
                stringBuilder.append("closure ").append(invoker.getClosure().getClass().getName());
            }
        } catch (NotFoundException e) {
            stringBuilder.append(invoker.getClosure().getClass().getName());
        }
    } else {
        stringBuilder.append("closure at line ").append(sourceInfo.getLineNumber()).append(" of ").append(sourceInfo.getUri());
    }
}
Also used : CtClass(javassist.CtClass) ClassFile(javassist.bytecode.ClassFile) ClassPool(javassist.ClassPool) NotFoundException(javassist.NotFoundException) ClosureUtil(ratpack.groovy.internal.ClosureUtil) CtMethod(javassist.CtMethod)

Aggregations

ClassFile (javassist.bytecode.ClassFile)51 DataInputStream (java.io.DataInputStream)15 ClassPool (javassist.ClassPool)15 DataOutputStream (java.io.DataOutputStream)14 CtClass (javassist.CtClass)14 AnnotationsAttribute (javassist.bytecode.AnnotationsAttribute)14 ConstPool (javassist.bytecode.ConstPool)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 ByteArrayInputStream (java.io.ByteArrayInputStream)11 IOException (java.io.IOException)11 MethodInfo (javassist.bytecode.MethodInfo)11 CtField (javassist.CtField)10 NotFoundException (javassist.NotFoundException)9 Annotation (javassist.bytecode.annotation.Annotation)9 Test (org.junit.Test)9 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)8 HashSet (java.util.HashSet)7 CtMethod (javassist.CtMethod)7 FieldInfo (javassist.bytecode.FieldInfo)7 Bytecode (javassist.bytecode.Bytecode)5