Skip to content

Commit

Permalink
Clean up DataSource classes
Browse files Browse the repository at this point in the history
Also add always_trace, which works like trace, but always shows,
regardless of debug-mode.
  • Loading branch information
LadyCailin committed Feb 1, 2019
1 parent 934848e commit e5ff148
Show file tree
Hide file tree
Showing 21 changed files with 277 additions and 157 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ nbproject/
nbactions-provisional-build.xml
nbactions-release-profile.xml
nbactions.xml
true
/logs/
/.logs/
/preferences.*
Expand Down
5 changes: 5 additions & 0 deletions pom.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#Generated by Maven
#Fri Feb 01 14:42:32 CET 2019
version=3.3.4-SNAPSHOT
groupId=com.sk89q
artifactId=commandhelper
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@
<configuration>
<archive>
<addMavenDescriptor>true</addMavenDescriptor>
<pomPropertiesFile>true</pomPropertiesFile>
<pomPropertiesFile>pom.properties</pomPropertiesFile>
<manifest>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ public static void main(String[] args) throws Exception {
Set<Class> apiClasses = new HashSet<>();
apiClasses.addAll(ClassDiscovery.getDefaultInstance().loadClassesWithAnnotation(api.class));
apiClasses.addAll(ClassDiscovery.getDefaultInstance().loadClassesWithAnnotation(typeof.class));
if(apiClasses.isEmpty()) {
// Sanity check
throw new Exception("API classes should not be empty");
}
for(Class c : apiClasses) {
boolean isGetNameExempt = false;
if(c.isInterface()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
*/
public class ClassDiscovery {

private static final boolean IS_DEBUG = java.lang.management.ManagementFactory.getRuntimeMXBean()
.getInputArguments().toString().contains("jdwp");

/**
* The default instance.
*/
Expand Down Expand Up @@ -794,8 +797,15 @@ public Set<Class<?>> loadClassesWithAnnotation(Class<? extends Annotation> annot
try {
set.add(cm.loadClass(loader, initialize));
} catch (NoClassDefFoundError e) {
//Ignore this for now?
//throw new Error("While trying to process " + cm.toString() + ", an error occurred.", e);
if(IS_DEBUG) {
// This is tough. Normally, we really want to ignore this error, but during development, it can be
// a critical error to see to diagnose a very hard to find error. So we compromize here, and only
// print error details out while in debug mode.
System.err.println("While trying to process " + cm.toString() + ", an error occurred. It it"
+ " probably safe to ignore this error, but if you're debugging to figure out why an"
+ " expected class is not showing up, then this is probably why.");
e.printStackTrace(System.err);
}
}
}
return set;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ public CClassType typeof() {
public static CClassType typeof(Mixed that) {
typeof ann = that.getClass().getAnnotation(typeof.class);
if(ann == null) {
throw new IllegalArgumentException();
throw new IllegalArgumentException("Missing typeof annotation for " + that.getClass());
}
return CClassType.get(ann.value());
}
Expand Down
113 changes: 95 additions & 18 deletions src/main/java/com/laytonsmith/core/functions/Debug.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
import com.laytonsmith.PureUtilities.Version;
import com.laytonsmith.annotations.api;
import com.laytonsmith.annotations.noboilerplate;
import com.laytonsmith.annotations.seealso;
import com.laytonsmith.core.ArgumentValidation;
import com.laytonsmith.core.MSVersion;
import com.laytonsmith.core.LogLevel;
import com.laytonsmith.core.MethodScriptFileLocations;
import com.laytonsmith.core.Optimizable;
import com.laytonsmith.core.ParseTree;
import com.laytonsmith.core.Prefs;
import com.laytonsmith.core.Script;
import com.laytonsmith.core.Static;
import com.laytonsmith.core.compiler.FileOptions;
import com.laytonsmith.core.constructs.CArray;
import com.laytonsmith.core.constructs.CString;
import com.laytonsmith.core.constructs.CVoid;
Expand All @@ -24,10 +28,13 @@
import com.laytonsmith.core.exceptions.CRE.CREIOException;
import com.laytonsmith.core.exceptions.CRE.CREPluginInternalException;
import com.laytonsmith.core.exceptions.CRE.CREThrowable;
import com.laytonsmith.core.exceptions.ConfigCompileException;
import com.laytonsmith.core.exceptions.ConfigRuntimeException;
import com.laytonsmith.core.natives.interfaces.Mixed;
import java.io.File;
import java.io.IOException;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;

/**
Expand Down Expand Up @@ -247,7 +254,40 @@ public Mixed exec(Target t, Environment environment, Mixed... args) throws Confi
}

@api
public static class trace extends AbstractFunction {
@seealso(always_trace.class)
public static class trace extends always_trace {

@Override
public Mixed exec(Target t, Environment environment, Mixed... args) throws ConfigRuntimeException {
//TODO: Once Prefs are no longer static, check to see if debug mode is on during compilation, and
//if so, remove this function entirely
if(Prefs.DebugMode()) {
return always_trace.doTrace(t, environment, args);
}
return CVoid.VOID;
}

@Override
public String getName() {
return "trace";
}

@Override
public String docs() {
return "void {ivar} Works like {{function|always_trace}}, but only if debug-mode is enabled in the"
+ " preferences. See {{function|always_trace}} for details of the output.";
}

@Override
public MSVersion since() {
return MSVersion.V3_3_1;
}

}

@api
@seealso(trace.class)
public static class always_trace extends AbstractFunction implements Optimizable {

@Override
public Class<? extends CREThrowable>[] thrown() {
Expand All @@ -266,18 +306,7 @@ public Boolean runAsync() {

@Override
public Mixed exec(Target t, Environment environment, Mixed... args) throws ConfigRuntimeException {
if(args[0] instanceof IVariable) {
if(Prefs.DebugMode()) {
IVariable ivar = (IVariable) args[0];
Mixed val = environment.getEnv(GlobalEnv.class).GetVarList().get(ivar.getVariableName(), t);
StreamUtils.GetSystemOut().println(ivar.getVariableName() + ": " + val.val());
}
return CVoid.VOID;
} else {
throw new CRECastException("Expecting an ivar, but recieved " + args[0].typeof().getSimpleName() + " instead", t);
}
//TODO: Once Prefs are no longer static, check to see if debug mode is on during compilation, and
//if so, remove this function entirely
return doTrace(t, environment, args);
}

@Override
Expand All @@ -287,7 +316,7 @@ public boolean preResolveVariables() {

@Override
public String getName() {
return "trace";
return "always_trace";
}

@Override
Expand All @@ -297,14 +326,62 @@ public Integer[] numArgs() {

@Override
public String docs() {
return "void {ivar} If debug mode is on, outputs debug information about a variable. Unlike debug, this only accepts an ivar; it is a meta function."
+ " The runtime will then take the variable, and output information about it, in a human readable format, including"
+ " the variable's name and value. If debug mode is off, the function is ignored.";
return "void {ivar} Outputs debug information about a variable to standard out. Unlike {{function|debug}},"
+ " this only accepts an ivar; it is a meta function. The runtime will then take the variable,"
+ " and output information about it, in a human readable format, including the variable's"
+ " defined type, actual type, name and value.";
}

@Override
public MSVersion since() {
return MSVersion.V3_3_1;
return MSVersion.V3_3_4;
}

@Override
public ExampleScript[] examples() throws ConfigCompileException {
// We have to hardcode the output here, because otherwise it just prints to stdout, and we don't curently
// capture that.
return new ExampleScript[]{
new ExampleScript("Basic usage with auto type", "@m = 2;\n"
+ "always_trace(@m);", "auto (actual type ms.lang.int) @m: 2"),
new ExampleScript("With defined type", "int @i = 1;\n"
+ "always_trace(@i);", "ms.lang.int (actual type ms.lang.int) @i: 1"),
new ExampleScript("With subtype", "number @n = 2.0;\n"
+ "always_trace(@n);", "ms.lang.number (actual type ms.lang.double) @n: 2.0")
};
}

public static CVoid doTrace(Target t, Environment environment, Mixed... args) {
if(args[0] instanceof IVariable) {
IVariable ivar = environment.getEnv(GlobalEnv.class).GetVarList()
.get(((IVariable) args[0]).getVariableName(), t);
Mixed val = ivar.ival();
StreamUtils.GetSystemOut().println(ivar.getDefinedType() + " (actual type " + val.typeof() + ") "
+ ivar.getVariableName() + ": " + val.val());
return CVoid.VOID;
} else {
throw new CRECastException("Expecting an ivar, but recieved " + args[0].typeof().getSimpleName()
+ " instead", t);
}
}

@Override
public Set<OptimizationOption> optimizationOptions() {
return EnumSet.of(OptimizationOption.OPTIMIZE_DYNAMIC);
}

@Override
public ParseTree optimizeDynamic(Target t, List<ParseTree> children, FileOptions fileOptions)
throws ConfigCompileException, ConfigRuntimeException {
if(children.size() != 1) {
throw new ConfigCompileException(getName() + " expects 1 parameter, but " + children.size()
+ " were provided.", t);
}
ParseTree child = children.get(0);
if(!(child.getData() instanceof IVariable)) {
throw new ConfigCompileException(getName() + " can only accept an ivar as the argument.", t);
}
return null;
}

}
Expand Down
Loading

0 comments on commit e5ff148

Please sign in to comment.