• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

i-net-software / JWebAssembly / 534

pending completion
534

push

travis-ci-com

Horcrux7
replace static constructor für MethodHandles.

1 of 1 new or added line in 1 file covered. (100.0%)

5933 of 6826 relevant lines covered (86.92%)

0.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.86
/src/de/inetsoftware/jwebassembly/module/ClassFileLoader.java
1
/*
2
   Copyright 2020 - 2023 Volker Berlin (i-net software)
3

4
   Licensed under the Apache License, Version 2.0 (the "License");
5
   you may not use this file except in compliance with the License.
6
   You may obtain a copy of the License at
7

8
       http://www.apache.org/licenses/LICENSE-2.0
9

10
   Unless required by applicable law or agreed to in writing, software
11
   distributed under the License is distributed on an "AS IS" BASIS,
12
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
   See the License for the specific language governing permissions and
14
   limitations under the License.
15

16
*/
17
package de.inetsoftware.jwebassembly.module;
18

19
import java.io.IOException;
20
import java.io.InputStream;
21
import java.util.HashMap;
22

23
import javax.annotation.Nonnull;
24
import javax.annotation.Nullable;
25

26
import de.inetsoftware.classparser.ClassFile;
27
import de.inetsoftware.jwebassembly.WasmException;
28

29
/**
30
 * Cache and manager for the loaded ClassFiles
31
 * 
32
 * @author Volker Berlin
33
 */
34
public class ClassFileLoader {
35

36
    private final HashMap<String, ClassFile>        replace   = new HashMap<>();
1✔
37

38
    //A weak cache has produce problems if there are different versions of the same class in the build path and/or library path. Then the prescan can add the second version of the class. 
39
    private final HashMap<String, ClassFile>        cache = new HashMap<>();
1✔
40

41
    private final ClassLoader                       loader;
42

43
    private final ClassLoader                       bootLoader;
44

45
    /**
46
     * Create a new instance
47
     * 
48
     * @param loader
49
     *            the classloader to find the *.class files
50
     */
51
    public ClassFileLoader( ClassLoader loader ) {
1✔
52
        this.loader = loader;
1✔
53
        ClassLoader cl = ClassLoader.getSystemClassLoader();
1✔
54
        do {
55
            ClassLoader parent = cl.getParent();
1✔
56
            if( parent == null ) {
1✔
57
                bootLoader = cl;
1✔
58
                break;
1✔
59
            }
60
            cl = parent;
1✔
61
        } while( true );
1✔
62
    }
1✔
63

64
    /**
65
     * Get the ClassFile from cache or load it.
66
     * 
67
     * @param className
68
     *            the class name like "java/lang/Object"
69
     * @return the ClassFile or null
70
     * @throws IOException
71
     *             If any I/O error occur
72
     */
73
    @Nullable
74
    public ClassFile get( String className ) throws IOException {
75
        ClassFile classFile = replace.get( className );
1✔
76
        if( classFile != null ) {
1✔
77
            return classFile;
1✔
78
        }
79
        classFile = cache.get( className );
1✔
80
        if( classFile != null ) {
1✔
81
            return classFile;
1✔
82
        }
83
        InputStream stream = loader.getResourceAsStream( className + ".class" );
1✔
84
        if( stream != null ) {
1✔
85
            classFile = new ClassFile( stream );
1✔
86
            cache.put( className, classFile );
1✔
87
        }
88
        return classFile;
1✔
89
    }
90

91
    /**
92
     * Get the ClassFile from cache or load it. This returns ever a ClassFile or throw an exception if not found.
93
     * 
94
     * @param className
95
     *            the class name like "java/lang/Object"
96
     * @return the ClassFile or null
97
     * @throws IOException
98
     *             If any I/O error occur
99
     * @throws WasmException
100
     *             if the class was not found
101
     */
102
    @Nonnull
103
    public ClassFile getClassFile( String className ) throws IOException, WasmException {
104
        ClassFile classFile = get( className );
1✔
105
        if( classFile != null ) {
1✔
106
            return classFile;
1✔
107
        }
108
        throw new WasmException( "Missing class: " + className, -1 );
×
109
    }
110

111
    /**
112
     * Add a class file to the weak cache.
113
     * 
114
     * @param classFile
115
     *            the class file
116
     */
117
    public void cache( @Nonnull ClassFile classFile ) {
118
        String name = classFile.getThisClass().getName();
1✔
119
        if( bootLoader.getResource( name + ".class" ) != null ) {
1✔
120
            // if the same resource is exist in the JVM self then we need to hold the reference permanently
121
            if( replace.get( name ) == null ) {
×
122
                // does not add a second version of the same file
123
                replace.put( name, classFile );
×
124
            }
125
        } else {
126
            if( cache.get( name ) == null ) {
1✔
127
                // does not add a second version of the same file
128
                cache.put( name, classFile );
1✔
129
            }
130
        }
131
    }
1✔
132

133
    /**
134
     * Replace the class in the cache with the given instance to the loader cache.
135
     * 
136
     * @param className
137
     *            the name of the class to replace
138
     * @param classFile
139
     *            the replacing ClassFile
140
     */
141
    void replace( String className, ClassFile classFile ) {
142
        if( replace.get( className ) == null ) {
1✔
143
            classFile = new ClassFile( className, classFile );
1✔
144
            replace.put( className, classFile );
1✔
145
        }
146
    }
1✔
147

148
    /**
149
     * Add a partial class with the given instance to the loader cache.
150
     * 
151
     * @param className
152
     *            the name of the class to replace like "java/lang/String"
153
     * @param partialClassFile
154
     *            the partial ClassFile
155
     * @throws IOException
156
     *             If any I/O error occur
157
     */
158
    void partial( String className, ClassFile partialClassFile ) throws IOException {
159
        ClassFile classFile = get( className );
1✔
160
        replace.put( className, classFile );
1✔
161
        classFile.partial( partialClassFile );
1✔
162
    }
1✔
163
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc