Skip to content

Commit

Permalink
replaced int-flags with enums + code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbien committed Mar 31, 2010
1 parent 2fe517b commit 5cef766
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 84 deletions.
102 changes: 42 additions & 60 deletions src/java/com/sun/gluegen/JavaConfiguration.java
Expand Up @@ -39,6 +39,8 @@

package com.sun.gluegen;

import com.sun.gluegen.JavaEmitter.EmissionStyle;
import com.sun.gluegen.JavaEmitter.MethodAccess;
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
Expand All @@ -47,6 +49,11 @@

import com.sun.gluegen.jgram.*;
import com.sun.gluegen.cgram.types.*;
import java.util.logging.Logger;

import static java.util.logging.Level.*;
import static com.sun.gluegen.JavaEmitter.MethodAccess.*;
import static com.sun.gluegen.JavaEmitter.EmissionStyle.*;

/** Parses and provides access to the contents of .cfg files for the
JavaEmitter. */
Expand All @@ -58,6 +65,8 @@ public class JavaConfiguration {
private String implPackageName;
private String className;
private String implClassName;

protected static final Logger LOG = Logger.getLogger(JavaConfiguration.class.getPackage().getName());

/**
* Root directory for the hierarchy of generated java classes. Default is
Expand Down Expand Up @@ -95,7 +104,7 @@ public class JavaConfiguration {
* (InterfaceAndImpl), only the interface (InterfaceOnly), or only
* the implementation (ImplOnly).
*/
private int emissionStyle = JavaEmitter.ALL_STATIC;
private EmissionStyle emissionStyle = AllStatic;

/**
* List of imports to emit at the head of the output files.
Expand All @@ -115,7 +124,8 @@ public class JavaConfiguration {
*/
private String runtimeExceptionType = "RuntimeException";
private String unsupportedExceptionType = "UnsupportedOperationException";
private Map<String, Integer> accessControl = new HashMap<String, Integer>();

private Map<String, MethodAccess> accessControl = new HashMap<String, MethodAccess>();
private Map<String, TypeInfo> typeInfoMap = new HashMap<String, TypeInfo>();
private Set<String> returnsString = new HashSet<String>();
private Map<String, String> returnedArrayLengths = new HashMap<String, String>();
Expand Down Expand Up @@ -207,16 +217,15 @@ protected final void read(String filename, String linePrefix) throws IOException

if (nestedReads == 0) {
if (allStatic() && implClassName != null) {
throw new IllegalStateException(
"Error in configuration file \"" + filename + "\": Cannot use " +
throw new IllegalStateException("Error in configuration file \"" + filename + "\": Cannot use " +
"directive \"ImplJavaClass\" in conjunction with " +
"\"Style AllStatic\"");
}

if (className == null && (emissionStyle() != JavaEmitter.IMPL_ONLY)) {
if (className == null && (emissionStyle() != ImplOnly)) {
// throw new RuntimeException("Output class name was not specified in configuration file \"" + filename + "\"");
}
if (packageName == null && (emissionStyle() != JavaEmitter.IMPL_ONLY)) {
if (packageName == null && (emissionStyle() != ImplOnly)) {
throw new RuntimeException("Output package name was not specified in configuration file \"" + filename + "\"");
}

Expand Down Expand Up @@ -293,18 +302,18 @@ public boolean tagNativeBinding() {
}

/** Returns the code emission style (constants in JavaEmitter) parsed from the configuration file. */
public int emissionStyle() {
public EmissionStyle emissionStyle() {
return emissionStyle;
}

/** Returns the access control for the emitted Java method. Returns one of JavaEmitter.ACC_PUBLIC, JavaEmitter.ACC_PROTECTED, JavaEmitter.ACC_PRIVATE, or JavaEmitter.ACC_PACKAGE_PRIVATE. */
public int accessControl(String methodName) {
Integer ret = accessControl.get(methodName);
public MethodAccess accessControl(String methodName) {
MethodAccess ret = accessControl.get(methodName);
if (ret != null) {
return ret.intValue();
return ret;
}
// Default access control is public
return JavaEmitter.ACC_PUBLIC;
return PUBLIC;
}

/** Returns the package in which the generated glue code expects to
Expand Down Expand Up @@ -715,7 +724,7 @@ private boolean shouldIgnoreInImpl_Int(String symbol) {
if (!matcher.matches()) {
// Special case as this is most often likely to be the case.
// Unignores are not used very often.
if(unignores.size() == 0) {
if(unignores.isEmpty()) {
if(DEBUG_IGNORES) {
System.err.println("Ignore Impl unignores==0: "+symbol);
}
Expand Down Expand Up @@ -793,20 +802,17 @@ public void addJavaSymbolRename(String origName, String newName) {

/** Returns true if the emission style is AllStatic. */
public boolean allStatic() {
return (emissionStyle == JavaEmitter.ALL_STATIC);
return emissionStyle == AllStatic;
}

/** Returns true if an interface should be emitted during glue code generation. */
public boolean emitInterface() {
return (emissionStyle() == JavaEmitter.INTERFACE_AND_IMPL ||
emissionStyle() == JavaEmitter.INTERFACE_ONLY);
return emissionStyle() == InterfaceAndImpl || emissionStyle() == InterfaceOnly;
}

/** Returns true if an implementing class should be emitted during glue code generation. */
public boolean emitImpl() {
return (emissionStyle() == JavaEmitter.ALL_STATIC ||
emissionStyle() == JavaEmitter.INTERFACE_AND_IMPL ||
emissionStyle() == JavaEmitter.IMPL_ONLY);
return emissionStyle() == AllStatic || emissionStyle() == InterfaceAndImpl || emissionStyle() == ImplOnly;
}

/** Returns a list of Strings which should be emitted as a prologue
Expand Down Expand Up @@ -863,19 +869,11 @@ protected void dispatch(String cmd, StringTokenizer tok, File file, String filen
} else if (cmd.equalsIgnoreCase("TagNativeBinding")) {
tagNativeBinding = readBoolean("TagNativeBinding", tok, filename, lineNo).booleanValue();
} else if (cmd.equalsIgnoreCase("Style")) {
String style = readString("Style", tok, filename, lineNo);
if (style.equalsIgnoreCase("AllStatic")) {
emissionStyle = JavaEmitter.ALL_STATIC;
} else if (style.equalsIgnoreCase("InterfaceAndImpl")) {
emissionStyle = JavaEmitter.INTERFACE_AND_IMPL;
} else if (style.equalsIgnoreCase("InterfaceOnly")) {
emissionStyle = JavaEmitter.INTERFACE_ONLY;
} else if (style.equalsIgnoreCase("ImplOnly")) {
emissionStyle = JavaEmitter.IMPL_ONLY;
} else {
System.err.println("WARNING: Error parsing \"style\" command at line " + lineNo +
" in file \"" + filename + "\"");
}
try{
emissionStyle = EmissionStyle.valueOf(readString("Style", tok, filename, lineNo));
}catch(IllegalArgumentException ex) {
LOG.log(WARNING, "Error parsing \"style\" command at line {0} in file \"{1}\"", new Object[]{lineNo, filename});
}
} else if (cmd.equalsIgnoreCase("AccessControl")) {
readAccessControl(tok, filename, lineNo);
} else if (cmd.equalsIgnoreCase("Import")) {
Expand Down Expand Up @@ -1025,31 +1023,17 @@ protected Class<?> stringToPrimitiveType(String type) throws ClassNotFoundExcept
throw new RuntimeException("Only primitive types are supported here");
}

protected void readAccessControl(StringTokenizer tok, String filename, int lineNo) {
try {
String methodName = tok.nextToken();
String style = tok.nextToken();
int acc = 0;
if (style.equalsIgnoreCase("PUBLIC")) {
acc = JavaEmitter.ACC_PUBLIC;
} else if (style.equalsIgnoreCase("PROTECTED")) {
acc = JavaEmitter.ACC_PROTECTED;
} else if (style.equalsIgnoreCase("PRIVATE")) {
acc = JavaEmitter.ACC_PRIVATE;
} else if (style.equalsIgnoreCase("PACKAGE_PRIVATE")) {
acc = JavaEmitter.ACC_PACKAGE_PRIVATE;
} else if (style.equalsIgnoreCase("PUBLIC_ABSTRACT")) {
acc = JavaEmitter.ACC_PUBLIC_ABSTRACT;
} else {
throw new RuntimeException("Error parsing \"AccessControl\" command at line " + lineNo +
" in file \"" + filename + "\"");
}
accessControl.put(methodName, new Integer(acc));
} catch (Exception e) {
throw new RuntimeException("Error parsing \"AccessControl\" command at line " + lineNo +
" in file \"" + filename + "\"", e);
protected void readAccessControl(StringTokenizer tok, String filename, int lineNo) {
try {
String methodName = tok.nextToken();
String style = tok.nextToken();
MethodAccess access = MethodAccess.valueOf(style.toUpperCase());
accessControl.put(methodName, access);
} catch (Exception e) {
throw new RuntimeException("Error parsing \"AccessControl\" command at line " + lineNo
+ " in file \"" + filename + "\"", e);
}
}
}

protected void readOpaque(StringTokenizer tok, String filename, int lineNo) {
try {
Expand Down Expand Up @@ -1104,8 +1088,7 @@ protected void readExtendedInterfaceSymbols(StringTokenizer tok, String filename
javaFile = new File(tok.nextToken());
javaReader = new BufferedReader(new FileReader(javaFile));
} catch (FileNotFoundException e) {
e.printStackTrace();
return;
throw new RuntimeException(e);
}

JavaLexer lexer = new JavaLexer(javaReader);
Expand All @@ -1117,8 +1100,7 @@ protected void readExtendedInterfaceSymbols(StringTokenizer tok, String filename
try {
parser.compilationUnit();
} catch (Exception e) {
e.printStackTrace();
return;
throw new RuntimeException(e);
}

if(onlyList) {
Expand Down Expand Up @@ -1386,7 +1368,7 @@ protected void doInclude(StringTokenizer tok, File file, String filename, int li

protected void doIncludeAs(StringTokenizer tok, File file, String filename, int lineNo) throws IOException {
try {
StringBuffer linePrefix = new StringBuffer(128);
StringBuilder linePrefix = new StringBuilder(128);
while (tok.countTokens() > 1)
{
linePrefix.append(tok.nextToken());
Expand Down
36 changes: 12 additions & 24 deletions src/java/com/sun/gluegen/JavaEmitter.java
Expand Up @@ -39,20 +39,15 @@

package com.sun.gluegen;

import java.beans.PropertyChangeEvent;
import java.io.*;
import java.util.*;
import java.text.MessageFormat;

import com.sun.gluegen.cgram.types.*;
import java.beans.PropertyChangeListener;
import java.util.logging.ConsoleHandler;
import java.util.logging.Filter;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import javax.swing.plaf.basic.BasicComboBoxUI.PropertyChangeHandler;

import static java.util.logging.Level.*;
import static com.sun.gluegen.JavaEmitter.MethodAccess.*;

// PROBLEMS:
// - what if something returns 'const int *'? Could we
Expand All @@ -78,19 +73,12 @@ public class JavaEmitter implements GlueEmitter {
* (InterfaceAndImpl), only the interface (InterfaceOnly), or only
* the implementation (ImplOnly).
*/
public static final int ALL_STATIC = 1;
public static final int INTERFACE_AND_IMPL = 2;
public static final int INTERFACE_ONLY = 3;
public static final int IMPL_ONLY = 4;
public enum EmissionStyle {AllStatic, InterfaceAndImpl, InterfaceOnly, ImplOnly};

/**
* Access control for emitted Java methods.
*/
public static final int ACC_PUBLIC = 1;
public static final int ACC_PROTECTED = 2;
public static final int ACC_PRIVATE = 3;
public static final int ACC_PACKAGE_PRIVATE = 4;
public static final int ACC_PUBLIC_ABSTRACT = 5;
public enum MethodAccess {PUBLIC, PROTECTED, PRIVATE, PACKAGE_PRIVATE, PUBLIC_ABSTRACT}

private PrintWriter javaWriter; // Emits either interface or, in AllStatic mode, everything
private PrintWriter javaImplWriter; // Only used in non-AllStatic modes for impl class
Expand Down Expand Up @@ -485,9 +473,9 @@ protected void generatePublicEmitters(MethodBinding binding,
return;
}

int accessControl = cfg.accessControl(binding.getName());
MethodAccess accessControl = cfg.accessControl(binding.getName());
// We should not emit anything except public APIs into interfaces
if (signatureOnly && (accessControl != ACC_PUBLIC)) {
if (signatureOnly && (accessControl != PUBLIC)) {
return;
}

Expand Down Expand Up @@ -519,9 +507,9 @@ protected void generatePublicEmitters(MethodBinding binding,
signatureOnly,
cfg);
switch (accessControl) {
case ACC_PUBLIC: emitter.addModifier(JavaMethodBindingEmitter.PUBLIC); break;
case ACC_PROTECTED: emitter.addModifier(JavaMethodBindingEmitter.PROTECTED); break;
case ACC_PRIVATE: emitter.addModifier(JavaMethodBindingEmitter.PRIVATE); break;
case PUBLIC: emitter.addModifier(JavaMethodBindingEmitter.PUBLIC); break;
case PROTECTED: emitter.addModifier(JavaMethodBindingEmitter.PROTECTED); break;
case PRIVATE: emitter.addModifier(JavaMethodBindingEmitter.PRIVATE); break;
default: break; // package-private adds no modifiers
}
if (cfg.allStatic()) {
Expand Down Expand Up @@ -793,7 +781,7 @@ public void emitStructImpl(CompoundType structType,

if (name == null) {
LOG.log(WARNING, "skipping emission of unnamed struct \"{0}\"", structType);
return;
return;
}

if (cfg.shouldIgnoreInInterface(name)) {
Expand Down Expand Up @@ -1610,7 +1598,7 @@ public void emit(PrintWriter w) {
};

String[] accessModifiers = null;
if(cfg.accessControl(cfg.className())==ACC_PUBLIC_ABSTRACT) {
if(cfg.accessControl(cfg.className()) == PUBLIC_ABSTRACT) {
accessModifiers = new String[] { "public", "abstract" };
} else {
accessModifiers = new String[] { "public" };
Expand Down Expand Up @@ -1653,7 +1641,7 @@ public void emit(PrintWriter w) {
}

String[] accessModifiers = null;
if(cfg.accessControl(cfg.implClassName())==ACC_PUBLIC_ABSTRACT) {
if(cfg.accessControl(cfg.implClassName()) == PUBLIC_ABSTRACT) {
accessModifiers = new String[] { "public", "abstract" };
} else {
accessModifiers = new String[] { "public" };
Expand Down

0 comments on commit 5cef766

Please sign in to comment.