builder = Stream.builder();
68 | iterable.forEach(builder);
69 | return builder.build();
70 | }
71 |
72 | private URLArray() {
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/modules/normalizer/src/it/sample/build.xml:
--------------------------------------------------------------------------------
1 |
2 |
20 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 | org.apache.commons.lang3.reflect.TypeLiteral,
44 | org.apache.commons.weaver.normalizer.example.ContrivedWrapper
45 |
46 | org.apache.commons.weaver.normalizer.example.normalized
47 |
48 |
49 |
50 |
51 |
52 |
53 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
--------------------------------------------------------------------------------
/ant/src/main/java/org/apache/commons/weaver/ant/InlineProperties.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one
3 | * or more contributor license agreements. See the NOTICE file
4 | * distributed with this work for additional information
5 | * regarding copyright ownership. The ASF licenses this file
6 | * to you under the Apache License, Version 2.0 (the
7 | * "License"); you may not use this file except in compliance
8 | * with the License. You may obtain a copy of the License at
9 | *
10 | * https://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing,
13 | * software distributed under the License is distributed on an
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 | * KIND, either express or implied. See the License for the
16 | * specific language governing permissions and limitations
17 | * under the License.
18 | */
19 | package org.apache.commons.weaver.ant;
20 |
21 | import java.util.Properties;
22 |
23 | import org.apache.commons.lang3.StringUtils;
24 | import org.apache.tools.ant.DynamicElementNS;
25 |
26 | /**
27 | * Structure to allow inline specification of properties.
28 | * Example:
29 | * {pre}<foo>foo-value</foo>
30 | * <bar>bar-value</bar>
31 | * <baz>baz
32 | * -nextline-value</baz>
33 | * {/pre}
34 | *
35 | */
36 | public class InlineProperties implements DynamicElementNS {
37 | /**
38 | * Represents a single inline property.
39 | */
40 | public final class InlineProperty {
41 | private final String name;
42 |
43 | private InlineProperty(final String name) {
44 | this.name = name;
45 | }
46 |
47 | /**
48 | * Add text to this property.
49 | * @param text to add
50 | */
51 | public void addText(final String text) {
52 | final String value;
53 | if (properties.containsKey(name)) {
54 | value = StringUtils.join(properties.getProperty(name), text);
55 | } else {
56 | value = text;
57 | }
58 | properties.setProperty(name, value);
59 | }
60 | }
61 |
62 | /**
63 | * {@link Properties} object maintained by the {@link InlineProperties}.
64 | */
65 | final Properties properties = new Properties();
66 |
67 | /**
68 | * Handle the specified nested element.
69 | * @param uri String URI
70 | * @param localName local element name
71 | * @param qName qualified name
72 | * @return InlineProperty
73 | */
74 | @Override
75 | public InlineProperty createDynamicElement(final String uri, final String localName, final String qName) {
76 | return new InlineProperty(localName);
77 | }
78 | }
--------------------------------------------------------------------------------
/.github/workflows/scorecards-analysis.yml:
--------------------------------------------------------------------------------
1 | # Licensed to the Apache Software Foundation (ASF) under one or more
2 | # contributor license agreements. See the NOTICE file distributed with
3 | # this work for additional information regarding copyright ownership.
4 | # The ASF licenses this file to You under the Apache license, Version 2.0
5 | # (the "License"); you may not use this file except in compliance with
6 | # the License. You may obtain a copy of the License at
7 | #
8 | # https://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 | name: "Scorecards supply-chain security"
17 |
18 | on:
19 | branch_protection_rule:
20 | schedule:
21 | - cron: "30 1 * * 6" # Weekly on Saturdays
22 | push:
23 | branches: [ "master" ]
24 |
25 | permissions: read-all
26 |
27 | jobs:
28 |
29 | analysis:
30 |
31 | name: "Scorecards analysis"
32 | runs-on: ubuntu-latest
33 | permissions:
34 | # Needed to upload the results to the code-scanning dashboard.
35 | security-events: write
36 | actions: read
37 | id-token: write # This is required for requesting the JWT
38 | contents: read # This is required for actions/checkout
39 |
40 | steps:
41 |
42 | - name: "Checkout code"
43 | uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
44 | with:
45 | persist-credentials: false
46 |
47 | - name: "Run analysis"
48 | uses: ossf/scorecard-action@4eaacf0543bb3f2c246792bd56e8cdeffafb205a # 2.4.3
49 | with:
50 | results_file: results.sarif
51 | results_format: sarif
52 | # A read-only PAT token, which is sufficient for the action to function.
53 | # The relevant discussion: https://github.com/ossf/scorecard-action/issues/188
54 | repo_token: ${{ secrets.GITHUB_TOKEN }}
55 | # Publish the results for public repositories to enable scorecard badges.
56 | # For more details: https://github.com/ossf/scorecard-action#publishing-results
57 | publish_results: true
58 |
59 | - name: "Upload artifact"
60 | uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # 6.0.0
61 | with:
62 | name: SARIF file
63 | path: results.sarif
64 | retention-days: 5
65 |
66 | - name: "Upload to code-scanning"
67 | uses: github/codeql-action/upload-sarif@5d4e8d1aca955e8d8589aabd499c5cae939e33c7 # v4.31.9
68 | with:
69 | sarif_file: results.sarif
70 |
--------------------------------------------------------------------------------
/processor/src/test/java/org/apache/commons/weaver/test/WeaveProcessorTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one
3 | * or more contributor license agreements. See the NOTICE file
4 | * distributed with this work for additional information
5 | * regarding copyright ownership. The ASF licenses this file
6 | * to you under the Apache License, Version 2.0 (the
7 | * "License"); you may not use this file except in compliance
8 | * with the License. You may obtain a copy of the License at
9 | *
10 | * https://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing,
13 | * software distributed under the License is distributed on an
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 | * KIND, either express or implied. See the License for the
16 | * specific language governing permissions and limitations
17 | * under the License.
18 | */
19 | package org.apache.commons.weaver.test;
20 |
21 | import java.util.Arrays;
22 | import java.util.Properties;
23 |
24 | import org.apache.commons.weaver.test.beans.TestBeanWithClassAnnotation;
25 | import org.apache.commons.weaver.test.beans.TestBeanWithMethodAnnotation;
26 | import org.apache.commons.weaver.test.weaver.TestWeaver;
27 | import org.apache.commons.weaver.WeaveProcessor;
28 |
29 | import org.junit.Assert;
30 | import org.junit.Test;
31 |
32 | /**
33 | * Test the {@link WeaveProcessor}
34 | */
35 | public class WeaveProcessorTest extends WeaverTestBase {
36 |
37 | @Test
38 | public void testWeaveVisiting() throws Exception {
39 | addClassForScanning(TestBeanWithMethodAnnotation.class);
40 | addClassForScanning(TestBeanWithClassAnnotation.class);
41 |
42 | final Properties config = new Properties();
43 | config.put("configKey", "configValue");
44 |
45 | final WeaveProcessor wp = new WeaveProcessor(getClassPathEntries(), getTargetFolder(), config);
46 |
47 | TestWeaver.wovenClasses.clear();
48 | TestWeaver.wovenMethods.clear();
49 |
50 | wp.weave();
51 |
52 | Assert.assertEquals(1, TestWeaver.wovenClasses.size());
53 | Assert.assertEquals(TestBeanWithClassAnnotation.class, TestWeaver.wovenClasses.get(0));
54 |
55 | Assert.assertEquals(1, TestWeaver.wovenMethods.size());
56 | Assert.assertEquals(TestBeanWithMethodAnnotation.class, TestWeaver.wovenMethods.get(0).getDeclaringClass());
57 |
58 | Assert.assertEquals(1, TestWeaver.implementors.size());
59 | Assert.assertEquals(TestBeanWithClassAnnotation.class, TestWeaver.implementors.get(0));
60 |
61 | Assert.assertEquals(2, TestWeaver.subclasses.size());
62 | Assert.assertTrue(TestWeaver.subclasses.containsAll(Arrays.> asList(
63 | TestBeanWithClassAnnotation.class, TestBeanWithMethodAnnotation.class)));
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/processor/src/main/java/org/apache/commons/weaver/CleanProcessor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one
3 | * or more contributor license agreements. See the NOTICE file
4 | * distributed with this work for additional information
5 | * regarding copyright ownership. The ASF licenses this file
6 | * to you under the Apache License, Version 2.0 (the
7 | * "License"); you may not use this file except in compliance
8 | * with the License. You may obtain a copy of the License at
9 | *
10 | * https://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing,
13 | * software distributed under the License is distributed on an
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 | * KIND, either express or implied. See the License for the
16 | * specific language governing permissions and limitations
17 | * under the License.
18 | */
19 | package org.apache.commons.weaver;
20 |
21 | import java.io.File;
22 | import java.util.List;
23 | import java.util.Properties;
24 | import java.util.ServiceLoader;
25 | import java.util.logging.Logger;
26 |
27 | import org.apache.commons.weaver.lifecycle.WeaveLifecycle; //NOPMD used in Javadoc
28 | import org.apache.commons.weaver.model.WeaveEnvironment;
29 | import org.apache.commons.weaver.spi.Cleaner;
30 |
31 | /**
32 | * Implements {@link WeaveLifecycle#CLEAN}.
33 | */
34 | public class CleanProcessor extends ProcessorBase {
35 |
36 | /**
37 | * Create a new {@link CleanProcessor} instance using the {@link ServiceLoader} mechanism.
38 | *
39 | * @param classpath not {@code null}
40 | * @param target not {@code null}
41 | * @param configuration not {@code null}
42 | */
43 | public CleanProcessor(final List classpath, final File target, final Properties configuration) {
44 | this(classpath, target, configuration, getServiceInstances(Cleaner.class));
45 | }
46 |
47 | /**
48 | * Create a new {@link CleanProcessor} instance.
49 | *
50 | * @param classpath not {@code null}
51 | * @param target not {@code null}
52 | * @param configuration not {@code null}
53 | * @param providers not (@code null}
54 | */
55 | public CleanProcessor(final List classpath, final File target, final Properties configuration,
56 | final Iterable providers) {
57 | super(classpath, target, configuration, providers);
58 | }
59 |
60 | /**
61 | * Clean specified targets.
62 | */
63 | public void clean() {
64 | if (!target.exists()) {
65 | log.warning(() -> String.format("Target directory %s does not exist; nothing to do!", target));
66 | }
67 | for (final Cleaner cleaner : providers) {
68 | final WeaveEnvironment env = new LocalWeaveEnvironment(target, classLoader, configuration,
69 | Logger.getLogger(cleaner.getClass().getName()));
70 | cleaner.clean(env, finder);
71 | }
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/processor/src/main/java/org/apache/commons/weaver/WeaveProcessor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one
3 | * or more contributor license agreements. See the NOTICE file
4 | * distributed with this work for additional information
5 | * regarding copyright ownership. The ASF licenses this file
6 | * to you under the Apache License, Version 2.0 (the
7 | * "License"); you may not use this file except in compliance
8 | * with the License. You may obtain a copy of the License at
9 | *
10 | * https://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing,
13 | * software distributed under the License is distributed on an
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 | * KIND, either express or implied. See the License for the
16 | * specific language governing permissions and limitations
17 | * under the License.
18 | */
19 | package org.apache.commons.weaver;
20 |
21 | import java.io.File;
22 | import java.util.List;
23 | import java.util.Properties;
24 | import java.util.ServiceLoader;
25 | import java.util.logging.Logger;
26 |
27 | import org.apache.commons.weaver.lifecycle.WeaveLifecycle; //NOPMD used in Javadoc
28 | import org.apache.commons.weaver.model.WeaveEnvironment;
29 | import org.apache.commons.weaver.spi.Weaver;
30 |
31 | /**
32 | * Implements {@link WeaveLifecycle#WEAVE}.
33 | */
34 | public class WeaveProcessor extends ProcessorBase {
35 |
36 | /**
37 | * Create a new {@link WeaveProcessor} instance using the {@link ServiceLoader} mechanism.
38 | *
39 | * @param classpath not {@code null}
40 | * @param target not {@code null}
41 | * @param configuration not {@code null}
42 | */
43 | public WeaveProcessor(final List classpath, final File target, final Properties configuration) {
44 | super(classpath, target, configuration, getServiceInstances(Weaver.class));
45 | }
46 |
47 | /**
48 | * Create a new {@link WeaveProcessor} instance.
49 | *
50 | * @param classpath not {@code null}
51 | * @param target not {@code null}
52 | * @param configuration not {@code null}
53 | * @param providers not (@code null}
54 | */
55 | public WeaveProcessor(final List classpath, final File target, final Properties configuration,
56 | final Iterable providers) {
57 | super(classpath, target, configuration, providers);
58 | }
59 |
60 | /**
61 | * Weave classes in target directory.
62 | */
63 | public void weave() {
64 | if (!target.exists()) {
65 | log.warning(() -> String.format("Target directory %s does not exist; nothing to do!", target));
66 | }
67 | for (final Weaver weaver : providers) {
68 | final WeaveEnvironment env = new LocalWeaveEnvironment(target, classLoader, configuration,
69 | Logger.getLogger(weaver.getClass().getName()));
70 | weaver.process(env, finder);
71 | }
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/modules/privilizer/weaver/src/it/sample/src/main/java/org/apache/commons/weaver/privilizer/example/UsingArgs.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one
3 | * or more contributor license agreements. See the NOTICE file
4 | * distributed with this work for additional information
5 | * regarding copyright ownership. The ASF licenses this file
6 | * to you under the Apache License, Version 2.0 (the
7 | * "License"); you may not use this file except in compliance
8 | * with the License. You may obtain a copy of the License at
9 | *
10 | * https://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing,
13 | * software distributed under the License is distributed on an
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 | * KIND, either express or implied. See the License for the
16 | * specific language governing permissions and limitations
17 | * under the License.
18 | */
19 | package org.apache.commons.weaver.privilizer.example;
20 |
21 | import java.util.ArrayList;
22 |
23 | import org.apache.commons.lang3.ArrayUtils;
24 | import org.apache.commons.weaver.privilizer.Privileged;
25 |
26 | public class UsingArgs {
27 |
28 | @Privileged
29 | String getProperty(String name) {
30 | return System.getProperty(name);
31 | }
32 |
33 | @Privileged
34 | String[] getProperties(String... names) {
35 | if (names == null) {
36 | return null;
37 | }
38 | final ArrayList result = new ArrayList();
39 | // in reality one would delegate to #getProperty to minimize the scope
40 | // of the privileged action
41 | for (String name : names) {
42 | result.add(System.getProperty(name));
43 | }
44 | return result.toArray(ArrayUtils.EMPTY_STRING_ARRAY);
45 | }
46 |
47 | @Privileged
48 | void throwAwayProperty(int first, String middle, char last) {
49 | System.getProperty(new StringBuilder().append((char) first).append(middle).append(last).toString());
50 | }
51 |
52 | @Privileged
53 | Object assembleAndGetProperty(char first, CharSequence middle, int last) {
54 | return System.getProperty(new StringBuilder().append(first).append(middle).append((char) last).toString());
55 | }
56 |
57 | public static class CheckedException1 extends Exception {
58 | private static final long serialVersionUID = 1L;
59 | }
60 |
61 | public static class CheckedException2 extends Exception {
62 | private static final long serialVersionUID = 1L;
63 | }
64 |
65 | @Privileged
66 | int throwingCheckedException(int which, String propertyToGet) throws CheckedException1, CheckedException2 {
67 | System.getProperty(propertyToGet);
68 | switch (which) {
69 | case 1:
70 | throw new CheckedException1();
71 | case 2:
72 | throw new CheckedException2();
73 | default:
74 | return which;
75 | }
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/modules/privilizer/weaver/src/it/sample/src/main/java/org/apache/commons/weaver/privilizer/example/StaticUsingArgs.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one
3 | * or more contributor license agreements. See the NOTICE file
4 | * distributed with this work for additional information
5 | * regarding copyright ownership. The ASF licenses this file
6 | * to you under the Apache License, Version 2.0 (the
7 | * "License"); you may not use this file except in compliance
8 | * with the License. You may obtain a copy of the License at
9 | *
10 | * https://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing,
13 | * software distributed under the License is distributed on an
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 | * KIND, either express or implied. See the License for the
16 | * specific language governing permissions and limitations
17 | * under the License.
18 | */
19 | package org.apache.commons.weaver.privilizer.example;
20 |
21 | import java.util.ArrayList;
22 |
23 | import org.apache.commons.lang3.ArrayUtils;
24 | import org.apache.commons.weaver.privilizer.Privileged;
25 |
26 | public abstract class StaticUsingArgs {
27 |
28 | private StaticUsingArgs() {
29 | }
30 |
31 | @Privileged
32 | static String getProperty(String name) {
33 | return System.getProperty(name);
34 | }
35 |
36 | @Privileged
37 | static String[] getProperties(String... names) {
38 | if (names == null) {
39 | return null;
40 | }
41 | final ArrayList result = new ArrayList();
42 | // in reality one would delegate to #getProperty to minimize the scope
43 | // of the privileged action
44 | for (String name : names) {
45 | result.add(System.getProperty(name));
46 | }
47 | return result.toArray(ArrayUtils.EMPTY_STRING_ARRAY);
48 | }
49 |
50 | @Privileged
51 | static void throwAwayProperty(int first, String middle, char last) {
52 | System.getProperty(new StringBuilder().append((char) first).append(middle).append(last).toString());
53 | }
54 |
55 | @Privileged
56 | static Object assembleAndGetProperty(char first, CharSequence middle, int last) {
57 | return System.getProperty(new StringBuilder().append(first).append(middle).append((char) last).toString());
58 | }
59 |
60 | public static class CheckedException1 extends Exception {
61 | private static final long serialVersionUID = 1L;
62 | }
63 |
64 | public static class CheckedException2 extends Exception {
65 | private static final long serialVersionUID = 1L;
66 | }
67 |
68 | @Privileged
69 | static int throwingCheckedException(int which, String propertyToGet) throws CheckedException1, CheckedException2 {
70 | System.getProperty(propertyToGet);
71 | switch (which) {
72 | case 1:
73 | throw new CheckedException1();
74 | case 2:
75 | throw new CheckedException2();
76 | default:
77 | return which;
78 | }
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/processor/src/main/java/org/apache/commons/weaver/model/WeavablePackage.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one
3 | * or more contributor license agreements. See the NOTICE file
4 | * distributed with this work for additional information
5 | * regarding copyright ownership. The ASF licenses this file
6 | * to you under the Apache License, Version 2.0 (the
7 | * "License"); you may not use this file except in compliance
8 | * with the License. You may obtain a copy of the License at
9 | *
10 | * https://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing,
13 | * software distributed under the License is distributed on an
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 | * KIND, either express or implied. See the License for the
16 | * specific language governing permissions and limitations
17 | * under the License.
18 | */
19 | package org.apache.commons.weaver.model;
20 |
21 | import java.util.Collections;
22 | import java.util.Comparator;
23 | import java.util.concurrent.ConcurrentNavigableMap;
24 | import java.util.concurrent.ConcurrentSkipListMap;
25 |
26 | /**
27 | * {@link Weavable} {@link Package}.
28 | */
29 | public class WeavablePackage extends Weavable {
30 | private static final Comparator CMP = Comparator.nullsFirst(Comparator
31 | .comparing(WeavablePackage::getTarget, Comparator.nullsFirst(Comparator.comparing(Package::getName))));
32 |
33 | private final ConcurrentNavigableMap> clazzes = new ConcurrentSkipListMap<>();
34 |
35 | /**
36 | * Create a new {@link WeavablePackage} instance.
37 | * @param target package
38 | */
39 | public WeavablePackage(final Package target) {
40 | super(target);
41 | }
42 |
43 | /**
44 | * Gets a {@link WeavableClass} representing {@code cls}.
45 | * @param cls to wrap
46 | * @param generic type of {@code cls}
47 | * @return {@link WeavableClass}
48 | */
49 | @SuppressWarnings("unchecked")
50 | public synchronized WeavableClass getWeavable(final Class cls) {
51 | return (WeavableClass) clazzes.computeIfAbsent(cls.getName(), k -> new WeavableClass<>(cls, this));
52 | }
53 |
54 | /**
55 | * Gets enclosed {@link WeavableClass}es.
56 | * @return {@link Iterable}
57 | */
58 | public Iterable> getClasses() {
59 | return Collections.unmodifiableCollection(clazzes.values());
60 | }
61 |
62 | /**
63 | * Implement {@link Comparable}.
64 | * @param arg0 {@link WeavablePackage} to compare against
65 | * @return int per {@link Comparable#compareTo(Object)} contract
66 | */
67 | @Override
68 | public int compareTo(final WeavablePackage arg0) {
69 | return CMP.compare(this, arg0);
70 | }
71 |
72 | /**
73 | * {@inheritDoc}
74 | */
75 | @Override
76 | public String toString() {
77 | if (getTarget() == null) {
78 | return "Weavable default package";
79 | }
80 | return super.toString();
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/modules/privilizer/weaver/src/it/sample/src/test/java/org/apache/commons/weaver/privilizer/example/UsingBlueprintsTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one
3 | * or more contributor license agreements. See the NOTICE file
4 | * distributed with this work for additional information
5 | * regarding copyright ownership. The ASF licenses this file
6 | * to you under the Apache License, Version 2.0 (the
7 | * "License"); you may not use this file except in compliance
8 | * with the License. You may obtain a copy of the License at
9 | *
10 | * https://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing,
13 | * software distributed under the License is distributed on an
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 | * KIND, either express or implied. See the License for the
16 | * specific language governing permissions and limitations
17 | * under the License.
18 | */
19 | package org.apache.commons.weaver.privilizer.example;
20 |
21 | import static org.junit.Assume.assumeTrue;
22 | import static org.junit.Assert.assertEquals;
23 |
24 | import org.apache.commons.lang3.StringUtils;
25 | import org.apache.commons.lang3.SystemUtils;
26 |
27 | import org.junit.Before;
28 | import org.junit.Test;
29 |
30 | public class UsingBlueprintsTest {
31 |
32 | private UsingBlueprints usingBlueprints;
33 |
34 | @Before
35 | public void setUp() throws Exception {
36 | Setup.setProperty("foo", "foo-value");
37 | Setup.setProperty("bar", "bar-value");
38 | Setup.setProperty("baz", "baz-value");
39 | usingBlueprints = new UsingBlueprints();
40 | }
41 |
42 | @Test
43 | public void testUtilsReadPublicConstant() {
44 | assertEquals(Utils.FOO, usingBlueprints.utilsReadPublicConstant());
45 | }
46 |
47 | @Test
48 | public void testUtilsReadPrivateField() {
49 | assertEquals(999, usingBlueprints.utilsReadPrivateField());
50 | }
51 |
52 | @Test
53 | public void testUtilsGetProperty() {
54 | assertEquals("foo-value", usingBlueprints.utilsGetProperty());
55 | }
56 |
57 | @Test
58 | public void testUtilsGetProperty_String() {
59 | assertEquals("foo-value", usingBlueprints.utilsGetProperty("foo"));
60 | assertEquals("bar-value", usingBlueprints.utilsGetProperty("bar"));
61 | assertEquals("baz-value", usingBlueprints.utilsGetProperty("baz"));
62 | }
63 |
64 | @Test
65 | public void testUtilsGetProperty_int_String() {
66 | assertEquals("foo-value", usingBlueprints.utilsGetProperty(2, "foo"));
67 | assertEquals("bar-value", usingBlueprints.utilsGetProperty(2, "bar"));
68 | assertEquals("baz-value", usingBlueprints.utilsGetProperty(2, "baz"));
69 | }
70 |
71 | @Test
72 | public void testMoreGetProperty() {
73 | assertEquals("bar-value", usingBlueprints.moreGetProperty());
74 | }
75 |
76 | @Test
77 | public void testMoreGetTopStackElementClassName() {
78 | assumeTrue(StringUtils.containsIgnoreCase(SystemUtils.JAVA_VENDOR, "oracle"));
79 | assertEquals(Utils.More.class.getName(), usingBlueprints.moreGetTopStackElementClassName());
80 | }
81 |
82 | }
83 |
--------------------------------------------------------------------------------
/processor/src/main/java/org/apache/commons/weaver/model/ScanRequest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one
3 | * or more contributor license agreements. See the NOTICE file
4 | * distributed with this work for additional information
5 | * regarding copyright ownership. The ASF licenses this file
6 | * to you under the Apache License, Version 2.0 (the
7 | * "License"); you may not use this file except in compliance
8 | * with the License. You may obtain a copy of the License at
9 | *
10 | * https://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing,
13 | * software distributed under the License is distributed on an
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 | * KIND, either express or implied. See the License for the
16 | * specific language governing permissions and limitations
17 | * under the License.
18 | */
19 |
20 | package org.apache.commons.weaver.model;
21 |
22 | import java.util.ArrayList;
23 | import java.util.Collections;
24 | import java.util.LinkedHashSet;
25 | import java.util.List;
26 | import java.util.Objects;
27 | import java.util.Set;
28 |
29 | import org.apache.commons.lang3.Validate;
30 | import org.apache.commons.weaver.spi.Cleaner;
31 | import org.apache.commons.weaver.spi.Weaver;
32 |
33 | /**
34 | * Scan request object describing the types of elements in which a given {@link Weaver} or {@link Cleaner} is
35 | * interested.
36 | */
37 | public class ScanRequest {
38 |
39 | private final List interests = new ArrayList<>();
40 | private final Set> supertypes = new LinkedHashSet<>();
41 |
42 | /**
43 | * Register a {@link WeaveInterest}.
44 | * @param interest {@link WeaveInterest} to add
45 | * @return {@code this}, fluently
46 | */
47 | public ScanRequest add(final WeaveInterest interest) {
48 | Objects.requireNonNull(interest, "interest");
49 | interests.add(interest);
50 | return this;
51 | }
52 |
53 | /**
54 | * Register one or more types whose subtypes you are looking for.
55 | * @param types {@link Class}es to add
56 | * @return {@code this}, fluently
57 | */
58 | public ScanRequest addSupertypes(final Class>... types) {
59 | Collections.addAll(supertypes, Validate.noNullElements(types, "null element at [%s]"));
60 | return this;
61 | }
62 |
63 | /**
64 | * Gets registered {@link WeaveInterest}s.
65 | * @return {@link Iterable}
66 | */
67 | public Iterable getInterests() {
68 | return Collections.unmodifiableList(interests);
69 | }
70 |
71 | /**
72 | * Gets registered {@link Class}es whose subtypes will be returned.
73 | * @return {@link Set}
74 | */
75 | public Set> getSupertypes() {
76 | return Collections.unmodifiableSet(supertypes);
77 | }
78 |
79 | /**
80 | * Learn whether this {@link ScanRequest} has been constrained. An unconstrained {@link ScanRequest} will return all
81 | * known types.
82 | * @return {@code boolean}
83 | * @since 1.3
84 | */
85 | public boolean isConstrained() {
86 | return !interests.isEmpty() || !supertypes.isEmpty();
87 | }
88 | }
89 |
--------------------------------------------------------------------------------