|
What this is
Other links
The source code
/*
* Sun Public License Notice
*
* The contents of this file are subject to the Sun Public License
* Version 1.0 (the "License"). You may not use this file except in
* compliance with the License. A copy of the License is available at
* http://www.sun.com/
*
* The Original Code is NetBeans. The Initial Developer of the Original
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2001 Sun
* Microsystems, Inc. All Rights Reserved.
*/
package org.netbeans.lib.jmi.mapping;
import org.netbeans.api.mdr.JMIStreamFactory;
import org.netbeans.lib.jmi.util.ClassFileGenerator;
import org.netbeans.lib.jmi.util.ContainsIterator;
import javax.jmi.model.*;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
/**
*
* @author Dusan Balek
* @version 0.1
*/
public class ClassFileMapper extends GenericMapper {
protected String[] getAncestorNames(GeneralizableElement ge, String postfix, String defaultAncestor) {
Collection supertypes = ge.getSupertypes();
if (supertypes.size() == 0)
return new String [] {defaultAncestor};
String[] ancestors = new String [supertypes.size()];
int i = 0;
for (Iterator it = supertypes.iterator(); it.hasNext(); i++)
ancestors[i] = tagProvider.getTypeFullName((ModelElement) it.next()) + postfix;
return ancestors;
}
protected static Object getTypedValue(String typeName, String value) {
if (value == null)
return null;
else if (typeName.equals("String")) //NOI18N
return value;
else if (typeName.equals("Integer")) //NOI18N
return new Integer(value);
else if (typeName.equals("Boolean")) //NOI18N
return new Integer(value.equalsIgnoreCase("true") ? 1 : 0); //NOI18N
else if (typeName.equals("Double")) //NOI18N
return new Double(value);
else if (typeName.equals("Float")) //NOI18N
return new Float(value);
else if (typeName.equals("Long")) //NOI18N
return new Long(value);
else
return null;
}
public ClassFileMapper(JMIStreamFactory sf) {
this.generator = sf;
}
private final JMIStreamFactory generator;
private OutputStream stream;
protected boolean createStream(List pkg, String fileName) throws IOException {
if (stream != null) {
closeStream();
throw new IllegalStateException("Attempting to create stream before previous stream was closed.");
}
stream = generator.createStream(pkg, fileName, JMIStreamFactory.EXT_CLASS);
return stream != null;
}
protected void closeStream() throws IOException {
if (stream == null)
throw new IllegalStateException("Attempting to close the stream without opening it first.");
OutputStream os = stream;
stream = null;
os.close();
}
protected void classProxyTemplate(javax.jmi.model.MofClass objClass) throws IOException {
new ClassProxyGenerator(objClass).generate();
}
protected void classInstanceTemplate(javax.jmi.model.MofClass objClass) throws IOException {
new ClassInstanceGenerator(objClass).generate();
}
// generates interface for a given association
protected void associationTemplate(Association objAssociation) throws IOException {
new AssociationGenerator(objAssociation).generate();
}
// generates interface for a given package
protected void packageTemplate(MofPackage objPackage) throws IOException {
new PackageGenerator(objPackage).generate();
}
//generates class for an exception
protected void exceptionTemplate(MofException objException) throws IOException {
new ExceptionGenerator(objException).generate();
}
//generates interface for an enumeration
protected void enumerationInterfaceTemplate(EnumerationType objEnumeration) throws IOException {
new EnumerationGenerator(objEnumeration).generate();
}
//generates class for an enumeration
protected void enumerationClassTemplate(EnumerationType objEnumeration) throws IOException {
new EnumerationImplGenerator(objEnumeration).generate();
}
// generates interface for a structure
protected void structureTemplate(StructureType objStructure) throws IOException {
new StructureGenerator(objStructure).generate();
}
abstract class JMIClassFileGenerator extends ClassFileGenerator {
protected JMIClassFileGenerator(String className, String[] interfaces, String superclass, int accessFlags) {
super(className, interfaces, superclass, accessFlags);
}
protected void generate() throws IOException {
generateClassFile(stream);
}
}
class ClassProxyGenerator extends JMIClassFileGenerator {
protected javax.jmi.model.MofClass objClass;
public ClassProxyGenerator(javax.jmi.model.MofClass objClass) {
super(tagProvider.getTypeFullName(objClass) + CLASS_POSTFIX, new String[] {DT_CLASS}, DT_ANY, ACC_PUBLIC | ACC_INTERFACE | ACC_ABSTRACT);
this.objClass = objClass;
}
protected MethodInfo[] generateMethods() throws IOException {
ArrayList methods = new ArrayList();
for (Iterator it = objClass.getContents().iterator(); it.hasNext();) {
ModelElement element = (ModelElement) it.next();
if (element instanceof StructureType) {
ArrayList parameters = new ArrayList();
for (Iterator itt = ((StructureType)element).getContents().iterator(); itt.hasNext();) {
ModelElement field = (ModelElement) itt.next();
if (field instanceof StructureField)
parameters.add(getTypeName2((StructureField) field));
}
methods.add(new MethodInfo("create" + firstUpper(tagProvider.getSubstName(element)), getMethodDescriptor((String[])parameters.toArray(new String[parameters.size()]), tagProvider.getTypeFullName(element)), ACC_PUBLIC | ACC_ABSTRACT)); //NOI18N
}
}
ArrayList allAttributes = new ArrayList();
for (Iterator it = new ContainsIterator(objClass); it.hasNext();) {
ModelElement element = (ModelElement) it.next();
if (element instanceof Attribute) {
Attribute attr = (Attribute) element;
if (attr.getScope().equals(ScopeKindEnum.CLASSIFIER_LEVEL)) {
String attrName = firstUpper(tagProvider.getSubstName(attr));
Classifier attrType = getAttrType(attr);
String attrTypeName = getTypeName(attrType);
if (attr.getMultiplicity().getUpper() == 1) {
String name;
String setterName;
if (attrType instanceof PrimitiveType && attrType.getName().equals("Boolean")) { //NOI18N
if (attrName.substring(0, 2).equals("Is")) name = firstLower(attrName); //NOI18N
else name = "is" + attrName; //NOI18N
setterName = "set" + name.substring(2); //NOI18N
} else {
name = "get" + attrName; //NOI18N
setterName = "set" + attrName; //NOI18N
}
String getterType = attrTypeName;
if (attr.getMultiplicity().getLower() == 1)
getterType = getPrimitiveName(getterType);
methods.add(new MethodInfo(name, getMethodDescriptor(new String[0], getterType), ACC_PUBLIC | ACC_ABSTRACT));
if (attr.isChangeable())
methods.add(new MethodInfo(setterName, getMethodDescriptor(new String[] {getterType}, "void"), ACC_PUBLIC | ACC_ABSTRACT)); //NOI18N
} else if (attr.getMultiplicity().getUpper() != 0)
methods.add(new MethodInfo("get" + attrName, getMethodDescriptor(new String[0], (attr.getMultiplicity().isOrdered() ? DT_ORDERED : DT_MULTIVALUED)), ACC_PUBLIC | ACC_ABSTRACT)); //NOI18N
} else if (!attr.isDerived())
allAttributes.add(getTypeName(attr));
} else if (element instanceof Operation) {
Operation oper = (Operation) element;
if (oper.getScope().equals(ScopeKindEnum.CLASSIFIER_LEVEL)) {
Collection parameters = new ArrayList();
String operType = "void"; //NOI18N
for (Iterator itt = oper.getContents().iterator(); itt.hasNext();) {
ModelElement el = (ModelElement) itt.next();
if (el instanceof Parameter) {
Parameter param = (Parameter) el;
if (param.getDirection().equals(DirectionKindEnum.RETURN_DIR))
operType = getTypeName(param);
else
parameters.add(getTypeName(param) + (param.getDirection().equals(DirectionKindEnum.IN_DIR) ? "" : "[]")); //NOI18N
}
}
MethodInfo mInfo = new MethodInfo(tagProvider.getSubstName(oper), getMethodDescriptor((String[])parameters.toArray(new String [parameters.size()]), operType), ACC_PUBLIC | ACC_ABSTRACT);
Collection exceptions = oper.getExceptions();
short[] declaredExceptions = new short[exceptions.size()];
int i = 0;
for (Iterator itt = exceptions.iterator(); itt.hasNext(); i++)
declaredExceptions[i] = cp.getClass(dotToSlash(tagProvider.getTypeFullName((ModelElement)itt.next())));
mInfo.setDeclaredExceptions(declaredExceptions);
methods.add(mInfo);
}
}
}
if (!objClass.isAbstract()) {
methods.add(new MethodInfo("create" + tagProvider.getSubstName(objClass), getMethodDescriptor(new String[0], tagProvider.getTypeFullName(objClass)), ACC_PUBLIC | ACC_ABSTRACT)); //NOI18N
if (allAttributes.size() > 0)
methods.add(new MethodInfo("create" + tagProvider.getSubstName(objClass), getMethodDescriptor((String[])allAttributes.toArray(new String [allAttributes.size()]), tagProvider.getTypeFullName(objClass)), ACC_PUBLIC | ACC_ABSTRACT)); //NOI18N
}
return (MethodInfo[]) methods.toArray(new MethodInfo[methods.size()]);
}
protected FieldInfo[] generateFields() throws IOException {
return new FieldInfo[0];
}
}
class ClassInstanceGenerator extends JMIClassFileGenerator {
protected javax.jmi.model.MofClass objClass;
public ClassInstanceGenerator(javax.jmi.model.MofClass objClass) {
super(tagProvider.getTypeFullName(objClass), getAncestorNames(objClass, "", DT_INSTANCE), DT_ANY, ACC_PUBLIC | ACC_INTERFACE | ACC_ABSTRACT); //NOI18N
this.objClass = objClass;
}
protected MethodInfo[] generateMethods() throws IOException {
ArrayList methods = new ArrayList();
for (Iterator it = objClass.getContents().iterator(); it.hasNext();) {
ModelElement element = (ModelElement) it.next();
if (element instanceof Feature) {
Feature feature = (Feature) element;
if (feature.getVisibility().equals(VisibilityKindEnum.PUBLIC_VIS)) {
if (feature instanceof Attribute) {
Attribute attr = (Attribute) feature;
String attrName = firstUpper(tagProvider.getSubstName(attr));
Classifier attrType = getAttrType(attr);
String attrTypeName = getTypeName(attrType);
if (attr.getMultiplicity().getUpper() == 1) {
String name;
String setterName;
if (attrType instanceof PrimitiveType && attrType.getName().equals("Boolean")) { //NOI18N
if (attrName.substring(0, 2).equals("Is")) name = firstLower(attrName); //NOI18N
else name = "is" + attrName; //NOI18N
setterName = "set" + name.substring(2); //NOI18N
} else {
name = "get" + attrName; //NOI18N
setterName = "set" + attrName; //NOI18N
}
String getterType = attrTypeName;
if (attr.getMultiplicity().getLower() == 1)
getterType = getPrimitiveName(getterType);
methods.add(new MethodInfo(name, getMethodDescriptor(new String[0], getterType), ACC_PUBLIC | ACC_ABSTRACT));
if (attr.isChangeable())
methods.add(new MethodInfo(setterName, getMethodDescriptor(new String[] {getterType}, "void"), ACC_PUBLIC | ACC_ABSTRACT)); //NOI18N
} else if (attr.getMultiplicity().getUpper() != 0)
methods.add(new MethodInfo("get" + attrName, getMethodDescriptor(new String[0], (attr.getMultiplicity().isOrdered() ? DT_ORDERED : DT_MULTIVALUED)), ACC_PUBLIC | ACC_ABSTRACT)); //NOI18N
} else if (feature instanceof Operation) {
Operation oper = (Operation) feature;
Collection parameters = new ArrayList();
String operType = "void"; //NOI18N
for (Iterator itt = oper.getContents().iterator(); itt.hasNext();) {
ModelElement el = (ModelElement) itt.next();
if (el instanceof Parameter) {
Parameter param = (Parameter) el;
if (param.getDirection().equals(DirectionKindEnum.RETURN_DIR))
operType = getTypeName(param);
else
parameters.add(getTypeName(param) + (param.getDirection().equals(DirectionKindEnum.IN_DIR) ? "" : "[]")); //NOI18N
}
}
MethodInfo mInfo = new MethodInfo(tagProvider.getSubstName(oper), getMethodDescriptor((String[])parameters.toArray(new String [parameters.size()]), operType), ACC_PUBLIC | ACC_ABSTRACT);
Collection exceptions = oper.getExceptions();
short[] declaredExceptions = new short[exceptions.size()];
int i = 0;
for (Iterator itt = exceptions.iterator(); itt.hasNext(); i++)
declaredExceptions[i] = cp.getClass(dotToSlash(tagProvider.getTypeFullName((ModelElement)itt.next())));
mInfo.setDeclaredExceptions(declaredExceptions);
methods.add(mInfo);
} else if (feature instanceof Reference) {
Reference ref = (Reference) feature;
String refName = firstUpper(tagProvider.getSubstName(ref));
String refType = getTypeName(getAttrType(ref));
if (ref.getMultiplicity().getUpper() == 1) {
methods.add(new MethodInfo("get" + refName, getMethodDescriptor(new String[0], refType), ACC_PUBLIC | ACC_ABSTRACT)); //NOI18N
if (ref.isChangeable())
methods.add(new MethodInfo("set" + refName, getMethodDescriptor(new String[] {refType}, "void"), ACC_PUBLIC | ACC_ABSTRACT)); //NOI18N
} else if (ref.getMultiplicity().getUpper() != 0) {
methods.add(new MethodInfo("get" + refName, getMethodDescriptor(new String[0], (ref.getMultiplicity().isOrdered() ? DT_ORDERED : DT_MULTIVALUED)), ACC_PUBLIC | ACC_ABSTRACT)); //NOI18N
}
}
}
}
}
return (MethodInfo[]) methods.toArray(new MethodInfo[methods.size()]);
}
protected FieldInfo[] generateFields() throws IOException {
ArrayList fields = new ArrayList();
for (Iterator it = objClass.getContents().iterator(); it.hasNext();) {
ModelElement element = (ModelElement) it.next();
if (element instanceof Constant) {
DataType type = (DataType) getAttrType((Constant)element);
String value = ((Constant)element).getValue();
FieldInfo fInfo = new FieldInfo(tagProvider.getSubstName(element), getFieldType(getTypeName2((Constant)element)), ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
fInfo.setConstValue(getTypedValue(type.getName(), value));
fields.add(fInfo);
}
}
return (FieldInfo[]) fields.toArray(new FieldInfo[fields.size()]);
}
}
class AssociationGenerator extends JMIClassFileGenerator {
protected Association objAssociation;
public AssociationGenerator(Association objAssociation) {
super(tagProvider.getTypeFullName(objAssociation), new String[] {DT_ASSOCIATION}, DT_ANY, ACC_PUBLIC | ACC_INTERFACE | ACC_ABSTRACT);
this.objAssociation = objAssociation;
}
protected MethodInfo[] generateMethods() throws IOException {
ArrayList methods = new ArrayList();
AssociationEnd[] ends = new AssociationEnd[2];
int i = 0;
for (Iterator it = objAssociation.getContents().iterator(); it.hasNext() && i < 2;) {
Object element = it.next();
if (element instanceof AssociationEnd)
ends[i++] = (AssociationEnd) element;
}
String end1Name = tagProvider.getSubstName(ends[0]);
String end1Class = tagProvider.getTypeFullName(getAttrType(ends[0]));
String end2Name = tagProvider.getSubstName(ends[1]);
String end2Class = tagProvider.getTypeFullName(getAttrType(ends[1]));
// exists(
|
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.