This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 227050
Collapse All | Expand All

(-)a/settings/manifest.mf (-1 / +1 lines)
Lines 3-7 Link Here
3
OpenIDE-Module-Layer: org/netbeans/modules/settings/resources/mf-layer.xml
3
OpenIDE-Module-Layer: org/netbeans/modules/settings/resources/mf-layer.xml
4
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/settings/resources/Bundle.properties
4
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/settings/resources/Bundle.properties
5
AutoUpdate-Essential-Module: true
5
AutoUpdate-Essential-Module: true
6
OpenIDE-Module-Specification-Version: 1.39
6
OpenIDE-Module-Specification-Version: 1.40
7
7
(-)0343ed0a18d1 (+69 lines)
Added Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2013 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2013 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.api.settings;
43
44
import java.lang.annotation.ElementType;
45
import java.lang.annotation.Retention;
46
import java.lang.annotation.RetentionPolicy;
47
import java.lang.annotation.Target;
48
import org.netbeans.spi.settings.Convertor;
49
50
/** Specifies an alternative factory method to use (rather than constructor)
51
 * to create the instance. Use in orchestration with 
52
 * {@link ConvertAsProperties} or only any class that is processed by serializing
53
 * {@link Convertor}s.
54
 * 
55
 * @since 1.40
56
 * @author Jaroslav Tulach <jtulach@netbeans.org>
57
 */
58
@Target(ElementType.TYPE)
59
@Retention(RetentionPolicy.RUNTIME)
60
public @interface FactoryMethod {
61
    /**
62
     * Name of factory method to use instead of default constructor. Sometimes, for
63
     * example when dealing with singletons, it may be desirable to control how
64
     * an instance of given class is created. In such case one can create a
65
     * factory method (takes no arguments and returns instance of desired type)
66
     * in the class annotated by {@link ConvertAsProperties} annotation.
67
     */
68
    String value();
69
}
(-)a/settings/src/org/netbeans/modules/settings/convertors/XMLPropertiesConvertor.java (-1 / +1 lines)
Lines 225-231 Link Here
225
        
225
        
226
        Class c = getInstanceClass();
226
        Class c = getInstanceClass();
227
        try {
227
        try {
228
            return c.newInstance();
228
            return XMLSettingsSupport.newInstance(c);
229
        } catch (Exception ex) { // IllegalAccessException, InstantiationException
229
        } catch (Exception ex) { // IllegalAccessException, InstantiationException
230
            IOException ioe = new IOException("Cannot create instance of " + c.getName()); // NOI18N
230
            IOException ioe = new IOException("Cannot create instance of " + c.getName()); // NOI18N
231
            ioe.initCause(ex);
231
            ioe.initCause(ex);
(-)a/settings/src/org/netbeans/modules/settings/convertors/XMLSettingsSupport.java (-3 / +16 lines)
Lines 46-55 Link Here
46
46
47
import java.io.*;
47
import java.io.*;
48
import java.lang.reflect.Constructor;
48
import java.lang.reflect.Constructor;
49
import java.lang.reflect.InvocationTargetException;
49
import java.lang.reflect.Method;
50
import java.lang.reflect.Method;
50
import java.util.*;
51
import java.util.*;
51
import java.util.logging.Level;
52
import java.util.logging.Level;
52
import java.util.logging.Logger;
53
import java.util.logging.Logger;
54
import org.netbeans.api.settings.FactoryMethod;
53
55
54
import org.openide.filesystems.*;
56
import org.openide.filesystems.*;
55
import org.openide.modules.ModuleInfo;
57
import org.openide.modules.ModuleInfo;
Lines 78-83 Link Here
78
    
80
    
79
    /** Logging for events in XML settings system. */
81
    /** Logging for events in XML settings system. */
80
    static final Logger err = Logger.getLogger(XMLSettingsSupport.class.getName()); // NOI18N
82
    static final Logger err = Logger.getLogger(XMLSettingsSupport.class.getName()); // NOI18N
83
84
    static Object newInstance(Class<?> clazz) throws IllegalArgumentException, 
85
    InstantiationException, NoSuchMethodException, InvocationTargetException, 
86
    IllegalAccessException {
87
        FactoryMethod fm = clazz.getAnnotation(FactoryMethod.class);
88
        if (fm != null) {
89
            return clazz.getMethod(fm.value()).invoke(null);
90
        }
91
        Constructor<?> c = clazz.getDeclaredConstructor();
92
        c.setAccessible(true);
93
        return c.newInstance();
94
    }
95
81
    
96
    
82
    /** Store instanceof elements.
97
    /** Store instanceof elements.
83
     * @param classes everything what class extends or implements
98
     * @param classes everything what class extends or implements
Lines 603-611 Link Here
603
                        }
618
                        }
604
                    } else {
619
                    } else {
605
                        try {
620
                        try {
606
                            Constructor<?> c = clazz.getDeclaredConstructor();
621
                            inst = newInstance(clazz);
607
                            c.setAccessible(true);
608
                            inst = c.newInstance();
609
                        } catch (Exception ex) {
622
                        } catch (Exception ex) {
610
                            IOException ioe = new IOException();
623
                            IOException ioe = new IOException();
611
                            ioe.initCause(ex);
624
                            ioe.initCause(ex);
(-)a/settings/test/unit/src/org/netbeans/modules/settings/convertors/SerialDataConvertorTest.java (+35 lines)
Lines 53-61 Link Here
53
import org.openide.modules.ModuleInfo;
53
import org.openide.modules.ModuleInfo;
54
import org.openide.util.*;
54
import org.openide.util.*;
55
import java.io.*;
55
import java.io.*;
56
import java.lang.ref.Reference;
57
import java.lang.ref.WeakReference;
56
import java.util.*;
58
import java.util.*;
57
import junit.framework.Test;
59
import junit.framework.Test;
58
import junit.framework.TestSuite;
60
import junit.framework.TestSuite;
61
import org.netbeans.api.settings.FactoryMethod;
59
62
60
import org.netbeans.junit.*;
63
import org.netbeans.junit.*;
61
64
Lines 657-660 Link Here
657
        assertNotNull("Missing InstanceCookie", ic);
660
        assertNotNull("Missing InstanceCookie", ic);
658
        assertNotNull("the persisted object cannot be read", ic.instanceCreate());
661
        assertNotNull("the persisted object cannot be read", ic.instanceCreate());
659
    }
662
    }
663
    
664
    public void testFactoryMethod() throws IOException, ClassNotFoundException {
665
        DataFolder df = DataFolder.findFolder(FileUtil.getConfigRoot().createFolder("testFactoryMethod"));
666
        FileObject fo = df.getPrimaryFile().createData("test.settings");
667
        OutputStream os = fo.getOutputStream();
668
        os.write((
669
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
670
"<!DOCTYPE settings PUBLIC \"-//NetBeans//DTD Session settings 1.0//EN\" \"http://www.netbeans.org/dtds/sessionsettings-1_0.dtd\">\n" +
671
"<settings version=\"1.0\">\n" +
672
"  <instance class=\"" + FactoryBase.class.getName() + "\"/>\n" +
673
"</settings>\n"
674
        ).getBytes("UTF-8"));
675
        os.close();
676
        
677
        InstanceCookie ido = DataObject.find(fo).getCookie(InstanceCookie.class);
678
        FactoryBase fb = (FactoryBase) ido.instanceCreate();
679
        assertNotNull("Re-created OK!", fb);
680
    }
681
682
    @FactoryMethod("create")
683
    public static class FactoryBase implements Serializable {
684
        private FactoryBase() {
685
            throw new IllegalStateException("Don't call my default constructor");
686
        }
687
        
688
        FactoryBase(boolean ok) {
689
        }
690
        
691
        public static FactoryBase create() {
692
            return new FactoryBase(true);
693
        }
694
    }
660
}
695
}
(-)a/settings/test/unit/src/org/netbeans/modules/settings/convertors/XMLPropertiesConvertorTest.java (+52 lines)
Lines 45-50 Link Here
45
package org.netbeans.modules.settings.convertors;
45
package org.netbeans.modules.settings.convertors;
46
46
47
import java.io.*;
47
import java.io.*;
48
import java.lang.ref.Reference;
49
import java.lang.ref.WeakReference;
50
import java.util.Properties;
51
import static junit.framework.Assert.assertEquals;
52
import static junit.framework.Assert.assertNotNull;
53
import org.netbeans.api.settings.ConvertAsProperties;
54
import org.netbeans.api.settings.FactoryMethod;
48
55
49
import org.netbeans.junit.NbTestCase;
56
import org.netbeans.junit.NbTestCase;
50
import org.netbeans.junit.RandomlyFails;
57
import org.netbeans.junit.RandomlyFails;
Lines 58-63 Link Here
58
import org.openide.filesystems.FileObject;
65
import org.openide.filesystems.FileObject;
59
import org.openide.filesystems.FileSystem;
66
import org.openide.filesystems.FileSystem;
60
import org.openide.filesystems.FileUtil;
67
import org.openide.filesystems.FileUtil;
68
import org.openide.filesystems.Repository;
61
import org.openide.filesystems.XMLFileSystem;
69
import org.openide.filesystems.XMLFileSystem;
62
import org.openide.loaders.*;
70
import org.openide.loaders.*;
63
import org.openide.modules.ModuleInfo;
71
import org.openide.modules.ModuleInfo;
Lines 352-357 Link Here
352
        assertEquals("Listener not deregistered", 0, obj.getListenerCount());
360
        assertEquals("Listener not deregistered", 0, obj.getListenerCount());
353
        assertNull(filename + ".settings was not deleted!", root.getFileObject(filename));
361
        assertNull(filename + ".settings was not deleted!", root.getFileObject(filename));
354
    }
362
    }
363
364
    public void testFactoryMethod() throws Exception {
365
        FileObject dtdFO = Repository.getDefault().getDefaultFileSystem().
366
            findResource("/xml/lookups/abc/x.instance");
367
        assertNotNull("Provider not found", dtdFO);
368
        Convertor c = XMLPropertiesConvertor.create(dtdFO);
369
        assertNotNull("Convertor created", c);
370
        
371
        DataFolder folder = DataFolder.findFolder(root);
372
        
373
        FactoryBase inst = FactoryBase.create();
374
        InstanceDataObject ido = InstanceDataObject.create(folder, null, inst, null);
375
376
        assertSame("Instance is there", inst, ido.instanceCreate());
377
        
378
        Reference<Object> ref = new WeakReference<Object>(inst);
379
        inst = null;
380
        
381
        assertGC("Instance can disappear", ref);
382
        
383
        Object obj = ido.instanceCreate();
384
        assertEquals("One can re-create it without default constructor", FactoryBase.class, obj.getClass());
385
    }
386
    
387
    @ConvertAsProperties(dtd = "-//abc/x")
388
    @FactoryMethod("create")
389
    public static class FactoryBase implements Serializable {
390
        public FactoryBase() {
391
            throw new IllegalStateException("Don't call my default constructor");
392
        }
393
        
394
        FactoryBase(boolean ok) {
395
        }
396
        
397
        public static FactoryBase create() {
398
            return new FactoryBase(true);
399
        }
400
        
401
        void readProperties(Properties p) {
402
        }
403
        
404
        void writeProperties(Properties p) {
405
        }
406
    }
355
    
407
    
356
    public void testModuleDisabling() throws Exception {
408
    public void testModuleDisabling() throws Exception {
357
        FileObject dtd = FileUtil.getConfigFile("xml/lookups/NetBeans_org_netbeans_modules_settings_testModuleDisabling/DTD_XML_FooSetting_1_0.instance");
409
        FileObject dtd = FileUtil.getConfigFile("xml/lookups/NetBeans_org_netbeans_modules_settings_testModuleDisabling/DTD_XML_FooSetting_1_0.instance");

Return to bug 227050