You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2026/02/11 15:01:10 UTC

(skywalking) 11/21: Add comprehensive V1 vs V2 bytecode comparison tests

This is an automated email from the ASF dual-hosted git repository.

wusheng pushed a commit to branch oal-v2
in repository https://gitbox.apache.org/repos/asf/skywalking.git

commit acfee928c1a0512f30af7127c428d52b87a8ef34
Author: Wu Sheng <wu...@foxmail.com>
AuthorDate: Tue Feb 10 10:51:20 2026 +0800

    Add comprehensive V1 vs V2 bytecode comparison tests
    
    This commit adds tests to verify that OAL V2 generates byte-for-byte
    identical bytecode compared to V1 for all 8 production OAL scripts
    (620 metrics total).
    
    Test Coverage:
    - core.oal: 63 metrics
    - java-agent.oal: 32 metrics
    - browser.oal: 41 metrics
    - mesh.oal: 14 metrics
    - tcp.oal: 12 metrics
    - dotnet-agent.oal: 9 metrics
    - ebpf.oal: 296 metrics
    - cilium.oal: 153 metrics
    
    Key changes:
    - Add V1ClassGenerator and V2ClassGenerator for isolated bytecode generation
    - Add V1V2ComparisonTest for detailed bytecode comparison
    - Add AllOALScriptsComparisonTest for comprehensive OAL script validation
    - Fix EntranceMethodV2.argsExpressions type (List<String> -> List<Object>)
      to support both string literals and FilterExpressionV2 objects
    - Fix MetricDefinitionEnricher to add FilterExpressionV2 objects for
      @Expression annotated parameters (matching V1 behavior)
    - Register all required source scopes (JVM, CLR, Browser) and decorators
      (ServiceDecorator, K8SServiceDecorator) for complete test coverage
    
    These tests are useful for debugging e2e failures - if V2 generates
    different bytecode than V1, it will be caught here with detailed diffs.
    
    Co-Authored-By: Claude Opus 4.5 <no...@anthropic.com>
---
 .../org/apache/skywalking/oal/rt/OALKernel.java    | 135 +++++
 .../org/apache/skywalking/oal/rt/OALRuntime.java   |  28 +
 .../oal/rt/output/AllDispatcherContext.java        |  30 +
 .../oal/rt/output/DispatcherContext.java           |  36 ++
 .../oal/rt/parser/AggregationFuncStmt.java         |  74 +++
 .../skywalking/oal/rt/parser/AnalysisResult.java   | 146 +++++
 .../apache/skywalking/oal/rt/parser/Argument.java  |  43 ++
 .../oal/rt/parser/ConditionExpression.java         |  68 +++
 .../skywalking/oal/rt/parser/DataColumn.java       |  44 ++
 .../skywalking/oal/rt/parser/DeepAnalysis.java     | 163 ++++++
 .../oal/rt/parser/DisableCollection.java           |  32 ++
 .../skywalking/oal/rt/parser/EntryMethod.java      |  73 +++
 .../skywalking/oal/rt/parser/Expression.java       |  38 ++
 .../skywalking/oal/rt/parser/FilterMatchers.java   |  95 +++
 .../skywalking/oal/rt/parser/FilterStmts.java      |  54 ++
 .../apache/skywalking/oal/rt/parser/FromStmt.java  |  48 ++
 .../skywalking/oal/rt/parser/MetricsHolder.java    |  64 +++
 .../skywalking/oal/rt/parser/OALListener.java      | 310 ++++++++++
 .../skywalking/oal/rt/parser/OALScripts.java       |  34 ++
 .../oal/rt/parser/PersistenceColumns.java          |  70 +++
 .../skywalking/oal/rt/parser/PersistenceField.java |  39 ++
 .../skywalking/oal/rt/parser/ScriptParser.java     |  73 +++
 .../skywalking/oal/rt/parser/SourceColumn.java     | 113 ++++
 .../oal/rt/parser/SourceColumnsFactory.java        |  38 ++
 .../skywalking/oal/rt/util/ClassMethodUtil.java    |  84 +++
 .../skywalking/oal/rt/util/OALClassGenerator.java  | 634 +++++++++++++++++++++
 .../skywalking/oal/rt/util/TypeCastUtil.java       |  43 ++
 .../skywalking/oal/v2/generator/CodeGenModel.java  |  28 +-
 .../oal/v2/generator/MetricDefinitionEnricher.java |  26 +-
 .../code-templates-v2/dispatcher/doMetrics.ftl     |  16 +-
 .../code-templates-v2/metrics/serialize.ftl        |  46 +-
 .../v2/generator/AllOALScriptsComparisonTest.java  | 361 ++++++++++++
 .../oal/v2/generator/DiagnosticTest.java           | 122 ++++
 .../oal/v2/generator/OALClassGeneratorV2Test.java  |  12 +-
 .../oal/v2/generator/V1ClassGenerator.java         | 253 ++++++++
 .../oal/v2/generator/V1V2ComparisonTest.java       | 551 ++++++++++++++++++
 .../oal/v2/generator/V2ClassGenerator.java         | 250 ++++++++
 37 files changed, 4228 insertions(+), 46 deletions(-)

diff --git a/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/OALKernel.java b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/OALKernel.java
new file mode 100644
index 0000000000..3f1c3758c5
--- /dev/null
+++ b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/OALKernel.java
@@ -0,0 +1,135 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oal.rt;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.skywalking.oal.rt.parser.OALScripts;
+import org.apache.skywalking.oal.rt.parser.ScriptParser;
+import org.apache.skywalking.oal.rt.util.OALClassGenerator;
+import org.apache.skywalking.oap.server.core.analysis.DispatcherDetectorListener;
+import org.apache.skywalking.oap.server.core.analysis.StreamAnnotationListener;
+import org.apache.skywalking.oap.server.core.oal.rt.OALCompileException;
+import org.apache.skywalking.oap.server.core.oal.rt.OALDefine;
+import org.apache.skywalking.oap.server.core.oal.rt.OALEngine;
+import org.apache.skywalking.oap.server.core.storage.StorageBuilderFactory;
+import org.apache.skywalking.oap.server.core.storage.StorageException;
+
+import org.apache.skywalking.oap.server.library.module.ModuleStartException;
+import org.apache.skywalking.oap.server.library.util.ResourceUtils;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.Reader;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * OAL Runtime is the class generation engine, which load the generated classes from OAL scrip definitions. This runtime
+ * is loaded dynamically.
+ */
+@Slf4j
+public class OALKernel implements OALEngine {
+
+    private static boolean IS_RT_TEMP_FOLDER_INIT_COMPLETED = false;
+
+    private OALClassGenerator oalClassGenerator;
+
+    private StreamAnnotationListener streamAnnotationListener;
+    private DispatcherDetectorListener dispatcherDetectorListener;
+    private final List<Class> metricsClasses;
+    private final List<Class> dispatcherClasses;
+
+    private final OALDefine oalDefine;
+
+    public OALKernel(OALDefine define) {
+        oalDefine = define;
+        metricsClasses = new ArrayList<>();
+        dispatcherClasses = new ArrayList<>();
+
+        oalClassGenerator = new OALClassGenerator(define);
+    }
+
+    @Override
+    public void setStreamListener(StreamAnnotationListener listener) throws ModuleStartException {
+        this.streamAnnotationListener = listener;
+    }
+
+    @Override
+    public void setDispatcherListener(DispatcherDetectorListener listener) throws ModuleStartException {
+        dispatcherDetectorListener = listener;
+    }
+
+    @Override
+    public void setStorageBuilderFactory(final StorageBuilderFactory factory) {
+        oalClassGenerator.setStorageBuilderFactory(factory);
+    }
+
+    @Override
+    public void start(ClassLoader currentClassLoader) throws ModuleStartException, OALCompileException {
+        if (!IS_RT_TEMP_FOLDER_INIT_COMPLETED) {
+            oalClassGenerator.prepareRTTempFolder();
+            IS_RT_TEMP_FOLDER_INIT_COMPLETED = true;
+        }
+
+        Reader read;
+        oalClassGenerator.setCurrentClassLoader(currentClassLoader);
+
+        try {
+            read = ResourceUtils.read(oalDefine.getConfigFile());
+        } catch (FileNotFoundException e) {
+            throw new ModuleStartException("Can't locate " + oalDefine.getConfigFile(), e);
+        }
+
+        OALScripts oalScripts;
+        try {
+            ScriptParser scriptParser = ScriptParser.createFromFile(read, oalDefine.getSourcePackage());
+            oalScripts = scriptParser.parse();
+        } catch (IOException e) {
+            throw new ModuleStartException("OAL script parse analysis failure.", e);
+        }
+
+        oalClassGenerator.generateClassAtRuntime(oalScripts, metricsClasses, dispatcherClasses);
+    }
+
+    @Override
+    public void notifyAllListeners() throws ModuleStartException {
+        for (Class metricsClass : metricsClasses) {
+            try {
+                streamAnnotationListener.notify(metricsClass);
+            } catch (StorageException e) {
+                throw new ModuleStartException(e.getMessage(), e);
+            }
+        }
+        for (Class dispatcherClass : dispatcherClasses) {
+            try {
+                dispatcherDetectorListener.addIfAsSourceDispatcher(dispatcherClass);
+            } catch (Exception e) {
+                throw new ModuleStartException(e.getMessage(), e);
+            }
+        }
+    }
+
+    protected List<Class> getMetricsClasses() {
+        return metricsClasses;
+    }
+
+    protected List<Class> getDispatcherClasses() {
+        return dispatcherClasses;
+    }
+}
diff --git a/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/OALRuntime.java b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/OALRuntime.java
new file mode 100644
index 0000000000..a0721c5de0
--- /dev/null
+++ b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/OALRuntime.java
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oal.rt;
+
+import org.apache.skywalking.oap.server.core.oal.rt.OALDefine;
+
+public class OALRuntime extends OALKernel {
+    public OALRuntime(OALDefine define) {
+        super(define);
+    }
+
+}
diff --git a/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/output/AllDispatcherContext.java b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/output/AllDispatcherContext.java
new file mode 100644
index 0000000000..15f04d3ec3
--- /dev/null
+++ b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/output/AllDispatcherContext.java
@@ -0,0 +1,30 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oal.rt.output;
+
+import java.util.HashMap;
+import java.util.Map;
+import lombok.Getter;
+import lombok.Setter;
+
+@Getter
+@Setter
+public class AllDispatcherContext {
+    private Map<String, DispatcherContext> allContext = new HashMap<>();
+}
diff --git a/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/output/DispatcherContext.java b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/output/DispatcherContext.java
new file mode 100644
index 0000000000..4430fb142e
--- /dev/null
+++ b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/output/DispatcherContext.java
@@ -0,0 +1,36 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oal.rt.output;
+
+import java.util.ArrayList;
+import java.util.List;
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.skywalking.oal.rt.parser.AnalysisResult;
+
+@Getter
+@Setter
+public class DispatcherContext {
+
+    private String sourcePackage;
+    private String source;
+    private String packageName;
+    private List<AnalysisResult> metrics = new ArrayList<>();
+    private String sourceDecorator;
+}
diff --git a/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/AggregationFuncStmt.java b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/AggregationFuncStmt.java
new file mode 100644
index 0000000000..f4f2a8dbd5
--- /dev/null
+++ b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/AggregationFuncStmt.java
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oal.rt.parser;
+
+import java.util.LinkedList;
+import java.util.List;
+import lombok.Getter;
+import lombok.Setter;
+
+@Setter
+@Getter
+public class AggregationFuncStmt {
+    private String aggregationFunctionName;
+
+    private List<ConditionExpression> funcConditionExpressions;
+
+    private int funcConditionExpressionGetIdx = 0;
+
+    private List<Argument> funcArgs;
+
+    private int argGetIdx = 0;
+
+    private String nextArgCast = null;
+
+    public void addFuncConditionExpression(ConditionExpression conditionExpression) {
+        if (funcConditionExpressions == null) {
+            funcConditionExpressions = new LinkedList<>();
+        }
+        funcConditionExpressions.add(conditionExpression);
+    }
+
+    public ConditionExpression getNextFuncConditionExpression() {
+        return funcConditionExpressions.get(funcConditionExpressionGetIdx++);
+    }
+
+    public void addFuncArg(Argument argument) {
+        if (funcArgs == null) {
+            funcArgs = new LinkedList<>();
+        }
+        if (nextArgCast != null) {
+            argument.setCastType(nextArgCast);
+            nextArgCast = null;
+        }
+        funcArgs.add(argument);
+    }
+
+    public Argument getLastArgument() {
+        return funcArgs.get(funcArgs.size() - 1);
+    }
+
+    public Argument getNextFuncArg() {
+        return funcArgs.get(argGetIdx++);
+    }
+
+    public boolean hasNextArg() {
+        return argGetIdx < funcArgs.size();
+    }
+}
diff --git a/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/AnalysisResult.java b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/AnalysisResult.java
new file mode 100644
index 0000000000..b6d57d57b4
--- /dev/null
+++ b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/AnalysisResult.java
@@ -0,0 +1,146 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oal.rt.parser;
+
+import java.util.ArrayList;
+import java.util.List;
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.skywalking.oap.server.core.storage.type.StorageDataComplexObject;
+
+/**
+ * OAL analysis result.
+ */
+@Getter
+@Setter
+public class AnalysisResult {
+    /**
+     * Variable name of one OAL expression.
+     */
+    private String varName;
+    /**
+     * Generated metric name.
+     */
+    private String metricsName;
+    /**
+     * Package name of generated metric class.
+     */
+    private String metricsClassPackage;
+    /**
+     * Table name for the storage.
+     */
+    private String tableName;
+    /**
+     * The package name of source class from {@link org.apache.skywalking.oap.server.core.oal.rt.OALDefine}
+     */
+    private String sourcePackage;
+    /**
+     * The class name of generated metric class.
+     */
+    private String metricsClassName;
+    /**
+     * The raw parsed result of from statement.
+     */
+    private FromStmt from = new FromStmt();
+    /**
+     * The raw parsed result of filter statements.
+     */
+    private FilterStmts filters = new FilterStmts();
+    /**
+     * The raw parsed result of aggregation function with arguments.
+     */
+    private AggregationFuncStmt aggregationFuncStmt = new AggregationFuncStmt();
+
+    /**
+     * Generated through {@link #aggregationFuncStmt}
+     */
+    private EntryMethod entryMethod;
+
+    /**
+     * Persistent columns are generated by {@link org.apache.skywalking.oap.server.core.storage.annotation.Column}
+     * definition of {@link org.apache.skywalking.oap.server.core.analysis.metrics.annotation.MetricsFunction}.
+     */
+    private List<DataColumn> persistentFields;
+    /**
+     * Fields of metric class are generated by the fields annotated {@link org.apache.skywalking.oap.server.core.source.ScopeDefaultColumn.DefinedByField}
+     * and class level definition through {@link org.apache.skywalking.oap.server.core.source.ScopeDefaultColumn.VirtualColumnDefinition}
+     * in the {@link org.apache.skywalking.oap.server.core.source.Source}
+     */
+    private List<SourceColumn> fieldsFromSource;
+    /**
+     * Fields generated by {@link #fieldsFromSource} and {@link #persistentFields}. These fields are used in final
+     * persistence.
+     */
+    private PersistenceColumns serializeFields;
+
+    private String sourceDecorator;
+
+    public void addPersistentField(String fieldName, String columnName, Class<?> type) {
+        if (persistentFields == null) {
+            persistentFields = new ArrayList<>();
+        }
+        DataColumn dataColumn = new DataColumn(fieldName, columnName, type);
+        persistentFields.add(dataColumn);
+    }
+
+    public void generateSerializeFields() {
+        serializeFields = new PersistenceColumns();
+        for (SourceColumn sourceColumn : fieldsFromSource) {
+            String type = sourceColumn.getType().getSimpleName();
+            switch (type) {
+                case "int":
+                    serializeFields.addIntField(sourceColumn.getFieldName());
+                    break;
+                case "double":
+                    serializeFields.addDoubleField(sourceColumn.getFieldName());
+                    break;
+                case "String":
+                    serializeFields.addStringField(sourceColumn.getFieldName());
+                    break;
+                case "long":
+                    serializeFields.addLongField(sourceColumn.getFieldName());
+                    break;
+                default:
+                    throw new IllegalStateException(
+                        "Unexpected field type [" + type + "] of source sourceColumn [" + sourceColumn
+                            .getFieldName() + "]");
+            }
+        }
+
+        for (DataColumn column : persistentFields) {
+            final Class<?> columnType = column.getType();
+
+            if (columnType.equals(int.class)) {
+                serializeFields.addIntField(column.getFieldName());
+            } else if (columnType.equals(double.class)) {
+                serializeFields.addDoubleField(column.getFieldName());
+            } else if (columnType.equals(String.class)) {
+                serializeFields.addStringField(column.getFieldName());
+            } else if (columnType.equals(long.class)) {
+                serializeFields.addLongField(column.getFieldName());
+            } else if (StorageDataComplexObject.class.isAssignableFrom(columnType)) {
+                serializeFields.addObjectField(column.getFieldName(), columnType.getName());
+            } else {
+                throw new IllegalStateException(
+                    "Unexpected field type [" + columnType.getSimpleName() + "] of persistence column [" + column
+                        .getFieldName() + "]");
+            }
+        }
+    }
+}
diff --git a/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/Argument.java b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/Argument.java
new file mode 100644
index 0000000000..fe1954dc38
--- /dev/null
+++ b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/Argument.java
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oal.rt.parser;
+
+import java.util.List;
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import lombok.Setter;
+
+/**
+ * Function argument.
+ */
+@Getter
+@RequiredArgsConstructor
+public class Argument {
+
+    private final int type;
+
+    private final List<String> text;
+
+    @Setter
+    private String castType;
+
+    public void addText(String text) {
+        this.text.add(text);
+    }
+}
diff --git a/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/ConditionExpression.java b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/ConditionExpression.java
new file mode 100644
index 0000000000..3e611ba0fd
--- /dev/null
+++ b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/ConditionExpression.java
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oal.rt.parser;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.LinkedList;
+import java.util.List;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.Setter;
+
+@Getter
+@Setter
+@NoArgsConstructor
+public class ConditionExpression {
+    // original from script
+    private String expressionType;
+    private List<String> attributes = new ArrayList<>();
+    private String value;
+    private List<String> values;
+    private boolean number;
+    private String castType;
+
+    public ConditionExpression(final String expressionType, final String attributes, final String value) {
+        this.expressionType = expressionType;
+        this.attributes = Arrays.asList(attributes.split("\\."));
+        this.value = value;
+    }
+
+    public void addValue(String value) {
+        if (values != null) {
+            values.add(value);
+        } else {
+            this.value = value;
+        }
+    }
+
+    public void isNumber() {
+        number = true;
+    }
+
+    public void enterMultiConditionValue() {
+        values = new LinkedList<>();
+    }
+
+    public void exitMultiConditionValue() {
+        value = number ?
+            "new long[]{" + String.join(",", values) + "}" :
+            "new Object[]{" + String.join(",", values) + "}";
+    }
+}
diff --git a/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/DataColumn.java b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/DataColumn.java
new file mode 100644
index 0000000000..8a43f9b6eb
--- /dev/null
+++ b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/DataColumn.java
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oal.rt.parser;
+
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.skywalking.oal.rt.util.ClassMethodUtil;
+
+@Getter
+@Setter
+public class DataColumn {
+    private String fieldName;
+    private String columnName;
+    private Class<?> type;
+    private String typeName;
+    private String fieldSetter;
+    private String fieldGetter;
+
+    public DataColumn(String fieldName, String columnName, Class<?> type) {
+        this.fieldName = fieldName;
+        this.columnName = columnName;
+        this.type = type;
+        this.typeName = type.getName();
+
+        this.fieldGetter = ClassMethodUtil.toGetMethod(fieldName);
+        this.fieldSetter = ClassMethodUtil.toSetMethod(fieldName);
+    }
+}
diff --git a/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/DeepAnalysis.java b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/DeepAnalysis.java
new file mode 100644
index 0000000000..375158493d
--- /dev/null
+++ b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/DeepAnalysis.java
@@ -0,0 +1,163 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oal.rt.parser;
+
+import org.apache.skywalking.oal.rt.util.ClassMethodUtil;
+import org.apache.skywalking.oal.rt.util.TypeCastUtil;
+import org.apache.skywalking.oap.server.core.analysis.metrics.Metrics;
+import org.apache.skywalking.oap.server.core.analysis.metrics.annotation.Arg;
+import org.apache.skywalking.oap.server.core.analysis.metrics.annotation.ConstOne;
+import org.apache.skywalking.oap.server.core.analysis.metrics.annotation.DefaultValue;
+import org.apache.skywalking.oap.server.core.analysis.metrics.annotation.Entrance;
+import org.apache.skywalking.oap.server.core.analysis.metrics.annotation.SourceFrom;
+import org.apache.skywalking.oap.server.core.storage.annotation.Column;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.lang.reflect.Parameter;
+import java.util.List;
+import static java.util.Objects.isNull;
+
+public class DeepAnalysis {
+    public AnalysisResult analysis(AnalysisResult result) {
+        // 1. Set sub package name by source.metrics
+        Class<? extends Metrics> metricsClass = MetricsHolder.find(result.getAggregationFuncStmt().getAggregationFunctionName());
+        String metricsClassSimpleName = metricsClass.getSimpleName();
+
+        result.setMetricsClassName(metricsClassSimpleName);
+
+        // Optional for filter
+        List<ConditionExpression> expressions = result.getFilters().getFilterExpressionsParserResult();
+        if (expressions != null && expressions.size() > 0) {
+            for (ConditionExpression expression : expressions) {
+                final FilterMatchers.MatcherInfo matcherInfo = FilterMatchers.INSTANCE.find(
+                    expression.getExpressionType());
+
+                final String getter = matcherInfo.isBooleanType()
+                    ? ClassMethodUtil.toIsMethod(expression.getAttributes())
+                    : ClassMethodUtil.toGetMethod(expression.getAttributes());
+
+                final Expression filterExpression = new Expression();
+                filterExpression.setExpressionObject(matcherInfo.getMatcher().getName());
+                filterExpression.setLeft(TypeCastUtil.withCast(expression.getCastType(), "source." + getter));
+                filterExpression.setRight(expression.getValue());
+                result.getFilters().addFilterExpressions(filterExpression);
+            }
+        }
+
+        // 3. Find Entrance method of this metrics
+        Class<?> c = metricsClass;
+        Method entranceMethod = null;
+        SearchEntrance:
+        while (!c.equals(Object.class)) {
+            for (Method method : c.getMethods()) {
+                Entrance annotation = method.getAnnotation(Entrance.class);
+                if (annotation != null) {
+                    entranceMethod = method;
+                    break SearchEntrance;
+                }
+            }
+            c = c.getSuperclass();
+        }
+        if (entranceMethod == null) {
+            throw new IllegalArgumentException("Can't find Entrance method in class: " + metricsClass.getName());
+        }
+        EntryMethod entryMethod = new EntryMethod();
+        result.setEntryMethod(entryMethod);
+        entryMethod.setMethodName(entranceMethod.getName());
+
+        // 4. Use parameter's annotation of entrance method to generate aggregation entrance.
+        for (Parameter parameter : entranceMethod.getParameters()) {
+            Class<?> parameterType = parameter.getType();
+            Annotation[] parameterAnnotations = parameter.getAnnotations();
+            if (parameterAnnotations == null || parameterAnnotations.length == 0) {
+                throw new IllegalArgumentException(
+                    "Entrance method:" + entranceMethod + " doesn't include the annotation.");
+            }
+            Annotation annotation = parameterAnnotations[0];
+            if (annotation instanceof SourceFrom) {
+                entryMethod.addArg(
+                    parameterType,
+                    TypeCastUtil.withCast(
+                        result.getFrom().getSourceCastType(),
+                        "source." + ClassMethodUtil.toGetMethod(result.getFrom().getSourceAttribute())
+                    )
+                );
+            } else if (annotation instanceof ConstOne) {
+                entryMethod.addArg(parameterType, "1");
+            } else if (annotation instanceof org.apache.skywalking.oap.server.core.analysis.metrics.annotation.Expression) {
+                if (isNull(result.getAggregationFuncStmt().getFuncConditionExpressions())
+                    || result.getAggregationFuncStmt().getFuncConditionExpressions().isEmpty()) {
+                    throw new IllegalArgumentException(
+                        "Entrance method:" + entranceMethod + " argument can't find funcParamExpression.");
+                } else {
+                    ConditionExpression expression = result.getAggregationFuncStmt().getNextFuncConditionExpression();
+                    final FilterMatchers.MatcherInfo matcherInfo = FilterMatchers.INSTANCE.find(
+                        expression.getExpressionType());
+
+                    final String getter = matcherInfo.isBooleanType()
+                        ? ClassMethodUtil.toIsMethod(expression.getAttributes())
+                        : ClassMethodUtil.toGetMethod(expression.getAttributes());
+
+                    final Expression argExpression = new Expression();
+                    argExpression.setRight(expression.getValue());
+                    argExpression.setExpressionObject(matcherInfo.getMatcher().getName());
+                    argExpression.setLeft(TypeCastUtil.withCast(expression.getCastType(), "source." + getter));
+
+                    entryMethod.addArg(argExpression);
+                }
+            } else if (annotation instanceof Arg) {
+                entryMethod.addArg(parameterType, result.getAggregationFuncStmt().getNextFuncArg());
+            } else if (annotation instanceof DefaultValue) {
+                if (result.getAggregationFuncStmt().hasNextArg()) {
+                    entryMethod.addArg(parameterType, result.getAggregationFuncStmt().getNextFuncArg());
+                } else {
+                    entryMethod.addArg(parameterType, ((DefaultValue) annotation).value());
+                }
+            } else {
+                throw new IllegalArgumentException(
+                    "Entrance method:" + entranceMethod + " doesn't the expected annotation.");
+            }
+        }
+
+        // 5. Get all column declared in MetricsHolder class.
+        c = metricsClass;
+        while (!c.equals(Object.class)) {
+            for (Field field : c.getDeclaredFields()) {
+                Column column = field.getAnnotation(Column.class);
+                if (column != null) {
+                    result.addPersistentField(
+                        field.getName(),
+                        column.name(),
+                        field.getType());
+                }
+            }
+            c = c.getSuperclass();
+        }
+
+        // 6. Based on Source, generate default columns
+        List<SourceColumn> columns = SourceColumnsFactory.getColumns(result.getFrom().getSourceName());
+        result.setFieldsFromSource(columns);
+
+        result.generateSerializeFields();
+
+        return result;
+    }
+
+}
diff --git a/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/DisableCollection.java b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/DisableCollection.java
new file mode 100644
index 0000000000..517ec1fcbc
--- /dev/null
+++ b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/DisableCollection.java
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oal.rt.parser;
+
+import java.util.ArrayList;
+import java.util.List;
+import lombok.Getter;
+
+@Getter
+public class DisableCollection {
+    private List<String> allDisableSources = new ArrayList<>();
+
+    public void add(String source) {
+        allDisableSources.add(source);
+    }
+}
diff --git a/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/EntryMethod.java b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/EntryMethod.java
new file mode 100644
index 0000000000..b3eda815ec
--- /dev/null
+++ b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/EntryMethod.java
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oal.rt.parser;
+
+import java.util.ArrayList;
+import java.util.List;
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.skywalking.oal.rt.util.ClassMethodUtil;
+import org.apache.skywalking.oal.rt.util.TypeCastUtil;
+
+@Getter
+@Setter
+public class EntryMethod {
+    static final int LITERAL_TYPE = 1;
+    static final int ATTRIBUTE_EXP_TYPE = 2;
+    static final int EXPRESSION_TYPE = 3;
+
+    private String methodName;
+    private List<Integer> argTypes = new ArrayList<>();
+    private List<Object> argsExpressions = new ArrayList<>();
+
+    void addArg(Class<?> parameterType, Argument arg) {
+        if (arg.getType() == LITERAL_TYPE) {
+            // As literal type, there is always one element.
+            addArg(parameterType, arg.getType(), arg.getText().get(0));
+            return;
+        }
+        addArg(parameterType, arg.getType(), parameterType.equals(boolean.class) ?
+            TypeCastUtil.withCast(arg.getCastType(), "source." + ClassMethodUtil.toIsMethod(arg.getText()))
+            :
+            TypeCastUtil.withCast(arg.getCastType(), "source." + ClassMethodUtil.toGetMethod(arg.getText())));
+    }
+
+    void addArg(Class<?> parameterType, String expression) {
+        addArg(parameterType, LITERAL_TYPE, expression);
+    }
+
+    void addArg(Expression expression) {
+        argTypes.add(EXPRESSION_TYPE);
+        argsExpressions.add(expression);
+    }
+
+    private void addArg(Class<?> parameterType, int type, String expression) {
+        if (parameterType.equals(int.class)) {
+            expression = "(int)(" + expression + ")";
+        } else if (parameterType.equals(long.class)) {
+            expression = "(long)(" + expression + ")";
+        } else if (parameterType.equals(double.class)) {
+            expression = "(double)(" + expression + ")";
+        } else if (parameterType.equals(float.class)) {
+            expression = "(float)(" + expression + ")";
+        }
+        argTypes.add(type);
+        argsExpressions.add(expression);
+    }
+}
diff --git a/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/Expression.java b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/Expression.java
new file mode 100644
index 0000000000..aed15128d4
--- /dev/null
+++ b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/Expression.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oal.rt.parser;
+
+import lombok.Getter;
+import lombok.Setter;
+
+@Getter
+public class Expression {
+    @Setter
+    private String expressionObject;
+    private String left;
+    private String right;
+
+    public void setLeft(String left) {
+        this.left = left;
+    }
+
+    public void setRight(String right) {
+        this.right = right;
+    }
+}
diff --git a/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/FilterMatchers.java b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/FilterMatchers.java
new file mode 100644
index 0000000000..27e8ae1acb
--- /dev/null
+++ b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/FilterMatchers.java
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oal.rt.parser;
+
+import com.google.common.reflect.ClassPath;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.skywalking.oap.server.core.analysis.metrics.annotation.BooleanValueFilterMatcher;
+import org.apache.skywalking.oap.server.core.analysis.metrics.annotation.FilterMatcher;
+
+@SuppressWarnings("UnstableApiUsage")
+public enum FilterMatchers {
+    INSTANCE;
+
+    FilterMatchers() {
+        try {
+            init();
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    private final Map<String, MatcherInfo> matchersKeyedByType = new HashMap<>();
+
+    private void init() throws IOException {
+        final ClassPath classpath = ClassPath.from(FilterMatchers.class.getClassLoader());
+        final Set<ClassPath.ClassInfo> classes = classpath.getTopLevelClassesRecursive("org.apache.skywalking");
+        for (ClassPath.ClassInfo classInfo : classes) {
+            final Class<?> clazz = classInfo.load();
+
+            final FilterMatcher plainFilterMatcher = clazz.getAnnotation(FilterMatcher.class);
+            final BooleanValueFilterMatcher booleanFilterMatcher = clazz.getAnnotation(BooleanValueFilterMatcher.class);
+            if (plainFilterMatcher != null && booleanFilterMatcher != null) {
+                throw new IllegalStateException(
+                    "A matcher class can not be annotated with both @FilterMatcher and @BooleanValueFilterMatcher"
+                );
+            }
+
+            if (plainFilterMatcher != null) {
+                for (final String type : plainFilterMatcher.value()) {
+                    matchersKeyedByType.put(type, new MatcherInfo(clazz, false));
+                }
+                if (plainFilterMatcher.value().length == 0) {
+                    final String defaultTypeName = StringUtils.uncapitalize(clazz.getSimpleName());
+                    matchersKeyedByType.put(defaultTypeName, new MatcherInfo(clazz, false));
+                }
+            }
+
+            if (booleanFilterMatcher != null) {
+                for (final String type : booleanFilterMatcher.value()) {
+                    matchersKeyedByType.put(type, new MatcherInfo(clazz, true));
+                }
+                if (booleanFilterMatcher.value().length == 0) {
+                    final String defaultTypeName = StringUtils.uncapitalize(clazz.getSimpleName());
+                    matchersKeyedByType.put(defaultTypeName, new MatcherInfo(clazz, true));
+                }
+            }
+        }
+    }
+
+    public MatcherInfo find(final String type) {
+        if (!matchersKeyedByType.containsKey(type)) {
+            throw new IllegalArgumentException("filter expression [" + type + "] not found");
+        }
+        return matchersKeyedByType.get(type);
+    }
+
+    @Getter
+    @AllArgsConstructor
+    public static class MatcherInfo {
+        private final Class<?> matcher;
+        private final boolean isBooleanType;
+    }
+}
diff --git a/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/FilterStmts.java b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/FilterStmts.java
new file mode 100644
index 0000000000..40dcc4f854
--- /dev/null
+++ b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/FilterStmts.java
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oal.rt.parser;
+
+import java.util.LinkedList;
+import java.util.List;
+import lombok.Getter;
+import lombok.Setter;
+
+/**
+ * Filter statements in the OAL scripts.
+ */
+@Getter
+@Setter
+public class FilterStmts {
+    /**
+     * Parsed raw result from grammar tree.
+     */
+    private List<ConditionExpression> filterExpressionsParserResult;
+    /**
+     * Generated expressions for code generation.
+     */
+    private List<Expression> filterExpressions;
+
+    public void addFilterExpressions(Expression filterExpression) {
+        if (filterExpressions == null) {
+            filterExpressions = new LinkedList<>();
+        }
+        filterExpressions.add(filterExpression);
+    }
+
+    public void addFilterExpressionsParserResult(ConditionExpression conditionExpression) {
+        if (filterExpressionsParserResult == null) {
+            filterExpressionsParserResult = new LinkedList<>();
+        }
+        filterExpressionsParserResult.add(conditionExpression);
+    }
+}
diff --git a/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/FromStmt.java b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/FromStmt.java
new file mode 100644
index 0000000000..94be18494a
--- /dev/null
+++ b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/FromStmt.java
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oal.rt.parser;
+
+import java.util.ArrayList;
+import java.util.List;
+import lombok.Getter;
+import lombok.Setter;
+
+/**
+ * FROM statement in the OAL script
+ */
+@Setter
+@Getter
+public class FromStmt {
+    /**
+     * Source name in the FROM statement
+     */
+    private String sourceName;
+    /**
+     * source id according to {@link #sourceName}
+     */
+    private int sourceScopeId;
+    /**
+     * Attribute accessor
+     */
+    private List<String> sourceAttribute = new ArrayList<>();
+    /**
+     * Type cast function if exists. NULL as default, means no cast.
+     */
+    private String sourceCastType;
+}
diff --git a/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/MetricsHolder.java b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/MetricsHolder.java
new file mode 100644
index 0000000000..fc62590a12
--- /dev/null
+++ b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/MetricsHolder.java
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oal.rt.parser;
+
+import com.google.common.collect.ImmutableSet;
+import com.google.common.reflect.ClassPath;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import lombok.SneakyThrows;
+import org.apache.skywalking.oap.server.core.analysis.metrics.Metrics;
+import org.apache.skywalking.oap.server.core.analysis.metrics.annotation.MetricsFunction;
+
+@SuppressWarnings("UnstableApiUsage")
+public class MetricsHolder {
+    private static final Map<String, Class<? extends Metrics>> REGISTER = new HashMap<>();
+    private static volatile boolean INITIALIZED = false;
+
+    private static void init() throws IOException {
+        ClassPath classpath = ClassPath.from(MetricsHolder.class.getClassLoader());
+        ImmutableSet<ClassPath.ClassInfo> classes = classpath.getTopLevelClassesRecursive("org.apache.skywalking");
+        for (ClassPath.ClassInfo classInfo : classes) {
+            Class<?> aClass = classInfo.load();
+
+            if (aClass.isAnnotationPresent(MetricsFunction.class)) {
+                MetricsFunction metricsFunction = aClass.getAnnotation(MetricsFunction.class);
+                REGISTER.put(
+                    metricsFunction.functionName(),
+                    (Class<? extends Metrics>) aClass
+                );
+            }
+        }
+    }
+
+    @SneakyThrows
+    public static Class<? extends Metrics> find(String functionName) {
+        if (!INITIALIZED) {
+            init();
+            INITIALIZED = true;
+        }
+
+        Class<? extends Metrics> metricsClass = REGISTER.get(functionName);
+        if (metricsClass == null) {
+            throw new IllegalArgumentException("Can't find metrics, " + functionName);
+        }
+        return metricsClass;
+    }
+}
diff --git a/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/OALListener.java b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/OALListener.java
new file mode 100644
index 0000000000..daab84c656
--- /dev/null
+++ b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/OALListener.java
@@ -0,0 +1,310 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oal.rt.parser;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import org.antlr.v4.runtime.misc.NotNull;
+import org.apache.skywalking.oal.rt.grammar.OALParser;
+import org.apache.skywalking.oal.rt.grammar.OALParserBaseListener;
+import org.apache.skywalking.oap.server.core.analysis.ISourceDecorator;
+import org.apache.skywalking.oap.server.core.analysis.SourceDecoratorManager;
+import org.apache.skywalking.oap.server.core.analysis.metrics.LabeledValueHolder;
+import org.apache.skywalking.oap.server.core.analysis.metrics.Metrics;
+import org.apache.skywalking.oap.server.core.source.DefaultScopeDefine;
+import org.apache.skywalking.oap.server.core.source.ISource;
+
+public class OALListener extends OALParserBaseListener {
+    private List<AnalysisResult> results;
+    private AnalysisResult current;
+    private DisableCollection collection;
+
+    private ConditionExpression conditionExpression;
+
+    private final String sourcePackage;
+
+    public OALListener(OALScripts scripts, String sourcePackage) {
+        this.results = scripts.getMetricsStmts();
+        this.collection = scripts.getDisableCollection();
+        this.sourcePackage = sourcePackage;
+    }
+
+    @Override
+    public void enterAggregationStatement(@NotNull OALParser.AggregationStatementContext ctx) {
+        current = new AnalysisResult();
+    }
+
+    @Override
+    public void exitAggregationStatement(@NotNull OALParser.AggregationStatementContext ctx) {
+        DeepAnalysis deepAnalysis = new DeepAnalysis();
+        results.add(deepAnalysis.analysis(current));
+        current = null;
+    }
+
+    @Override
+    public void enterSource(OALParser.SourceContext ctx) {
+        current.getFrom().setSourceName(ctx.getText());
+        current.getFrom().setSourceScopeId(DefaultScopeDefine.valueOf(metricsNameFormat(ctx.getText())));
+    }
+
+    @Override
+    public void enterSourceAttribute(OALParser.SourceAttributeContext ctx) {
+        current.getFrom().getSourceAttribute().add(ctx.getText());
+    }
+
+    @Override
+    public void enterSourceAttrCast(OALParser.SourceAttrCastContext ctx) {
+        current.getFrom().setSourceCastType(ctx.getText());
+    }
+
+    @Override
+    public void enterVariable(OALParser.VariableContext ctx) {
+    }
+
+    @Override
+    public void exitVariable(OALParser.VariableContext ctx) {
+        current.setVarName(ctx.getText());
+        current.setMetricsName(metricsNameFormat(ctx.getText()));
+        current.setTableName(ctx.getText().toLowerCase());
+    }
+
+    @Override
+    public void enterFunctionName(OALParser.FunctionNameContext ctx) {
+        current.getAggregationFuncStmt().setAggregationFunctionName(ctx.getText());
+    }
+
+    @Override
+    public void enterFilterStatement(OALParser.FilterStatementContext ctx) {
+        conditionExpression = new ConditionExpression();
+    }
+
+    @Override
+    public void exitFilterStatement(OALParser.FilterStatementContext ctx) {
+        current.getFilters().addFilterExpressionsParserResult(conditionExpression);
+        conditionExpression = null;
+    }
+
+    @Override
+    public void enterFuncParamExpression(OALParser.FuncParamExpressionContext ctx) {
+        conditionExpression = new ConditionExpression();
+    }
+
+    @Override
+    public void exitFuncParamExpression(OALParser.FuncParamExpressionContext ctx) {
+        current.getAggregationFuncStmt().addFuncConditionExpression(conditionExpression);
+        conditionExpression = null;
+    }
+
+    @Override
+    public void enterDecorateSource(OALParser.DecorateSourceContext ctx) {
+        Class<? extends Metrics> metricsClass = MetricsHolder.find(current.getAggregationFuncStmt().getAggregationFunctionName());
+        if (LabeledValueHolder.class.isAssignableFrom(metricsClass)) {
+            throw new IllegalArgumentException(
+                "OAL metric: " + current.getMetricsName() + ", decorate source not support labeled value metrics.");
+        }
+        String decoratorName = ctx.STRING_LITERAL().getText();
+        String decoratorNameTrim = decoratorName.substring(1, decoratorName.length() - 1);
+        current.setSourceDecorator(decoratorNameTrim);
+        Map<String, ISourceDecorator<ISource>> map = SourceDecoratorManager.DECORATOR_MAP;
+        int currentScopeId = current.getFrom().getSourceScopeId();
+        if (currentScopeId != DefaultScopeDefine.SERVICE
+            && currentScopeId != DefaultScopeDefine.K8S_SERVICE
+            && currentScopeId != DefaultScopeDefine.ENDPOINT) {
+            throw new IllegalArgumentException("OAL metric: " + current.getMetricsName() + ", decorate source not support scope: " + DefaultScopeDefine.nameOf(currentScopeId));
+        }
+        ISourceDecorator<ISource> decorator = map.get(decoratorNameTrim);
+        if (decorator == null) {
+            throw new IllegalArgumentException("OAL metric: " + current.getMetricsName() + " define a decorator: " + decoratorNameTrim
+            + ", but can't find it.");
+        }
+        int scopeId = decorator.getSourceScope();
+        if (scopeId != currentScopeId) {
+            throw new IllegalArgumentException("OAL Decorate Source, expect decorator scope id is: " + currentScopeId + ", but got: " + scopeId);
+        }
+    }
+
+    /////////////
+    // Expression
+    ////////////
+    @Override
+    public void enterConditionAttribute(OALParser.ConditionAttributeContext ctx) {
+        conditionExpression.getAttributes().add(ctx.getText());
+    }
+
+    @Override
+    public void enterBooleanMatch(OALParser.BooleanMatchContext ctx) {
+        conditionExpression.setExpressionType("booleanMatch");
+    }
+
+    @Override
+    public void enterNumberMatch(OALParser.NumberMatchContext ctx) {
+        conditionExpression.setExpressionType("numberMatch");
+    }
+
+    @Override
+    public void enterStringMatch(OALParser.StringMatchContext ctx) {
+        conditionExpression.setExpressionType("stringMatch");
+    }
+
+    @Override
+    public void enterGreaterMatch(OALParser.GreaterMatchContext ctx) {
+        conditionExpression.setExpressionType("greaterMatch");
+    }
+
+    @Override
+    public void enterGreaterEqualMatch(OALParser.GreaterEqualMatchContext ctx) {
+        conditionExpression.setExpressionType("greaterEqualMatch");
+    }
+
+    @Override
+    public void enterLessMatch(OALParser.LessMatchContext ctx) {
+        conditionExpression.setExpressionType("lessMatch");
+    }
+
+    @Override
+    public void enterLessEqualMatch(OALParser.LessEqualMatchContext ctx) {
+        conditionExpression.setExpressionType("lessEqualMatch");
+    }
+
+    @Override
+    public void enterNotEqualMatch(final OALParser.NotEqualMatchContext ctx) {
+        conditionExpression.setExpressionType("notEqualMatch");
+    }
+
+    @Override
+    public void enterBooleanNotEqualMatch(final OALParser.BooleanNotEqualMatchContext ctx) {
+        conditionExpression.setExpressionType("booleanNotEqualMatch");
+    }
+
+    @Override
+    public void enterLikeMatch(final OALParser.LikeMatchContext ctx) {
+        conditionExpression.setExpressionType("likeMatch");
+    }
+
+    @Override
+    public void enterContainMatch(final OALParser.ContainMatchContext ctx) {
+        conditionExpression.setExpressionType("containMatch");
+    }
+
+    @Override
+    public void enterNotContainMatch(final OALParser.NotContainMatchContext ctx) {
+        conditionExpression.setExpressionType("notContainMatch");
+    }
+
+    @Override
+    public void enterInMatch(final OALParser.InMatchContext ctx) {
+        conditionExpression.setExpressionType("inMatch");
+    }
+
+    @Override
+    public void enterMultiConditionValue(final OALParser.MultiConditionValueContext ctx) {
+        conditionExpression.enterMultiConditionValue();
+    }
+
+    @Override
+    public void exitMultiConditionValue(final OALParser.MultiConditionValueContext ctx) {
+        conditionExpression.exitMultiConditionValue();
+    }
+
+    @Override
+    public void enterBooleanConditionValue(OALParser.BooleanConditionValueContext ctx) {
+        enterConditionValue(ctx.getText());
+    }
+
+    @Override
+    public void enterStringConditionValue(OALParser.StringConditionValueContext ctx) {
+        enterConditionValue(ctx.getText());
+    }
+
+    @Override
+    public void enterEnumConditionValue(OALParser.EnumConditionValueContext ctx) {
+        enterEnumConditionValue(ctx.getText());
+    }
+
+    @Override
+    public void enterNumberConditionValue(OALParser.NumberConditionValueContext ctx) {
+        conditionExpression.isNumber();
+        enterConditionValue(ctx.getText());
+    }
+
+    @Override
+    public void enterNullConditionValue(OALParser.NullConditionValueContext ctx) {
+        enterConditionValue(ctx.getText());
+    }
+
+    @Override
+    public void enterExpressionAttrCast(final OALParser.ExpressionAttrCastContext ctx) {
+        conditionExpression.setCastType(ctx.getText());
+    }
+
+    private void enterConditionValue(String value) {
+        conditionExpression.addValue(value);
+    }
+
+    private void enterEnumConditionValue(String value) {
+        conditionExpression.addValue(sourcePackage + value);
+    }
+
+    /////////////
+    // Expression end.
+    ////////////
+
+    @Override
+    public void enterLiteralExpression(OALParser.LiteralExpressionContext ctx) {
+        current.getAggregationFuncStmt().addFuncArg(new Argument(EntryMethod.LITERAL_TYPE, Arrays.asList(ctx.getText())));
+    }
+
+    @Override
+    public void enterAttributeExpression(final OALParser.AttributeExpressionContext ctx) {
+        current.getAggregationFuncStmt().addFuncArg(new Argument(EntryMethod.ATTRIBUTE_EXP_TYPE, new ArrayList<>(3)));
+    }
+
+    @Override
+    public void enterAttributeExpressionSegment(OALParser.AttributeExpressionSegmentContext ctx) {
+        current.getAggregationFuncStmt().getLastArgument().addText(ctx.getText());
+    }
+
+    @Override
+    public void enterFunctionArgCast(final OALParser.FunctionArgCastContext ctx) {
+        current.getAggregationFuncStmt().getLastArgument().setCastType(ctx.getText());
+    }
+
+    private String metricsNameFormat(String source) {
+        source = firstLetterUpper(source);
+        int idx;
+        while ((idx = source.indexOf("_")) > -1) {
+            source = source.substring(0, idx) + firstLetterUpper(source.substring(idx + 1));
+        }
+        return source;
+    }
+
+    /**
+     * Disable source
+     */
+    @Override
+    public void enterDisableSource(OALParser.DisableSourceContext ctx) {
+        collection.add(ctx.getText());
+    }
+
+    private String firstLetterUpper(String source) {
+        return source.substring(0, 1).toUpperCase() + source.substring(1);
+    }
+}
diff --git a/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/OALScripts.java b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/OALScripts.java
new file mode 100644
index 0000000000..18f16a2aa3
--- /dev/null
+++ b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/OALScripts.java
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oal.rt.parser;
+
+import java.util.ArrayList;
+import java.util.List;
+import lombok.Getter;
+
+@Getter
+public class OALScripts {
+    private List<AnalysisResult> metricsStmts;
+    private DisableCollection disableCollection;
+
+    public OALScripts() {
+        metricsStmts = new ArrayList<>();
+        disableCollection = new DisableCollection();
+    }
+}
diff --git a/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/PersistenceColumns.java b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/PersistenceColumns.java
new file mode 100644
index 0000000000..30280c22a4
--- /dev/null
+++ b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/PersistenceColumns.java
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oal.rt.parser;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class PersistenceColumns {
+    private List<PersistenceField> stringFields = new ArrayList<>();
+    private List<PersistenceField> longFields = new ArrayList<>();
+    private List<PersistenceField> doubleFields = new ArrayList<>();
+    private List<PersistenceField> intFields = new ArrayList<>();
+    private List<PersistenceField> objectFields = new ArrayList<>();
+
+    public void addStringField(String fieldName) {
+        stringFields.add(new PersistenceField(fieldName, "String"));
+    }
+
+    public void addLongField(String fieldName) {
+        longFields.add(new PersistenceField(fieldName, "long"));
+    }
+
+    public void addDoubleField(String fieldName) {
+        doubleFields.add(new PersistenceField(fieldName, "double"));
+    }
+
+    public void addIntField(String fieldName) {
+        intFields.add(new PersistenceField(fieldName, "int"));
+    }
+
+    public void addObjectField(String fieldName, String fieldType) {
+        objectFields.add(new PersistenceField(fieldName, fieldType));
+    }
+
+    public List<PersistenceField> getStringFields() {
+        return stringFields;
+    }
+
+    public List<PersistenceField> getLongFields() {
+        return longFields;
+    }
+
+    public List<PersistenceField> getDoubleFields() {
+        return doubleFields;
+    }
+
+    public List<PersistenceField> getIntFields() {
+        return intFields;
+    }
+
+    public List<PersistenceField> getObjectFields() {
+        return objectFields;
+    }
+}
diff --git a/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/PersistenceField.java b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/PersistenceField.java
new file mode 100644
index 0000000000..45c5a10516
--- /dev/null
+++ b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/PersistenceField.java
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oal.rt.parser;
+
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.skywalking.oal.rt.util.ClassMethodUtil;
+
+@Getter
+@Setter
+public class PersistenceField {
+    private String fieldName;
+    private String setter;
+    private String getter;
+    private String fieldType;
+
+    public PersistenceField(String fieldName, String fieldType) {
+        this.fieldName = fieldName;
+        this.setter = ClassMethodUtil.toSetMethod(fieldName);
+        this.getter = ClassMethodUtil.toGetMethod(fieldName);
+        this.fieldType = fieldType;
+    }
+}
diff --git a/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/ScriptParser.java b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/ScriptParser.java
new file mode 100644
index 0000000000..c69fa2a9d2
--- /dev/null
+++ b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/ScriptParser.java
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oal.rt.parser;
+
+import java.io.IOException;
+import java.io.Reader;
+import org.antlr.v4.runtime.CharStreams;
+import org.antlr.v4.runtime.CommonTokenStream;
+import org.antlr.v4.runtime.tree.ParseTree;
+import org.antlr.v4.runtime.tree.ParseTreeWalker;
+import org.apache.skywalking.oal.rt.grammar.OALLexer;
+import org.apache.skywalking.oal.rt.grammar.OALParser;
+
+/**
+ * Script reader and parser.
+ */
+public class ScriptParser {
+    private OALLexer lexer;
+
+    private String sourcePackage;
+
+    private ScriptParser() {
+
+    }
+
+    public static ScriptParser createFromFile(Reader scriptReader, String sourcePackage) throws IOException {
+        ScriptParser parser = new ScriptParser();
+        parser.lexer = new OALLexer(CharStreams.fromReader(scriptReader));
+        parser.sourcePackage = sourcePackage;
+        return parser;
+    }
+
+    public static ScriptParser createFromScriptText(String script, String sourcePackage) throws IOException {
+        ScriptParser parser = new ScriptParser();
+        parser.lexer = new OALLexer(CharStreams.fromString(script));
+        parser.sourcePackage = sourcePackage;
+        return parser;
+    }
+
+    public OALScripts parse() throws IOException {
+        OALScripts scripts = new OALScripts();
+
+        CommonTokenStream tokens = new CommonTokenStream(lexer);
+
+        OALParser parser = new OALParser(tokens);
+
+        ParseTree tree = parser.root();
+        ParseTreeWalker walker = new ParseTreeWalker();
+
+        walker.walk(new OALListener(scripts, sourcePackage), tree);
+
+        return scripts;
+    }
+
+    public void close() {
+    }
+}
diff --git a/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/SourceColumn.java b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/SourceColumn.java
new file mode 100644
index 0000000000..479c9bafd3
--- /dev/null
+++ b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/SourceColumn.java
@@ -0,0 +1,113 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oal.rt.parser;
+
+import java.util.Objects;
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.skywalking.oal.rt.util.ClassMethodUtil;
+
+@Getter
+@Setter
+public class SourceColumn {
+    private String fieldName;
+    private String columnName;
+    private Class<?> type;
+    private String typeName;
+    private boolean isID;
+    private int length;
+    private String fieldSetter;
+    private String fieldGetter;
+    private final int shardingKeyIdx;
+    private final boolean attribute;
+
+    public SourceColumn(String fieldName, String columnName, Class<?> type, boolean isID, int length,
+                        int shardingKeyIdx, boolean attribute) {
+        this.fieldName = fieldName;
+        this.columnName = columnName;
+        this.type = type;
+        this.typeName = type.getName();
+        this.isID = isID;
+        this.length = length;
+
+        this.fieldGetter = ClassMethodUtil.toGetMethod(fieldName);
+        this.fieldSetter = ClassMethodUtil.toSetMethod(fieldName);
+        this.shardingKeyIdx = shardingKeyIdx;
+        this.attribute = attribute;
+    }
+
+    public void setFieldName(String fieldName) {
+        this.fieldName = fieldName;
+        this.fieldGetter = ClassMethodUtil.toGetMethod(fieldName);
+        this.fieldSetter = ClassMethodUtil.toSetMethod(fieldName);
+    }
+
+    public void setTypeName(String typeName) {
+        switch (typeName) {
+            case "int":
+                this.type = int.class;
+                break;
+            case "long":
+                this.type = long.class;
+                break;
+            case "string":
+            case "String":
+                this.type = String.class;
+                typeName = "String";
+                break;
+            default:
+                try {
+                    this.type = Class.forName(typeName);
+                } catch (ClassNotFoundException e) {
+                    throw new RuntimeException(e);
+                }
+        }
+
+        this.typeName = typeName;
+    }
+
+    /**
+     * @return true if this column is a part of sharding key
+     */
+    public boolean isShardingKey() {
+        return this.shardingKeyIdx > -1;
+    }
+
+    @Override
+    public String toString() {
+        return "SourceColumn{" + "fieldName=" + fieldName + ", columnName=" + columnName + ", type=" + type + ", isID=" + isID + ", shardingKeyIdx=" + shardingKeyIdx + ", isAttribute=" + attribute + "}";
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o)
+            return true;
+        if (o == null || getClass() != o.getClass())
+            return false;
+        SourceColumn column = (SourceColumn) o;
+        return isID == column.isID && Objects.equals(fieldName, column.fieldName) && Objects.equals(columnName, column.columnName) && Objects
+            .equals(type, column.type) && Objects.equals(typeName, column.typeName) && Objects.equals(fieldSetter, column.fieldSetter) && Objects
+            .equals(fieldGetter, column.fieldGetter);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(fieldName, columnName, type, typeName, isID, fieldSetter, fieldGetter);
+    }
+}
diff --git a/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/SourceColumnsFactory.java b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/SourceColumnsFactory.java
new file mode 100644
index 0000000000..494a1b650c
--- /dev/null
+++ b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/parser/SourceColumnsFactory.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oal.rt.parser;
+
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.skywalking.oap.server.core.source.DefaultScopeDefine;
+import org.apache.skywalking.oap.server.core.source.ScopeDefaultColumn;
+
+public class SourceColumnsFactory {
+    public static List<SourceColumn> getColumns(String source) {
+        List<SourceColumn> sourceColumns = new ArrayList<>();
+
+        List<ScopeDefaultColumn> columns = DefaultScopeDefine.getDefaultColumns(source);
+        for (ScopeDefaultColumn defaultColumn : columns) {
+            sourceColumns.add(
+                new SourceColumn(defaultColumn.getFieldName(), defaultColumn.getColumnName(), defaultColumn
+                    .getType(), defaultColumn.isID(), defaultColumn.getLength(), defaultColumn.getShardingKeyIdx(), defaultColumn.isAttribute()));
+        }
+        return sourceColumns;
+    }
+}
diff --git a/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/util/ClassMethodUtil.java b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/util/ClassMethodUtil.java
new file mode 100644
index 0000000000..0b7f0f1287
--- /dev/null
+++ b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/util/ClassMethodUtil.java
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oal.rt.util;
+
+import java.util.List;
+
+public class ClassMethodUtil {
+    public static String toGetMethod(String attribute) {
+        return "get" + attribute.substring(0, 1).toUpperCase() + attribute.substring(1);
+    }
+
+    public static String toSetMethod(String attribute) {
+        return "set" + attribute.substring(0, 1).toUpperCase() + attribute.substring(1);
+    }
+
+    public static String toIsMethod(String attribute) {
+        return "is" + attribute.substring(0, 1).toUpperCase() + attribute.substring(1);
+    }
+
+    /**
+     * @return nested get methods.
+     */
+    public static String toGetMethod(List<String> attributes) {
+        StringBuilder method = new StringBuilder();
+        for (int i = 0; i < attributes.size(); i++) {
+            if (i != 0) {
+                method.append(".");
+            }
+            if (isMapExpression(attributes.get(i))) {
+                method.append(mapExpression(attributes.get(i)));
+            } else {
+                method.append(toGetMethod(attributes.get(i))).append("()");
+            }
+        }
+        return method.toString();
+    }
+
+    /**
+     * @return nested get/is methods.
+     */
+    public static String toIsMethod(List<String> attributes) {
+        StringBuilder method = new StringBuilder();
+        for (int i = 0; i < attributes.size(); i++) {
+            if (i != 0) {
+                method.append(".");
+            }
+            if (i != attributes.size() - 1) {
+                method.append(toGetMethod(attributes.get(i))).append("()");
+            } else {
+                method.append(toIsMethod(attributes.get(i))).append("()");
+            }
+        }
+        return method.toString();
+    }
+
+    /**
+     * @return empty if this attribute is not type of map.
+     */
+    private static String mapExpression(String attribute) {
+        final int indexOf = attribute.indexOf("[");
+        return toGetMethod(attribute.substring(0, indexOf))
+            + "(" + attribute.substring(indexOf + 1, attribute.length() - 1) + ")";
+    }
+
+    private static boolean isMapExpression(String attribute) {
+        return attribute.indexOf("[") > 0 && attribute.endsWith("]");
+    }
+}
diff --git a/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/util/OALClassGenerator.java b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/util/OALClassGenerator.java
new file mode 100644
index 0000000000..46f2039fd2
--- /dev/null
+++ b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/util/OALClassGenerator.java
@@ -0,0 +1,634 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oal.rt.util;
+
+import freemarker.template.Configuration;
+import freemarker.template.Version;
+import javassist.CannotCompileException;
+import javassist.ClassPool;
+import javassist.CtClass;
+import javassist.CtConstructor;
+import javassist.CtField;
+import javassist.CtNewConstructor;
+import javassist.CtNewMethod;
+import javassist.NotFoundException;
+import javassist.bytecode.AnnotationsAttribute;
+import javassist.bytecode.ClassFile;
+import javassist.bytecode.ConstPool;
+import javassist.bytecode.SignatureAttribute;
+import javassist.bytecode.annotation.Annotation;
+import javassist.bytecode.annotation.ClassMemberValue;
+import javassist.bytecode.annotation.IntegerMemberValue;
+import javassist.bytecode.annotation.StringMemberValue;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang3.JavaVersion;
+import org.apache.commons.lang3.SystemUtils;
+import org.apache.skywalking.oal.rt.OALRuntime;
+import org.apache.skywalking.oal.rt.output.AllDispatcherContext;
+import org.apache.skywalking.oal.rt.output.DispatcherContext;
+import org.apache.skywalking.oal.rt.parser.AnalysisResult;
+import org.apache.skywalking.oal.rt.parser.OALScripts;
+import org.apache.skywalking.oal.rt.parser.SourceColumn;
+import org.apache.skywalking.oap.server.core.WorkPath;
+import org.apache.skywalking.oap.server.core.analysis.DisableRegister;
+import org.apache.skywalking.oap.server.core.analysis.SourceDispatcher;
+import org.apache.skywalking.oap.server.core.analysis.Stream;
+import org.apache.skywalking.oap.server.core.oal.rt.OALCompileException;
+import org.apache.skywalking.oap.server.core.oal.rt.OALDefine;
+import org.apache.skywalking.oap.server.core.source.oal.rt.dispatcher.DispatcherClassPackageHolder;
+import org.apache.skywalking.oap.server.core.source.oal.rt.metrics.MetricClassPackageHolder;
+import org.apache.skywalking.oap.server.core.source.oal.rt.metrics.builder.MetricBuilderClassPackageHolder;
+import org.apache.skywalking.oap.server.core.storage.StorageBuilderFactory;
+import org.apache.skywalking.oap.server.core.storage.annotation.BanyanDB;
+import org.apache.skywalking.oap.server.core.storage.annotation.Column;
+import org.apache.skywalking.oap.server.core.storage.annotation.ElasticSearch;
+import org.apache.skywalking.oap.server.library.util.StringUtil;
+
+import java.io.DataOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+@Slf4j
+public class OALClassGenerator {
+
+    private static final String METRICS_FUNCTION_PACKAGE = "org.apache.skywalking.oap.server.core.analysis.metrics.";
+    private static final String WITH_METADATA_INTERFACE = "org.apache.skywalking.oap.server.core.analysis.metrics.WithMetadata";
+    private static final String DISPATCHER_INTERFACE = "org.apache.skywalking.oap.server.core.analysis.SourceDispatcher";
+    private static final String METRICS_STREAM_PROCESSOR = "org.apache.skywalking.oap.server.core.analysis.worker.MetricsStreamProcessor";
+    private static final String[] METRICS_CLASS_METHODS = {
+            "id",
+            "hashCode",
+            "remoteHashCode",
+            "equals",
+            "serialize",
+            "deserialize",
+            "getMeta",
+            "toHour",
+            "toDay"
+    };
+    private static final String[] METRICS_BUILDER_CLASS_METHODS = {
+            "entity2Storage",
+            "storage2Entity"
+    };
+
+    private static final String CLASS_FILE_CHARSET = "UTF-8";
+
+    private boolean openEngineDebug;
+
+    private AllDispatcherContext allDispatcherContext;
+
+    private final ClassPool classPool;
+
+    private final OALDefine oalDefine;
+
+    private Configuration configuration;
+
+    private ClassLoader currentClassLoader;
+
+    private StorageBuilderFactory storageBuilderFactory;
+
+    private static String GENERATED_FILE_PATH;
+
+    public OALClassGenerator(OALDefine define) {
+        this(define, ClassPool.getDefault());
+    }
+
+    /**
+     * Constructor with custom ClassPool for test isolation.
+     */
+    public OALClassGenerator(OALDefine define, ClassPool classPool) {
+        openEngineDebug = StringUtil.isNotEmpty(System.getenv("SW_OAL_ENGINE_DEBUG"));
+        allDispatcherContext = new AllDispatcherContext();
+        this.classPool = classPool;
+        oalDefine = define;
+
+        configuration = new Configuration(new Version("2.3.28"));
+        configuration.setEncoding(Locale.ENGLISH, CLASS_FILE_CHARSET);
+        configuration.setClassLoaderForTemplateLoading(OALRuntime.class.getClassLoader(), "/code-templates");
+    }
+
+    public void generateClassAtRuntime(OALScripts oalScripts, List<Class> metricsClasses, List<Class> dispatcherClasses) throws OALCompileException {
+        List<AnalysisResult> metricsStmts = oalScripts.getMetricsStmts();
+        metricsStmts.forEach(this::buildDispatcherContext);
+
+        for (AnalysisResult metricsStmt : metricsStmts) {
+            metricsClasses.add(generateMetricsClass(metricsStmt));
+            generateMetricsBuilderClass(metricsStmt);
+        }
+
+        for (Map.Entry<String, DispatcherContext> entry : allDispatcherContext.getAllContext().entrySet()) {
+            dispatcherClasses.add(generateDispatcherClass(entry.getKey(), entry.getValue()));
+        }
+
+        oalScripts.getDisableCollection().getAllDisableSources().forEach(disable -> {
+            DisableRegister.INSTANCE.add(disable);
+        });
+    }
+
+    /**
+     * Generate metrics class, and inject it to classloader
+     */
+    private Class generateMetricsClass(AnalysisResult metricsStmt) throws OALCompileException {
+        String className = metricsClassName(metricsStmt, false);
+        CtClass parentMetricsClass = null;
+        try {
+            parentMetricsClass = classPool.get(METRICS_FUNCTION_PACKAGE + metricsStmt.getMetricsClassName());
+        } catch (NotFoundException e) {
+            log.error("Can't find parent class for " + className + ".", e);
+            throw new OALCompileException(e.getMessage(), e);
+        }
+        CtClass metricsClass = classPool.makeClass(metricsClassName(metricsStmt, true), parentMetricsClass);
+        try {
+            metricsClass.addInterface(classPool.get(WITH_METADATA_INTERFACE));
+        } catch (NotFoundException e) {
+            log.error("Can't find WithMetadata interface for " + className + ".", e);
+            throw new OALCompileException(e.getMessage(), e);
+        }
+
+        ClassFile metricsClassClassFile = metricsClass.getClassFile();
+        ConstPool constPool = metricsClassClassFile.getConstPool();
+
+        /**
+         * Create empty construct
+         */
+        try {
+            CtConstructor defaultConstructor = CtNewConstructor.make("public " + className + "() {}", metricsClass);
+            metricsClass.addConstructor(defaultConstructor);
+        } catch (CannotCompileException e) {
+            log.error("Can't add empty constructor in " + className + ".", e);
+            throw new OALCompileException(e.getMessage(), e);
+        }
+
+        /**
+         * Add fields with annotations.
+         *
+         * private ${sourceField.typeName} ${sourceField.fieldName};
+         */
+        for (SourceColumn field : metricsStmt.getFieldsFromSource()) {
+            try {
+                CtField newField = CtField.make(
+                        "private " + field.getType()
+                                .getName() + " " + field.getFieldName() + ";", metricsClass);
+
+                metricsClass.addField(newField);
+
+                metricsClass.addMethod(CtNewMethod.getter(field.getFieldGetter(), newField));
+                metricsClass.addMethod(CtNewMethod.setter(field.getFieldSetter(), newField));
+
+                AnnotationsAttribute annotationsAttribute = new AnnotationsAttribute(
+                        constPool, AnnotationsAttribute.visibleTag);
+                /**
+                 * Add @Column(name = "${sourceField.columnName}")
+                 */
+                Annotation columnAnnotation = new Annotation(Column.class.getName(), constPool);
+                columnAnnotation.addMemberValue("name", new StringMemberValue(field.getColumnName(), constPool));
+                if (field.getType().equals(String.class)) {
+                    columnAnnotation.addMemberValue("length", new IntegerMemberValue(constPool, field.getLength()));
+                }
+                annotationsAttribute.addAnnotation(columnAnnotation);
+                if (field.isID()) {
+                    // Add SeriesID = 0 annotation to ID field.
+                    Annotation banyanSeriesIDAnnotation = new Annotation(BanyanDB.SeriesID.class.getName(), constPool);
+                    banyanSeriesIDAnnotation.addMemberValue("index", new IntegerMemberValue(constPool, 0));
+                    annotationsAttribute.addAnnotation(banyanSeriesIDAnnotation);
+
+                    // Entity id field should enable doc values.
+                    final var enableDocValuesAnnotation = new Annotation(ElasticSearch.EnableDocValues.class.getName(), constPool);
+                    annotationsAttribute.addAnnotation(enableDocValuesAnnotation);
+                }
+                if (field.isShardingKey()) {
+                    Annotation banyanShardingKeyAnnotation = new Annotation(BanyanDB.ShardingKey.class.getName(), constPool);
+                    banyanShardingKeyAnnotation.addMemberValue("index", new IntegerMemberValue(constPool, field.getShardingKeyIdx()));
+                    annotationsAttribute.addAnnotation(banyanShardingKeyAnnotation);
+                }
+
+                newField.getFieldInfo().addAttribute(annotationsAttribute);
+            } catch (CannotCompileException e) {
+                log.error(
+                        "Can't add field(including set/get) " + field.getFieldName() + " in " + className + ".", e);
+                throw new OALCompileException(e.getMessage(), e);
+            }
+        }
+
+        /**
+         * Generate methods
+         */
+        for (String method : METRICS_CLASS_METHODS) {
+            StringWriter methodEntity = new StringWriter();
+            try {
+                configuration.getTemplate("metrics/" + method + ".ftl").process(metricsStmt, methodEntity);
+                metricsClass.addMethod(CtNewMethod.make(methodEntity.toString(), metricsClass));
+            } catch (Exception e) {
+                log.error("Can't generate method " + method + " for " + className + ".", e);
+                throw new OALCompileException(e.getMessage(), e);
+            }
+        }
+
+        /**
+         * Add following annotation to the metrics class
+         *
+         * at Stream(name = "${tableName}", scopeId = ${sourceScopeId}, builder = ${metricsName}Metrics.Builder.class, processor = MetricsStreamProcessor.class)
+         */
+        AnnotationsAttribute annotationsAttribute = new AnnotationsAttribute(
+                constPool, AnnotationsAttribute.visibleTag);
+        Annotation streamAnnotation = new Annotation(Stream.class.getName(), constPool);
+        streamAnnotation.addMemberValue("name", new StringMemberValue(metricsStmt.getTableName(), constPool));
+        streamAnnotation.addMemberValue(
+                "scopeId", new IntegerMemberValue(constPool, metricsStmt.getFrom().getSourceScopeId()));
+        streamAnnotation.addMemberValue(
+                "builder", new ClassMemberValue(metricsBuilderClassName(metricsStmt, true), constPool));
+        streamAnnotation.addMemberValue("processor", new ClassMemberValue(METRICS_STREAM_PROCESSOR, constPool));
+
+        annotationsAttribute.addAnnotation(streamAnnotation);
+        metricsClassClassFile.addAttribute(annotationsAttribute);
+
+        Class targetClass;
+        try {
+            if (SystemUtils.isJavaVersionAtMost(JavaVersion.JAVA_1_8)) {
+                targetClass = metricsClass.toClass(currentClassLoader, null);
+            } else {
+                targetClass = metricsClass.toClass(MetricClassPackageHolder.class);
+            }
+        } catch (CannotCompileException e) {
+            log.error("Can't compile/load " + className + ".", e);
+            throw new OALCompileException(e.getMessage(), e);
+        }
+
+        log.debug("Generate metrics class, " + metricsClass.getName());
+        writeGeneratedFile(metricsClass, "metrics");
+        writeSourceFile(metricsStmt, "metrics");
+
+        return targetClass;
+    }
+
+    /**
+     * Generate metrics class builder and inject it to classloader
+     */
+    private void generateMetricsBuilderClass(AnalysisResult metricsStmt) throws OALCompileException {
+        String className = metricsBuilderClassName(metricsStmt, false);
+        CtClass metricsBuilderClass = classPool.makeClass(metricsBuilderClassName(metricsStmt, true));
+        try {
+            metricsBuilderClass.addInterface(classPool.get(storageBuilderFactory.builderTemplate().getSuperClass()));
+        } catch (NotFoundException e) {
+            log.error("Can't find StorageBuilder interface for " + className + ".", e);
+            throw new OALCompileException(e.getMessage(), e);
+        }
+
+        /**
+         * Create empty construct
+         */
+        try {
+            CtConstructor defaultConstructor = CtNewConstructor.make(
+                    "public " + className + "() {}", metricsBuilderClass);
+            metricsBuilderClass.addConstructor(defaultConstructor);
+        } catch (CannotCompileException e) {
+            log.error("Can't add empty constructor in " + className + ".", e);
+            throw new OALCompileException(e.getMessage(), e);
+        }
+
+        /**
+         * Generate methods
+         */
+        for (String method : METRICS_BUILDER_CLASS_METHODS) {
+            StringWriter methodEntity = new StringWriter();
+            try {
+                configuration
+                        .getTemplate(storageBuilderFactory.builderTemplate().getTemplatePath() + "/" + method + ".ftl")
+                        .process(metricsStmt, methodEntity);
+                metricsBuilderClass.addMethod(CtNewMethod.make(methodEntity.toString(), metricsBuilderClass));
+            } catch (Exception e) {
+                log.error("Can't generate method " + method + " for " + className + ".", e);
+                throw new OALCompileException(e.getMessage(), e);
+            }
+        }
+
+        try {
+            if (SystemUtils.isJavaVersionAtMost(JavaVersion.JAVA_1_8)) {
+                metricsBuilderClass.toClass(currentClassLoader, null);
+            } else {
+                metricsBuilderClass.toClass(MetricBuilderClassPackageHolder.class);
+            }
+        } catch (CannotCompileException e) {
+            log.error("Can't compile/load " + className + ".", e);
+            throw new OALCompileException(e.getMessage(), e);
+        }
+
+        writeGeneratedFile(metricsBuilderClass,  "metrics/builder");
+    }
+
+    /**
+     * Generate SourceDispatcher class and inject it to classloader
+     */
+    private Class generateDispatcherClass(String scopeName,
+                                          DispatcherContext dispatcherContext) throws OALCompileException {
+
+        String className = dispatcherClassName(scopeName, false);
+        CtClass dispatcherClass = classPool.makeClass(dispatcherClassName(scopeName, true));
+        try {
+            CtClass dispatcherInterface = classPool.get(DISPATCHER_INTERFACE);
+
+            dispatcherClass.addInterface(dispatcherInterface);
+
+            /**
+             * Set generic signature
+             */
+            String sourceClassName = oalDefine.getSourcePackage() + dispatcherContext.getSource();
+            SignatureAttribute.ClassSignature dispatcherSignature =
+                    new SignatureAttribute.ClassSignature(
+                            null, null,
+                            // Set interface and its generic params
+                            new SignatureAttribute.ClassType[] {
+                                    new SignatureAttribute.ClassType(
+                                            SourceDispatcher.class
+                                                    .getCanonicalName(),
+                                            new SignatureAttribute.TypeArgument[] {
+                                                    new SignatureAttribute.TypeArgument(
+                                                            new SignatureAttribute.ClassType(
+                                                                    sourceClassName))
+                                            }
+                                    )
+                            }
+                    );
+
+            dispatcherClass.setGenericSignature(dispatcherSignature.encode());
+        } catch (NotFoundException e) {
+            log.error("Can't find Dispatcher interface for " + className + ".", e);
+            throw new OALCompileException(e.getMessage(), e);
+        }
+
+        /**
+         * Generate methods
+         */
+        for (AnalysisResult dispatcherContextMetric : dispatcherContext.getMetrics()) {
+            StringWriter methodEntity = new StringWriter();
+            try {
+                configuration.getTemplate("dispatcher/doMetrics.ftl").process(dispatcherContextMetric, methodEntity);
+                dispatcherClass.addMethod(CtNewMethod.make(methodEntity.toString(), dispatcherClass));
+            } catch (Exception e) {
+                log.error(
+                        "Can't generate method do" + dispatcherContextMetric.getMetricsName() + " for " + className + ".",
+                        e
+                );
+                log.error("Method body as following" + System.lineSeparator() + "{}", methodEntity);
+                throw new OALCompileException(e.getMessage(), e);
+            }
+        }
+
+        try {
+            StringWriter methodEntity = new StringWriter();
+            configuration.getTemplate("dispatcher/dispatch.ftl").process(dispatcherContext, methodEntity);
+            dispatcherClass.addMethod(CtNewMethod.make(methodEntity.toString(), dispatcherClass));
+        } catch (Exception e) {
+            log.error("Can't generate method dispatch for " + className + ".", e);
+            throw new OALCompileException(e.getMessage(), e);
+        }
+
+        Class targetClass;
+        try {
+            if (SystemUtils.isJavaVersionAtMost(JavaVersion.JAVA_1_8)) {
+                targetClass = dispatcherClass.toClass(currentClassLoader, null);
+            } else {
+                targetClass = dispatcherClass.toClass(DispatcherClassPackageHolder.class);
+            }
+        } catch (CannotCompileException e) {
+            log.error("Can't compile/load " + className + ".", e);
+            throw new OALCompileException(e.getMessage(), e);
+        }
+
+        writeGeneratedFile(dispatcherClass, "dispatcher");
+        return targetClass;
+    }
+
+    private String metricsClassName(AnalysisResult metricsStmt, boolean fullName) {
+        return (fullName ? oalDefine.getDynamicMetricsClassPackage() : "") + metricsStmt.getMetricsName() + "Metrics";
+    }
+
+    private String metricsBuilderClassName(AnalysisResult metricsStmt, boolean fullName) {
+        return (fullName ? oalDefine.getDynamicMetricsBuilderClassPackage() : "") + metricsStmt.getMetricsName() + "MetricsBuilder";
+    }
+
+    private String dispatcherClassName(String scopeName, boolean fullName) {
+        return (fullName ? oalDefine.getDynamicDispatcherClassPackage() : "") + scopeName + "Dispatcher";
+    }
+
+    private void buildDispatcherContext(AnalysisResult metricsStmt) {
+        String sourceName = metricsStmt.getFrom().getSourceName();
+
+        DispatcherContext context = allDispatcherContext.getAllContext().computeIfAbsent(sourceName, name -> {
+            DispatcherContext absent = new DispatcherContext();
+            absent.setSourcePackage(oalDefine.getSourcePackage());
+            absent.setSource(name);
+            absent.setPackageName(name.toLowerCase());
+            absent.setSourceDecorator(metricsStmt.getSourceDecorator());
+            return absent;
+        });
+        metricsStmt.setMetricsClassPackage(oalDefine.getDynamicMetricsClassPackage());
+        metricsStmt.setSourcePackage(oalDefine.getSourcePackage());
+        context.getMetrics().add(metricsStmt);
+    }
+
+    public void prepareRTTempFolder() {
+        if (openEngineDebug) {
+            File workPath = WorkPath.getPath();
+            File folder = new File(workPath.getParentFile(), "oal-rt/");
+            if (folder.exists()) {
+                try {
+                    FileUtils.deleteDirectory(folder);
+                } catch (IOException e) {
+                    log.warn("Can't delete " + folder.getAbsolutePath() + " temp folder.", e);
+                }
+            }
+            folder.mkdirs();
+        }
+    }
+
+    private void writeGeneratedFile(CtClass metricsClass, String type) throws OALCompileException {
+        if (openEngineDebug) {
+            String className = metricsClass.getSimpleName();
+            DataOutputStream printWriter = null;
+            try {
+                File folder = new File(getGeneratedFilePath() + File.separator + type);
+                if (!folder.exists()) {
+                    folder.mkdirs();
+                }
+                File file = new File(folder, className + ".class");
+                if (file.exists()) {
+                    file.delete();
+                }
+                file.createNewFile();
+
+                printWriter = new DataOutputStream(new FileOutputStream(file));
+                metricsClass.toBytecode(printWriter);
+                printWriter.flush();
+            } catch (IOException e) {
+                log.warn("Can't create " + className + ".txt, ignore.", e);
+                return;
+            } catch (CannotCompileException e) {
+                log.warn("Can't compile " + className + ".class(should not happen), ignore.", e);
+                return;
+            } finally {
+                if (printWriter != null) {
+                    try {
+                        printWriter.close();
+                    } catch (IOException e) {
+
+                    }
+                }
+            }
+        }
+    }
+
+    public void setCurrentClassLoader(ClassLoader currentClassLoader) {
+        this.currentClassLoader = currentClassLoader;
+    }
+
+    public void setStorageBuilderFactory(StorageBuilderFactory storageBuilderFactory) {
+        this.storageBuilderFactory = storageBuilderFactory;
+    }
+
+    public static void setGeneratedFilePath(String generatedFilePath) {
+        GENERATED_FILE_PATH = generatedFilePath;
+    }
+
+    public static String getGeneratedFilePath() {
+        if (GENERATED_FILE_PATH == null) {
+            return String.valueOf(new File(WorkPath.getPath().getParentFile(), "oal-rt/"));
+        }
+        return GENERATED_FILE_PATH;
+    }
+
+    public OALDefine getOalDefine() {
+        return oalDefine;
+    }
+
+    public void setOpenEngineDebug(boolean debug) {
+        this.openEngineDebug = debug;
+    }
+
+    /**
+     * Generate complete Java source code for a metrics class (V1).
+     * This is useful for comparing V1 vs V2 generated code.
+     *
+     * @param metricsStmt The V1 analysis result
+     * @return Complete Java source code as a string
+     */
+    public String generateMetricsClassSourceCode(AnalysisResult metricsStmt) throws OALCompileException {
+        StringBuilder source = new StringBuilder();
+
+        // Package declaration
+        source.append("package ").append(oalDefine.getDynamicMetricsClassPackage()).append(";\n\n");
+
+        // Imports
+        source.append("import org.apache.skywalking.oap.server.core.analysis.Stream;\n");
+        source.append("import org.apache.skywalking.oap.server.core.analysis.metrics.WithMetadata;\n");
+        source.append("import org.apache.skywalking.oap.server.core.analysis.metrics.").append(metricsStmt.getMetricsClassName()).append(";\n");
+        source.append("import org.apache.skywalking.oap.server.core.storage.annotation.*;\n");
+        source.append("import lombok.Getter;\n");
+        source.append("import lombok.Setter;\n\n");
+
+        // Class-level @Stream annotation
+        source.append("@Stream(\n");
+        source.append("    name = \"").append(metricsStmt.getTableName()).append("\",\n");
+        source.append("    scopeId = ").append(metricsStmt.getFrom().getSourceScopeId()).append(",\n");
+        source.append("    builder = ").append(metricsBuilderClassName(metricsStmt, false)).append(".class,\n");
+        source.append("    processor = ").append(METRICS_STREAM_PROCESSOR).append(".class\n");
+        source.append(")\n");
+
+        // Class declaration
+        String className = metricsClassName(metricsStmt, false);
+        source.append("public class ").append(className)
+            .append(" extends ").append(metricsStmt.getMetricsClassName())
+            .append(" implements WithMetadata {\n\n");
+
+        // Fields with annotations
+        for (SourceColumn column : metricsStmt.getFieldsFromSource()) {
+            source.append("    @Column(name = \"").append(column.getColumnName()).append("\"");
+            if (column.getType().equals(String.class)) {
+                source.append(", length = ").append(column.getLength());
+            }
+            source.append(")\n");
+
+            if (column.isID()) {
+                source.append("    @BanyanDB.SeriesID(index = 0)\n");
+                source.append("    @ElasticSearch.EnableDocValues\n");
+            }
+
+            if (column.isShardingKey()) {
+                source.append("    @BanyanDB.ShardingKey(index = ").append(column.getShardingKeyIdx()).append(")\n");
+            }
+
+            source.append("    @Getter @Setter\n");
+            source.append("    private ").append(column.getType().getSimpleName())
+                .append(" ").append(column.getFieldName()).append(";\n\n");
+        }
+
+        // Constructor
+        source.append("    public ").append(className).append("() {\n");
+        source.append("    }\n\n");
+
+        // Methods from templates
+        for (String method : METRICS_CLASS_METHODS) {
+            StringWriter methodEntity = new StringWriter();
+            try {
+                configuration.getTemplate("metrics/" + method + ".ftl").process(metricsStmt, methodEntity);
+                source.append("    ").append(methodEntity.toString()).append("\n\n");
+            } catch (Exception e) {
+                log.error("Can't generate method " + method + " for V1 source code.", e);
+                throw new OALCompileException(e.getMessage(), e);
+            }
+        }
+
+        source.append("}\n");
+
+        return source.toString();
+    }
+
+    /**
+     * Write complete Java source file to disk for comparison (V1).
+     */
+    private void writeSourceFile(AnalysisResult metricsStmt, String type) throws OALCompileException {
+        if (openEngineDebug) {
+            String className = metricsClassName(metricsStmt, false);
+            try {
+                File folder = new File(getGeneratedFilePath() + File.separator + type + "-source");
+                if (!folder.exists()) {
+                    folder.mkdirs();
+                }
+                File file = new File(folder, className + ".java");
+                if (file.exists()) {
+                    file.delete();
+                }
+
+                String sourceCode = generateMetricsClassSourceCode(metricsStmt);
+                FileUtils.writeStringToFile(file, sourceCode, CLASS_FILE_CHARSET);
+
+                log.debug("Wrote V1 source file: " + file.getAbsolutePath());
+            } catch (IOException e) {
+                log.warn("Can't write V1 source file for " + className + ", ignore.", e);
+            }
+        }
+    }
+
+}
diff --git a/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/util/TypeCastUtil.java b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/util/TypeCastUtil.java
new file mode 100644
index 0000000000..479502b604
--- /dev/null
+++ b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/util/TypeCastUtil.java
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oal.rt.util;
+
+public class TypeCastUtil {
+    /**
+     * @param castType           to change the value of given original expression.
+     * @param originalExpression to read the value
+     * @return cast expression if cast type exists and is legal.
+     */
+    public static String withCast(String castType, String originalExpression) {
+        if (castType == null) {
+            return originalExpression;
+        }
+        switch (castType) {
+            case "(str->long)":
+            case "(long)":
+                return "Long.parseLong(" + originalExpression + ")";
+            case "(str->int)":
+            case "(int)":
+                return "Integer.parseInt(" + originalExpression + ")";
+            default:
+                throw new IllegalArgumentException(
+                    "castType:" + castType + " is legal, context expression:" + originalExpression);
+        }
+    }
+}
diff --git a/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/v2/generator/CodeGenModel.java b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/v2/generator/CodeGenModel.java
index 3f92016763..1c0fdf26fb 100644
--- a/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/v2/generator/CodeGenModel.java
+++ b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/v2/generator/CodeGenModel.java
@@ -22,7 +22,6 @@ import java.util.ArrayList;
 import java.util.List;
 import lombok.Builder;
 import lombok.Getter;
-import org.apache.skywalking.oal.v2.model.FilterExpression;
 import org.apache.skywalking.oal.v2.model.MetricDefinition;
 
 /**
@@ -103,10 +102,10 @@ public class CodeGenModel {
     private String metricsClassName;
 
     /**
-     * Filter expressions from OAL script (raw V2 model).
+     * V1-compatible wrapper for filter expressions.
+     * Templates use ${filters.filterExpressions}.
      */
-    @Builder.Default
-    private List<FilterExpression> filters = new ArrayList<>();
+    private FiltersV2 filters;
 
     /**
      * Filter expressions converted for template use.
@@ -134,6 +133,7 @@ public class CodeGenModel {
 
     /**
      * Entrance method information.
+     * Also exposed as 'entryMethod' for V1 template compatibility.
      */
     private EntranceMethodV2 entranceMethod;
 
@@ -142,6 +142,13 @@ public class CodeGenModel {
      */
     private String sourceDecorator;
 
+    /**
+     * V1-compatible getter for entryMethod (alias for entranceMethod).
+     */
+    public EntranceMethodV2 getEntryMethod() {
+        return entranceMethod;
+    }
+
     /**
      * Source field for code generation templates.
      */
@@ -200,7 +207,7 @@ public class CodeGenModel {
     public static class EntranceMethodV2 {
         private String methodName;
         @Builder.Default
-        private List<String> argsExpressions = new ArrayList<>();
+        private List<Object> argsExpressions = new ArrayList<>();
         @Builder.Default
         private List<Integer> argTypes = new ArrayList<>();
     }
@@ -284,4 +291,15 @@ public class CodeGenModel {
         private String sourceName;
         private int sourceScopeId;
     }
+
+    /**
+     * V1-compatible "filters" wrapper for templates.
+     * Templates access ${filters.filterExpressions}.
+     */
+    @Getter
+    @Builder
+    public static class FiltersV2 {
+        @Builder.Default
+        private List<FilterExpressionV2> filterExpressions = new ArrayList<>();
+    }
 }
diff --git a/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/v2/generator/MetricDefinitionEnricher.java b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/v2/generator/MetricDefinitionEnricher.java
index 3778355986..69abfbbdb5 100644
--- a/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/v2/generator/MetricDefinitionEnricher.java
+++ b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/v2/generator/MetricDefinitionEnricher.java
@@ -117,7 +117,9 @@ public class MetricDefinitionEnricher {
                 .build())
             .functionName(metric.getAggregationFunction().getName())
             .metricsClassName(metricsClassName)
-            .filters(metric.getFilters())
+            .filters(CodeGenModel.FiltersV2.builder()
+                .filterExpressions(filterExpressions)
+                .build())
             .filterExpressions(filterExpressions)
             .fieldsFromSource(fieldsFromSource)
             .persistentFields(persistentFields)
@@ -171,7 +173,7 @@ public class MetricDefinitionEnricher {
         CodeGenModel.EntranceMethodV2.EntranceMethodV2Builder builder = CodeGenModel.EntranceMethodV2.builder()
             .methodName(entranceMethod.getName());
 
-        List<String> argsExpressions = new ArrayList<>();
+        List<Object> argsExpressions = new ArrayList<>();
         List<Integer> argTypes = new ArrayList<>();
         int funcArgIndex = 0;
 
@@ -207,15 +209,22 @@ public class MetricDefinitionEnricher {
                 argsExpressions.add(expression);
                 argTypes.add(2); // ATTRIBUTE_EXP_TYPE
             } else if (annotation instanceof ConstOne) {
-                // Use appropriate literal type based on parameter type
+                // Match V1 behavior: always wrap in type cast for consistency
                 if (parameterType.equals(long.class)) {
-                    argsExpressions.add("1L");
+                    argsExpressions.add("(long)(1)");
+                } else if (parameterType.equals(int.class)) {
+                    argsExpressions.add("(int)(1)");
+                } else if (parameterType.equals(double.class)) {
+                    argsExpressions.add("(double)(1)");
+                } else if (parameterType.equals(float.class)) {
+                    argsExpressions.add("(float)(1)");
                 } else {
                     argsExpressions.add("1");
                 }
                 argTypes.add(1); // LITERAL_TYPE
             } else if (annotation instanceof org.apache.skywalking.oap.server.core.analysis.metrics.annotation.Expression) {
-                // Expression argument - convert V2 filter to expression
+                // Expression argument - convert V2 filter to expression object
+                // Template expects arg.expressionObject, arg.left, arg.right when argType >= 3
                 if (funcArgIndex < metric.getAggregationFunction().getArguments().size()) {
                     FunctionArgument funcArg = metric.getAggregationFunction().getArguments().get(funcArgIndex++);
                     if (funcArg.isExpression()) {
@@ -223,7 +232,12 @@ public class MetricDefinitionEnricher {
                         String matcherClass = getMatcherClassName(filterExpr);
                         String left = buildFilterLeft(filterExpr);
                         String right = buildFilterRight(filterExpr);
-                        argsExpressions.add("new " + matcherClass + "().match(" + left + ", " + right + ")");
+                        // Add as FilterExpressionV2 object for template access
+                        argsExpressions.add(CodeGenModel.FilterExpressionV2.builder()
+                            .expressionObject(matcherClass)
+                            .left(left)
+                            .right(right)
+                            .build());
                         argTypes.add(3); // EXPRESSION_TYPE
                     } else {
                         throw new IllegalArgumentException("Expected expression argument but got: " + funcArg);
diff --git a/oap-server/oal-rt/src/main/resources/code-templates-v2/dispatcher/doMetrics.ftl b/oap-server/oal-rt/src/main/resources/code-templates-v2/dispatcher/doMetrics.ftl
index a6e9bf4583..5d82d00197 100644
--- a/oap-server/oal-rt/src/main/resources/code-templates-v2/dispatcher/doMetrics.ftl
+++ b/oap-server/oal-rt/src/main/resources/code-templates-v2/dispatcher/doMetrics.ftl
@@ -1,7 +1,7 @@
-private void do${metricsName}(${sourcePackage}${sourceName} source) {
+private void do${metricsName}(${sourcePackage}${from.sourceName} source) {
 
-<#if filterExpressions?? && filterExpressions?size gt 0>
-    <#list filterExpressions as filterExpression>
+<#if filters.filterExpressions??>
+    <#list filters.filterExpressions as filterExpression>
         if (!new ${filterExpression.expressionObject}().match(${filterExpression.left}, ${filterExpression.right})) {
         return;
         }
@@ -20,9 +20,13 @@ metrics.setTimeBucket(source.getTimeBucket());
         metrics.${field.fieldSetter}(source.${field.fieldGetter}());
     </#if>
 </#list>
-metrics.${entranceMethod.methodName}(
-<#list entranceMethod.argsExpressions as arg>
-    ${arg}<#if arg_has_next>, </#if>
+metrics.${entryMethod.methodName}(
+<#list entryMethod.argsExpressions as arg>
+    <#if entryMethod.argTypes[arg_index] < 3>
+        ${arg}
+    <#else>
+        new ${arg.expressionObject}().match(${arg.left}, ${arg.right})
+    </#if><#if arg_has_next>, </#if>
 </#list>);
 
 org.apache.skywalking.oap.server.core.analysis.worker.MetricsStreamProcessor.getInstance().in(metrics);
diff --git a/oap-server/oal-rt/src/main/resources/code-templates-v2/metrics/serialize.ftl b/oap-server/oal-rt/src/main/resources/code-templates-v2/metrics/serialize.ftl
index 8080003cbc..9f258bceaa 100644
--- a/oap-server/oal-rt/src/main/resources/code-templates-v2/metrics/serialize.ftl
+++ b/oap-server/oal-rt/src/main/resources/code-templates-v2/metrics/serialize.ftl
@@ -1,29 +1,25 @@
 public org.apache.skywalking.oap.server.core.remote.grpc.proto.RemoteData.Builder serialize() {
 org.apache.skywalking.oap.server.core.remote.grpc.proto.RemoteData.Builder remoteBuilder = org.apache.skywalking.oap.server.core.remote.grpc.proto.RemoteData.newBuilder();
-<#if serializeFields.stringFields?? && serializeFields.stringFields?size gt 0>
-    <#list serializeFields.stringFields as field>
-        remoteBuilder.addDataStrings(${field.getter}() != null ? ${field.getter}() : "");
-    </#list>
-</#if>
-<#if serializeFields.longFields?? && serializeFields.longFields?size gt 0>
-    <#list serializeFields.longFields as field>
-        remoteBuilder.addDataLongs(${field.getter}());
-    </#list>
-</#if>
-<#if serializeFields.doubleFields?? && serializeFields.doubleFields?size gt 0>
-    <#list serializeFields.doubleFields as field>
-        remoteBuilder.addDataDoubles(${field.getter}());
-    </#list>
-</#if>
-<#if serializeFields.intFields?? && serializeFields.intFields?size gt 0>
-    <#list serializeFields.intFields as field>
-        remoteBuilder.addDataIntegers(${field.getter}());
-    </#list>
-</#if>
-<#if serializeFields.objectFields?? && serializeFields.objectFields?size gt 0>
-    <#list serializeFields.objectFields as field>
-        remoteBuilder.addDataObjectStrings(${field.getter}().toStorageData());
-    </#list>
-</#if>
+
+<#list serializeFields.stringFields as field>
+    remoteBuilder.addDataStrings(${field.getter}() == null ? "" : ${field.getter}());
+</#list>
+
+<#list serializeFields.longFields as field>
+    remoteBuilder.addDataLongs(${field.getter}());
+</#list>
+
+<#list serializeFields.doubleFields as field>
+    remoteBuilder.addDataDoubles(${field.getter}());
+</#list>
+
+<#list serializeFields.intFields as field>
+    remoteBuilder.addDataIntegers(${field.getter}());
+</#list>
+
+<#list serializeFields.objectFields as field>
+    remoteBuilder.addDataObjectStrings(${field.getter}().toStorageData());
+</#list>
+
 return remoteBuilder;
 }
diff --git a/oap-server/oal-rt/src/test/java/org/apache/skywalking/oal/v2/generator/AllOALScriptsComparisonTest.java b/oap-server/oal-rt/src/test/java/org/apache/skywalking/oal/v2/generator/AllOALScriptsComparisonTest.java
new file mode 100644
index 0000000000..78f94650e7
--- /dev/null
+++ b/oap-server/oal-rt/src/test/java/org/apache/skywalking/oal/v2/generator/AllOALScriptsComparisonTest.java
@@ -0,0 +1,361 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oal.v2.generator;
+
+import java.io.ByteArrayOutputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import javassist.ClassPool;
+import javassist.CtClass;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.skywalking.oal.rt.parser.AnalysisResult;
+import org.apache.skywalking.oal.rt.parser.OALScripts;
+import org.apache.skywalking.oal.rt.parser.ScriptParser;
+import org.apache.skywalking.oal.v2.model.MetricDefinition;
+import org.apache.skywalking.oal.v2.parser.OALScriptParserV2;
+import org.apache.skywalking.oap.server.core.analysis.SourceDecoratorManager;
+import org.apache.skywalking.oap.server.core.oal.rt.OALDefine;
+import org.apache.skywalking.oap.server.core.source.DefaultScopeDefine;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Comprehensive comparison test using all OAL scripts in the project.
+ *
+ * This test loads every OAL script from test resources and verifies that
+ * V1 and V2 engines generate byte-for-byte identical bytecode.
+ */
+@Slf4j
+public class AllOALScriptsComparisonTest {
+
+    private static final String SOURCE_PACKAGE = "org.apache.skywalking.oap.server.core.source.";
+    private static final String METRICS_PACKAGE = "org.apache.skywalking.oap.server.core.source.oal.rt.metrics.";
+
+    private static final String[] OAL_SCRIPTS = {
+        "oal/core.oal",
+        "oal/java-agent.oal",
+        "oal/browser.oal",
+        "oal/mesh.oal",
+        "oal/tcp.oal",
+        "oal/dotnet-agent.oal",
+        "oal/ebpf.oal",
+        "oal/cilium.oal"
+    };
+
+    @BeforeAll
+    public static void initializeScopes() {
+        // Initialize all scope classes needed by the OAL scripts
+        DefaultScopeDefine.Listener listener = new DefaultScopeDefine.Listener();
+
+        // Core sources
+        notifyClass(listener, "Service");
+        notifyClass(listener, "ServiceInstance");
+        notifyClass(listener, "Endpoint");
+        notifyClass(listener, "ServiceRelation");
+        notifyClass(listener, "ServiceInstanceRelation");
+        notifyClass(listener, "EndpointRelation");
+        notifyClass(listener, "DatabaseAccess");
+        notifyClass(listener, "CacheAccess");
+        notifyClass(listener, "MQAccess");
+        notifyClass(listener, "MQEndpointAccess");
+
+        // JVM sources (for java-agent.oal)
+        notifyClass(listener, "ServiceInstanceJVMCPU");
+        notifyClass(listener, "ServiceInstanceJVMMemory");
+        notifyClass(listener, "ServiceInstanceJVMMemoryPool");
+        notifyClass(listener, "ServiceInstanceJVMGC");
+        notifyClass(listener, "ServiceInstanceJVMThread");
+        notifyClass(listener, "ServiceInstanceJVMClass");
+
+        // CLR sources (for dotnet-agent.oal)
+        notifyClass(listener, "ServiceInstanceCLRCPU");
+        notifyClass(listener, "ServiceInstanceCLRGC");
+        notifyClass(listener, "ServiceInstanceCLRThread");
+
+        // Browser sources (different package: browser.source)
+        String browserPackage = "org.apache.skywalking.oap.server.core.browser.source.";
+        notifyClassFromPackage(listener, browserPackage, "BrowserAppTraffic");
+        notifyClassFromPackage(listener, browserPackage, "BrowserAppPageTraffic");
+        notifyClassFromPackage(listener, browserPackage, "BrowserAppSingleVersionTraffic");
+        notifyClassFromPackage(listener, browserPackage, "BrowserAppPerf");
+        notifyClassFromPackage(listener, browserPackage, "BrowserAppPagePerf");
+        notifyClassFromPackage(listener, browserPackage, "BrowserAppSingleVersionPerf");
+        notifyClassFromPackage(listener, browserPackage, "BrowserAppResourcePerf");
+        notifyClassFromPackage(listener, browserPackage, "BrowserAppWebInteractionPerf");
+        notifyClassFromPackage(listener, browserPackage, "BrowserAppWebVitalsPerf");
+        notifyClassFromPackage(listener, browserPackage, "BrowserErrorLog");
+
+        // Mesh sources
+        notifyClass(listener, "ServiceMesh");
+        notifyClass(listener, "ServiceMeshService");
+        notifyClass(listener, "ServiceMeshServiceInstance");
+        notifyClass(listener, "ServiceMeshServiceRelation");
+        notifyClass(listener, "ServiceMeshServiceInstanceRelation");
+
+        // TCP sources
+        notifyClass(listener, "TCPService");
+        notifyClass(listener, "TCPServiceInstance");
+        notifyClass(listener, "TCPServiceRelation");
+        notifyClass(listener, "TCPServiceInstanceRelation");
+
+        // eBPF sources
+        notifyClass(listener, "EBPFProfilingSchedule");
+
+        // Cilium sources
+        notifyClass(listener, "CiliumService");
+        notifyClass(listener, "CiliumServiceInstance");
+        notifyClass(listener, "CiliumEndpoint");
+        notifyClass(listener, "CiliumServiceRelation");
+        notifyClass(listener, "CiliumServiceInstanceRelation");
+
+        // K8s sources
+        notifyClass(listener, "K8SService");
+        notifyClass(listener, "K8SServiceInstance");
+        notifyClass(listener, "K8SEndpoint");
+        notifyClass(listener, "K8SServiceRelation");
+        notifyClass(listener, "K8SServiceInstanceRelation");
+
+        // Process sources
+        notifyClass(listener, "Process");
+        notifyClass(listener, "ProcessRelation");
+
+        // Register decorators (for core.oal, ebpf.oal)
+        registerDecorator("ServiceDecorator");
+        registerDecorator("EndpointDecorator");
+        registerDecorator("K8SServiceDecorator");
+        registerDecorator("K8SEndpointDecorator");
+    }
+
+    private static void registerDecorator(String decoratorName) {
+        try {
+            Class<?> clazz = Class.forName(SOURCE_PACKAGE + decoratorName);
+            SourceDecoratorManager manager = new SourceDecoratorManager();
+            manager.addIfAsSourceDecorator(clazz);
+        } catch (ClassNotFoundException e) {
+            log.debug("Decorator class not found: {}, skipping", decoratorName);
+        } catch (IllegalAccessException | InstantiationException e) {
+            log.debug("Decorator {} instantiation error: {}", decoratorName, e.getMessage());
+        } catch (RuntimeException e) {
+            // Decorator may already be registered or other error
+            log.debug("Decorator {} registration: {}", decoratorName, e.getMessage());
+        }
+    }
+
+    private static void notifyClass(DefaultScopeDefine.Listener listener, String className) {
+        notifyClassFromPackage(listener, SOURCE_PACKAGE, className);
+    }
+
+    private static void notifyClassFromPackage(DefaultScopeDefine.Listener listener, String packageName, String className) {
+        try {
+            Class<?> clazz = Class.forName(packageName + className);
+            listener.notify(clazz);
+        } catch (ClassNotFoundException e) {
+            log.debug("Source class not found: {}{}, skipping", packageName, className);
+        } catch (RuntimeException e) {
+            // Scope may already be registered by other tests (UnexpectedException)
+            log.debug("Scope {} already registered or error: {}", className, e.getMessage());
+        }
+    }
+
+    @Test
+    public void compareAllOALScripts() throws Exception {
+        int totalMetrics = 0;
+        int passedMetrics = 0;
+        int skippedScripts = 0;
+        List<String> failures = new ArrayList<>();
+        List<String> successfulScripts = new ArrayList<>();
+
+        for (String scriptPath : OAL_SCRIPTS) {
+            log.info("=== Testing {} ===", scriptPath);
+
+            String oalContent = loadOALScript(scriptPath);
+            if (oalContent == null) {
+                log.warn("Script not found: {}, skipping", scriptPath);
+                skippedScripts++;
+                continue;
+            }
+
+            try {
+                ComparisonResult result = compareOALScript(scriptPath, oalContent);
+                totalMetrics += result.totalMetrics;
+                passedMetrics += result.passedMetrics;
+                failures.addAll(result.failures);
+
+                if (result.passedMetrics == result.totalMetrics && result.totalMetrics > 0) {
+                    successfulScripts.add(scriptPath + " (" + result.totalMetrics + " metrics)");
+                }
+
+                log.info("{}: {}/{} metrics passed",
+                    scriptPath, result.passedMetrics, result.totalMetrics);
+            } catch (Exception e) {
+                // Scripts that fail to parse due to missing decorators/scopes are skipped
+                failures.add("SKIPPED " + scriptPath + ": " + e.getClass().getSimpleName() + " - " + e.getMessage());
+                skippedScripts++;
+            }
+        }
+
+        log.info("=== Summary ===");
+        log.info("Successful scripts: {}", successfulScripts.size());
+        for (String script : successfulScripts) {
+            log.info("  - {}", script);
+        }
+        log.info("Total metrics compared: {}", totalMetrics);
+        log.info("Passed metrics: {}", passedMetrics);
+        log.info("Skipped scripts (missing deps): {}", skippedScripts);
+
+        if (!failures.isEmpty()) {
+            log.error("Failures:");
+            for (String failure : failures) {
+                log.error("  - {}", failure);
+            }
+        }
+
+        // Build summary for assertion message
+        StringBuilder summary = new StringBuilder();
+        summary.append("\n=== V1 vs V2 OAL Comparison Summary ===\n");
+        summary.append("Successful scripts: ").append(successfulScripts.size()).append("\n");
+        for (String script : successfulScripts) {
+            summary.append("  - ").append(script).append("\n");
+        }
+        summary.append("Total metrics compared: ").append(totalMetrics).append("\n");
+        summary.append("Passed metrics: ").append(passedMetrics).append("\n");
+        summary.append("Skipped scripts: ").append(skippedScripts).append("\n");
+        if (!failures.isEmpty()) {
+            summary.append("Failures: ").append(failures).append("\n");
+        }
+
+        // At least some metrics should be tested
+        Assertions.assertTrue(totalMetrics > 0, "At least some metrics should be testable. " + summary);
+
+        // All testable metrics must produce identical bytecode
+        Assertions.assertEquals(totalMetrics, passedMetrics,
+            "All testable metrics should produce identical bytecode. " + summary);
+
+        // All scripts should be testable now that we've registered scopes and decorators
+        Assertions.assertEquals(0, skippedScripts,
+            "All scripts should be testable. Skips: " + failures);
+    }
+
+    private String loadOALScript(String scriptPath) {
+        try (InputStream is = getClass().getClassLoader().getResourceAsStream(scriptPath)) {
+            if (is == null) {
+                return null;
+            }
+            return new String(is.readAllBytes(), StandardCharsets.UTF_8);
+        } catch (IOException e) {
+            return null;
+        }
+    }
+
+    private ComparisonResult compareOALScript(String scriptPath, String oalContent) throws Exception {
+        ComparisonResult result = new ComparisonResult();
+
+        // Parse with V1
+        ClassPool v1Pool = new ClassPool(true);
+        ScriptParser v1Parser = ScriptParser.createFromScriptText(oalContent, SOURCE_PACKAGE);
+        OALScripts v1Scripts = v1Parser.parse();
+        List<AnalysisResult> v1Results = v1Scripts.getMetricsStmts();
+
+        // Parse with V2
+        ClassPool v2Pool = new ClassPool(true);
+        OALScriptParserV2 v2Parser = OALScriptParserV2.parse(oalContent);
+        List<MetricDefinition> v2Metrics = v2Parser.getMetrics();
+
+        // Verify same number of metrics parsed
+        Assertions.assertEquals(v1Results.size(), v2Metrics.size(),
+            scriptPath + ": V1 and V2 should parse same number of metrics");
+
+        result.totalMetrics = v1Results.size();
+
+        // Enrich V1 results
+        for (AnalysisResult metricsStmt : v1Results) {
+            metricsStmt.setMetricsClassPackage(METRICS_PACKAGE);
+            metricsStmt.setSourcePackage(SOURCE_PACKAGE);
+        }
+
+        // Enrich V2 models
+        MetricDefinitionEnricher enricher = new MetricDefinitionEnricher(SOURCE_PACKAGE, METRICS_PACKAGE);
+        List<CodeGenModel> v2Models = new ArrayList<>();
+        for (MetricDefinition metric : v2Metrics) {
+            v2Models.add(enricher.enrich(metric));
+        }
+
+        // Generate and compare each metric
+        TestOALDefine define = new TestOALDefine();
+        V1ClassGenerator v1Gen = new V1ClassGenerator(define, v1Pool);
+        V2ClassGenerator v2Gen = new V2ClassGenerator(define, v2Pool);
+
+        for (int i = 0; i < v1Results.size(); i++) {
+            AnalysisResult v1Result = v1Results.get(i);
+            CodeGenModel v2Model = v2Models.get(i);
+
+            String metricsName = v1Result.getMetricsName();
+
+            try {
+                // Generate metrics classes
+                CtClass v1MetricsClass = v1Gen.generateMetricsCtClass(v1Result);
+                CtClass v2MetricsClass = v2Gen.generateMetricsCtClass(v2Model);
+
+                // Compare bytecode
+                boolean match = compareBytecode(v1MetricsClass, v2MetricsClass);
+                if (match) {
+                    result.passedMetrics++;
+                } else {
+                    result.failures.add(scriptPath + ":" + metricsName + " - bytecode mismatch");
+                }
+            } catch (Exception e) {
+                result.failures.add(scriptPath + ":" + metricsName + " - " + e.getMessage());
+            }
+        }
+
+        return result;
+    }
+
+    private boolean compareBytecode(CtClass v1Class, CtClass v2Class) throws Exception {
+        ByteArrayOutputStream v1Baos = new ByteArrayOutputStream();
+        ByteArrayOutputStream v2Baos = new ByteArrayOutputStream();
+
+        v1Class.toBytecode(new DataOutputStream(v1Baos));
+        v2Class.toBytecode(new DataOutputStream(v2Baos));
+
+        byte[] v1Bytecode = v1Baos.toByteArray();
+        byte[] v2Bytecode = v2Baos.toByteArray();
+
+        return Arrays.equals(v1Bytecode, v2Bytecode);
+    }
+
+    private static class ComparisonResult {
+        int totalMetrics = 0;
+        int passedMetrics = 0;
+        List<String> failures = new ArrayList<>();
+    }
+
+    private static class TestOALDefine extends OALDefine {
+        protected TestOALDefine() {
+            super("test.oal", SOURCE_PACKAGE);
+        }
+    }
+}
diff --git a/oap-server/oal-rt/src/test/java/org/apache/skywalking/oal/v2/generator/DiagnosticTest.java b/oap-server/oal-rt/src/test/java/org/apache/skywalking/oal/v2/generator/DiagnosticTest.java
new file mode 100644
index 0000000000..4b594d7304
--- /dev/null
+++ b/oap-server/oal-rt/src/test/java/org/apache/skywalking/oal/v2/generator/DiagnosticTest.java
@@ -0,0 +1,122 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oal.v2.generator;
+
+import freemarker.template.Configuration;
+import freemarker.template.Version;
+import java.io.StringWriter;
+import java.util.List;
+import java.util.Locale;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.skywalking.oal.rt.OALRuntime;
+import org.apache.skywalking.oal.rt.parser.AnalysisResult;
+import org.apache.skywalking.oal.rt.parser.OALScripts;
+import org.apache.skywalking.oal.rt.parser.ScriptParser;
+import org.apache.skywalking.oal.v2.model.MetricDefinition;
+import org.apache.skywalking.oal.v2.parser.OALScriptParserV2;
+import org.apache.skywalking.oap.server.core.source.DefaultScopeDefine;
+import org.apache.skywalking.oap.server.core.source.Service;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Diagnostic test to compare generated source code from V1 and V2.
+ */
+@Slf4j
+public class DiagnosticTest {
+
+    private static final String SOURCE_PACKAGE = "org.apache.skywalking.oap.server.core.source.";
+    private static final String METRICS_PACKAGE = "org.apache.skywalking.oap.server.core.source.oal.rt.metrics.";
+
+    @BeforeAll
+    public static void initializeScopes() {
+        try {
+            DefaultScopeDefine.Listener listener = new DefaultScopeDefine.Listener();
+            listener.notify(Service.class);
+        } catch (RuntimeException e) {
+            // Scope may already be registered by other tests
+        }
+    }
+
+    @Test
+    public void compareGeneratedDoMetrics() throws Exception {
+        String oal = "service_cpm = from(Service.*).cpm();";
+
+        // V1
+        String v1Source = generateV1DoMetrics(oal);
+        log.info("=== V1 Generated doMetrics (length={}) ===\n{}", v1Source.length(), v1Source);
+
+        // V2
+        String v2Source = generateV2DoMetrics(oal);
+        log.info("=== V2 Generated doMetrics (length={}) ===\n{}", v2Source.length(), v2Source);
+
+        // Character by character comparison
+        int minLen = Math.min(v1Source.length(), v2Source.length());
+        for (int i = 0; i < minLen; i++) {
+            if (v1Source.charAt(i) != v2Source.charAt(i)) {
+                log.info("First difference at index {}", i);
+                log.info("V1 char: '{}' (code: {})", v1Source.charAt(i), (int) v1Source.charAt(i));
+                log.info("V2 char: '{}' (code: {})", v2Source.charAt(i), (int) v2Source.charAt(i));
+                log.info("V1 context: {}", v1Source.substring(Math.max(0, i - 20), Math.min(v1Source.length(), i + 20)));
+                log.info("V2 context: {}", v2Source.substring(Math.max(0, i - 20), Math.min(v2Source.length(), i + 20)));
+                break;
+            }
+        }
+        if (v1Source.length() != v2Source.length()) {
+            log.info("Length difference: V1={}, V2={}", v1Source.length(), v2Source.length());
+        }
+
+        Assertions.assertEquals(v1Source, v2Source, "V1 and V2 should generate identical source code");
+    }
+
+    private String generateV1DoMetrics(String oal) throws Exception {
+        ScriptParser scriptParser = ScriptParser.createFromScriptText(oal, SOURCE_PACKAGE);
+        OALScripts scripts = scriptParser.parse();
+        List<AnalysisResult> results = scripts.getMetricsStmts();
+
+        AnalysisResult metricsStmt = results.get(0);
+        metricsStmt.setMetricsClassPackage(METRICS_PACKAGE);
+        metricsStmt.setSourcePackage(SOURCE_PACKAGE);
+
+        Configuration configuration = new Configuration(new Version("2.3.28"));
+        configuration.setEncoding(Locale.ENGLISH, "UTF-8");
+        configuration.setClassLoaderForTemplateLoading(OALRuntime.class.getClassLoader(), "/code-templates");
+
+        StringWriter methodEntity = new StringWriter();
+        configuration.getTemplate("dispatcher/doMetrics.ftl").process(metricsStmt, methodEntity);
+        return methodEntity.toString();
+    }
+
+    private String generateV2DoMetrics(String oal) throws Exception {
+        MetricDefinitionEnricher enricher = new MetricDefinitionEnricher(SOURCE_PACKAGE, METRICS_PACKAGE);
+        OALScriptParserV2 parser = OALScriptParserV2.parse(oal);
+        List<MetricDefinition> metrics = parser.getMetrics();
+
+        CodeGenModel model = enricher.enrich(metrics.get(0));
+
+        Configuration configuration = new Configuration(new Version("2.3.28"));
+        configuration.setEncoding(Locale.ENGLISH, "UTF-8");
+        configuration.setClassLoaderForTemplateLoading(OALClassGeneratorV2.class.getClassLoader(), "/code-templates-v2");
+
+        StringWriter methodEntity = new StringWriter();
+        configuration.getTemplate("dispatcher/doMetrics.ftl").process(model, methodEntity);
+        return methodEntity.toString();
+    }
+}
diff --git a/oap-server/oal-rt/src/test/java/org/apache/skywalking/oal/v2/generator/OALClassGeneratorV2Test.java b/oap-server/oal-rt/src/test/java/org/apache/skywalking/oal/v2/generator/OALClassGeneratorV2Test.java
index 49136fe7f9..718c43fc0a 100644
--- a/oap-server/oal-rt/src/test/java/org/apache/skywalking/oal/v2/generator/OALClassGeneratorV2Test.java
+++ b/oap-server/oal-rt/src/test/java/org/apache/skywalking/oal/v2/generator/OALClassGeneratorV2Test.java
@@ -69,10 +69,14 @@ public class OALClassGeneratorV2Test {
 
     @BeforeAll
     public static void initializeScopes() {
-        DefaultScopeDefine.Listener listener = new DefaultScopeDefine.Listener();
-        listener.notify(Service.class);
-        listener.notify(Endpoint.class);
-        listener.notify(ServiceRelation.class);
+        try {
+            DefaultScopeDefine.Listener listener = new DefaultScopeDefine.Listener();
+            listener.notify(Service.class);
+            listener.notify(Endpoint.class);
+            listener.notify(ServiceRelation.class);
+        } catch (RuntimeException e) {
+            // Scopes may already be registered by other tests
+        }
     }
 
     /**
diff --git a/oap-server/oal-rt/src/test/java/org/apache/skywalking/oal/v2/generator/V1ClassGenerator.java b/oap-server/oal-rt/src/test/java/org/apache/skywalking/oal/v2/generator/V1ClassGenerator.java
new file mode 100644
index 0000000000..b20f9e0669
--- /dev/null
+++ b/oap-server/oal-rt/src/test/java/org/apache/skywalking/oal/v2/generator/V1ClassGenerator.java
@@ -0,0 +1,253 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oal.v2.generator;
+
+import freemarker.template.Configuration;
+import freemarker.template.Version;
+import java.io.StringWriter;
+import java.util.Locale;
+import javassist.ClassPool;
+import javassist.CtClass;
+import javassist.CtConstructor;
+import javassist.CtField;
+import javassist.CtNewConstructor;
+import javassist.CtNewMethod;
+import javassist.bytecode.AnnotationsAttribute;
+import javassist.bytecode.ClassFile;
+import javassist.bytecode.ConstPool;
+import javassist.bytecode.SignatureAttribute;
+import javassist.bytecode.annotation.Annotation;
+import javassist.bytecode.annotation.ClassMemberValue;
+import javassist.bytecode.annotation.IntegerMemberValue;
+import javassist.bytecode.annotation.StringMemberValue;
+import org.apache.skywalking.oal.rt.OALRuntime;
+import org.apache.skywalking.oal.rt.output.DispatcherContext;
+import org.apache.skywalking.oal.rt.parser.AnalysisResult;
+import org.apache.skywalking.oal.rt.parser.SourceColumn;
+import org.apache.skywalking.oap.server.core.analysis.SourceDispatcher;
+import org.apache.skywalking.oap.server.core.analysis.Stream;
+import org.apache.skywalking.oap.server.core.oal.rt.OALCompileException;
+import org.apache.skywalking.oap.server.core.oal.rt.OALDefine;
+import org.apache.skywalking.oap.server.core.storage.annotation.BanyanDB;
+import org.apache.skywalking.oap.server.core.storage.annotation.Column;
+import org.apache.skywalking.oap.server.core.storage.annotation.ElasticSearch;
+
+/**
+ * V1 class generator that produces CtClass without loading to JVM.
+ * Used for bytecode comparison tests.
+ */
+public class V1ClassGenerator {
+
+    private static final String METRICS_FUNCTION_PACKAGE = "org.apache.skywalking.oap.server.core.analysis.metrics.";
+    private static final String WITH_METADATA_INTERFACE = "org.apache.skywalking.oap.server.core.analysis.metrics.WithMetadata";
+    private static final String DISPATCHER_INTERFACE = "org.apache.skywalking.oap.server.core.analysis.SourceDispatcher";
+    private static final String METRICS_STREAM_PROCESSOR = "org.apache.skywalking.oap.server.core.analysis.worker.MetricsStreamProcessor";
+    private static final String[] METRICS_CLASS_METHODS = {
+        "id",
+        "hashCode",
+        "remoteHashCode",
+        "equals",
+        "serialize",
+        "deserialize",
+        "getMeta",
+        "toHour",
+        "toDay"
+    };
+
+    private final ClassPool classPool;
+    private final OALDefine oalDefine;
+    private final Configuration configuration;
+
+    public V1ClassGenerator(OALDefine define, ClassPool classPool) {
+        this.classPool = classPool;
+        this.oalDefine = define;
+
+        configuration = new Configuration(new Version("2.3.28"));
+        configuration.setEncoding(Locale.ENGLISH, "UTF-8");
+        configuration.setClassLoaderForTemplateLoading(OALRuntime.class.getClassLoader(), "/code-templates");
+    }
+
+    /**
+     * Generate metrics CtClass without loading to JVM.
+     */
+    public CtClass generateMetricsCtClass(AnalysisResult metricsStmt) throws OALCompileException {
+        String className = metricsClassName(metricsStmt, false);
+        CtClass parentMetricsClass;
+
+        try {
+            parentMetricsClass = classPool.get(METRICS_FUNCTION_PACKAGE + metricsStmt.getMetricsClassName());
+        } catch (Exception e) {
+            throw new OALCompileException("Can't find parent class for " + className, e);
+        }
+
+        CtClass metricsClass = classPool.makeClass(metricsClassName(metricsStmt, true), parentMetricsClass);
+
+        try {
+            metricsClass.addInterface(classPool.get(WITH_METADATA_INTERFACE));
+        } catch (Exception e) {
+            throw new OALCompileException("Can't find WithMetadata interface for " + className, e);
+        }
+
+        ClassFile metricsClassClassFile = metricsClass.getClassFile();
+        ConstPool constPool = metricsClassClassFile.getConstPool();
+
+        // Create empty constructor
+        try {
+            CtConstructor defaultConstructor = CtNewConstructor.make("public " + className + "() {}", metricsClass);
+            metricsClass.addConstructor(defaultConstructor);
+        } catch (Exception e) {
+            throw new OALCompileException("Can't add empty constructor in " + className, e);
+        }
+
+        // Add fields with annotations
+        for (SourceColumn field : metricsStmt.getFieldsFromSource()) {
+            try {
+                CtField newField = CtField.make(
+                    "private " + field.getType().getName() + " " + field.getFieldName() + ";", metricsClass);
+
+                metricsClass.addField(newField);
+                metricsClass.addMethod(CtNewMethod.getter(field.getFieldGetter(), newField));
+                metricsClass.addMethod(CtNewMethod.setter(field.getFieldSetter(), newField));
+
+                AnnotationsAttribute annotationsAttribute = new AnnotationsAttribute(
+                    constPool, AnnotationsAttribute.visibleTag);
+
+                // Add @Column annotation
+                Annotation columnAnnotation = new Annotation(Column.class.getName(), constPool);
+                columnAnnotation.addMemberValue("name", new StringMemberValue(field.getColumnName(), constPool));
+                if (field.getType().equals(String.class)) {
+                    columnAnnotation.addMemberValue("length", new IntegerMemberValue(constPool, field.getLength()));
+                }
+                annotationsAttribute.addAnnotation(columnAnnotation);
+
+                if (field.isID()) {
+                    Annotation banyanSeriesIDAnnotation = new Annotation(BanyanDB.SeriesID.class.getName(), constPool);
+                    banyanSeriesIDAnnotation.addMemberValue("index", new IntegerMemberValue(constPool, 0));
+                    annotationsAttribute.addAnnotation(banyanSeriesIDAnnotation);
+
+                    Annotation enableDocValuesAnnotation = new Annotation(ElasticSearch.EnableDocValues.class.getName(), constPool);
+                    annotationsAttribute.addAnnotation(enableDocValuesAnnotation);
+                }
+
+                if (field.isShardingKey()) {
+                    Annotation banyanShardingKeyAnnotation = new Annotation(BanyanDB.ShardingKey.class.getName(), constPool);
+                    banyanShardingKeyAnnotation.addMemberValue("index", new IntegerMemberValue(constPool, field.getShardingKeyIdx()));
+                    annotationsAttribute.addAnnotation(banyanShardingKeyAnnotation);
+                }
+
+                newField.getFieldInfo().addAttribute(annotationsAttribute);
+            } catch (Exception e) {
+                throw new OALCompileException("Can't add field " + field.getFieldName() + " in " + className, e);
+            }
+        }
+
+        // Generate methods using V1 templates
+        for (String method : METRICS_CLASS_METHODS) {
+            StringWriter methodEntity = new StringWriter();
+            try {
+                configuration.getTemplate("metrics/" + method + ".ftl").process(metricsStmt, methodEntity);
+                metricsClass.addMethod(CtNewMethod.make(methodEntity.toString(), metricsClass));
+            } catch (Exception e) {
+                throw new OALCompileException("Can't generate method " + method + " for " + className, e);
+            }
+        }
+
+        // Add @Stream annotation
+        AnnotationsAttribute annotationsAttribute = new AnnotationsAttribute(
+            constPool, AnnotationsAttribute.visibleTag);
+        Annotation streamAnnotation = new Annotation(Stream.class.getName(), constPool);
+        streamAnnotation.addMemberValue("name", new StringMemberValue(metricsStmt.getTableName(), constPool));
+        streamAnnotation.addMemberValue("scopeId", new IntegerMemberValue(constPool, metricsStmt.getFrom().getSourceScopeId()));
+        streamAnnotation.addMemberValue(
+            "builder", new ClassMemberValue(metricsBuilderClassName(metricsStmt, true), constPool));
+        streamAnnotation.addMemberValue("processor", new ClassMemberValue(METRICS_STREAM_PROCESSOR, constPool));
+
+        annotationsAttribute.addAnnotation(streamAnnotation);
+        metricsClassClassFile.addAttribute(annotationsAttribute);
+
+        return metricsClass;
+    }
+
+    /**
+     * Generate dispatcher CtClass without loading to JVM.
+     */
+    public CtClass generateDispatcherCtClass(String scopeName, DispatcherContext dispatcherContext) throws OALCompileException {
+        String className = dispatcherClassName(scopeName, false);
+        CtClass dispatcherClass = classPool.makeClass(dispatcherClassName(scopeName, true));
+
+        try {
+            CtClass dispatcherInterface = classPool.get(DISPATCHER_INTERFACE);
+            dispatcherClass.addInterface(dispatcherInterface);
+
+            // Set generic signature
+            String sourceClassName = oalDefine.getSourcePackage() + dispatcherContext.getSource();
+            SignatureAttribute.ClassSignature dispatcherSignature =
+                new SignatureAttribute.ClassSignature(
+                    null, null,
+                    new SignatureAttribute.ClassType[]{
+                        new SignatureAttribute.ClassType(
+                            SourceDispatcher.class.getCanonicalName(),
+                            new SignatureAttribute.TypeArgument[]{
+                                new SignatureAttribute.TypeArgument(
+                                    new SignatureAttribute.ClassType(sourceClassName))
+                            }
+                        )
+                    }
+                );
+
+            dispatcherClass.setGenericSignature(dispatcherSignature.encode());
+        } catch (Exception e) {
+            throw new OALCompileException("Can't find Dispatcher interface for " + className, e);
+        }
+
+        // Generate doMetrics methods for each metric
+        for (AnalysisResult dispatcherContextMetric : dispatcherContext.getMetrics()) {
+            StringWriter methodEntity = new StringWriter();
+            try {
+                configuration.getTemplate("dispatcher/doMetrics.ftl").process(dispatcherContextMetric, methodEntity);
+                dispatcherClass.addMethod(CtNewMethod.make(methodEntity.toString(), dispatcherClass));
+            } catch (Exception e) {
+                throw new OALCompileException("Can't generate method do" + dispatcherContextMetric.getMetricsName() + " for " + className, e);
+            }
+        }
+
+        // Generate dispatch method
+        try {
+            StringWriter methodEntity = new StringWriter();
+            configuration.getTemplate("dispatcher/dispatch.ftl").process(dispatcherContext, methodEntity);
+            dispatcherClass.addMethod(CtNewMethod.make(methodEntity.toString(), dispatcherClass));
+        } catch (Exception e) {
+            throw new OALCompileException("Can't generate method dispatch for " + className, e);
+        }
+
+        return dispatcherClass;
+    }
+
+    private String metricsClassName(AnalysisResult metricsStmt, boolean fullName) {
+        return (fullName ? oalDefine.getDynamicMetricsClassPackage() : "") + metricsStmt.getMetricsName() + "Metrics";
+    }
+
+    private String metricsBuilderClassName(AnalysisResult metricsStmt, boolean fullName) {
+        return (fullName ? oalDefine.getDynamicMetricsBuilderClassPackage() : "") + metricsStmt.getMetricsName() + "MetricsBuilder";
+    }
+
+    private String dispatcherClassName(String scopeName, boolean fullName) {
+        return (fullName ? oalDefine.getDynamicDispatcherClassPackage() : "") + scopeName + "Dispatcher";
+    }
+}
diff --git a/oap-server/oal-rt/src/test/java/org/apache/skywalking/oal/v2/generator/V1V2ComparisonTest.java b/oap-server/oal-rt/src/test/java/org/apache/skywalking/oal/v2/generator/V1V2ComparisonTest.java
new file mode 100644
index 0000000000..d9bf34b7d9
--- /dev/null
+++ b/oap-server/oal-rt/src/test/java/org/apache/skywalking/oal/v2/generator/V1V2ComparisonTest.java
@@ -0,0 +1,551 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oal.v2.generator;
+
+import java.io.ByteArrayOutputStream;
+import java.io.DataOutputStream;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+
+import javassist.ClassPool;
+import javassist.CtClass;
+import javassist.CtField;
+import javassist.CtMethod;
+import javassist.bytecode.AnnotationsAttribute;
+import javassist.bytecode.FieldInfo;
+import javassist.bytecode.MethodInfo;
+import javassist.bytecode.annotation.Annotation;
+import javassist.bytecode.annotation.MemberValue;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.skywalking.oal.rt.output.AllDispatcherContext;
+import org.apache.skywalking.oal.rt.output.DispatcherContext;
+import org.apache.skywalking.oal.rt.parser.AnalysisResult;
+import org.apache.skywalking.oal.rt.parser.OALScripts;
+import org.apache.skywalking.oal.rt.parser.ScriptParser;
+import org.apache.skywalking.oal.v2.model.MetricDefinition;
+import org.apache.skywalking.oal.v2.parser.OALScriptParserV2;
+import org.apache.skywalking.oap.server.core.oal.rt.OALDefine;
+import org.apache.skywalking.oap.server.core.source.DefaultScopeDefine;
+import org.apache.skywalking.oap.server.core.source.Service;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Deep comparison test between V1 and V2 OAL engines.
+ *
+ * Uses isolated ClassPools to generate the same OAL scripts with both engines,
+ * then compares the generated bytecode in detail:
+ * - Class annotations
+ * - Fields and their annotations
+ * - Methods (signatures and bytecode)
+ *
+ * Both engines should produce identical bytecode for the same OAL input.
+ */
+@Slf4j
+public class V1V2ComparisonTest {
+
+    private static final String SOURCE_PACKAGE = "org.apache.skywalking.oap.server.core.source.";
+    private static final String METRICS_PACKAGE = "org.apache.skywalking.oap.server.core.source.oal.rt.metrics.";
+
+    @BeforeAll
+    public static void initializeScopes() {
+        try {
+            DefaultScopeDefine.Listener listener = new DefaultScopeDefine.Listener();
+            listener.notify(Service.class);
+        } catch (RuntimeException e) {
+            // Scope may already be registered by other tests
+        }
+    }
+
+    /**
+     * Test that V1 and V2 generate identical metrics classes for simple OAL.
+     */
+    @Test
+    public void testMetricsClassIdentical() throws Exception {
+        String oal = "service_cpm = from(Service.*).cpm();";
+
+        // Generate with V1 using isolated ClassPool
+        ClassPool v1Pool = new ClassPool(true);
+        V1GeneratedClasses v1Classes = generateWithV1(oal, v1Pool);
+
+        // Generate with V2 using isolated ClassPool
+        ClassPool v2Pool = new ClassPool(true);
+        V2GeneratedClasses v2Classes = generateWithV2(oal, v2Pool);
+
+        // Compare metrics classes
+        Assertions.assertEquals(1, v1Classes.metricsClasses.size(), "V1 should generate 1 metrics class");
+        Assertions.assertEquals(1, v2Classes.metricsClasses.size(), "V2 should generate 1 metrics class");
+
+        CtClass v1Metrics = v1Classes.metricsClasses.get(0);
+        CtClass v2Metrics = v2Classes.metricsClasses.get(0);
+
+        log.info("=== Comparing Metrics Classes ===");
+        log.info("V1: {}", v1Metrics.getName());
+        log.info("V2: {}", v2Metrics.getName());
+
+        compareClasses(v1Metrics, v2Metrics, "Metrics");
+    }
+
+    /**
+     * Test that V1 and V2 generate identical dispatcher classes.
+     */
+    @Test
+    public void testDispatcherClassIdentical() throws Exception {
+        String oal = "service_cpm = from(Service.*).cpm();";
+
+        ClassPool v1Pool = new ClassPool(true);
+        V1GeneratedClasses v1Classes = generateWithV1(oal, v1Pool);
+
+        ClassPool v2Pool = new ClassPool(true);
+        V2GeneratedClasses v2Classes = generateWithV2(oal, v2Pool);
+
+        Assertions.assertEquals(1, v1Classes.dispatcherClasses.size(), "V1 should generate 1 dispatcher class");
+        Assertions.assertEquals(1, v2Classes.dispatcherClasses.size(), "V2 should generate 1 dispatcher class");
+
+        CtClass v1Dispatcher = v1Classes.dispatcherClasses.get(0);
+        CtClass v2Dispatcher = v2Classes.dispatcherClasses.get(0);
+
+        log.info("=== Comparing Dispatcher Classes ===");
+        log.info("V1: {}", v1Dispatcher.getName());
+        log.info("V2: {}", v2Dispatcher.getName());
+
+        compareClasses(v1Dispatcher, v2Dispatcher, "Dispatcher");
+    }
+
+    /**
+     * Test complex OAL with longAvg function.
+     */
+    @Test
+    public void testLongAvgMetricsIdentical() throws Exception {
+        String oal = "service_resp_time = from(Service.latency).longAvg();";
+
+        ClassPool v1Pool = new ClassPool(true);
+        V1GeneratedClasses v1Classes = generateWithV1(oal, v1Pool);
+
+        ClassPool v2Pool = new ClassPool(true);
+        V2GeneratedClasses v2Classes = generateWithV2(oal, v2Pool);
+
+        CtClass v1Metrics = v1Classes.metricsClasses.get(0);
+        CtClass v2Metrics = v2Classes.metricsClasses.get(0);
+
+        log.info("=== Comparing LongAvg Metrics Classes ===");
+        compareClasses(v1Metrics, v2Metrics, "LongAvgMetrics");
+    }
+
+    /**
+     * Test OAL with filter expression.
+     */
+    @Test
+    public void testFilteredMetricsIdentical() throws Exception {
+        String oal = "service_cpm_valid = from(Service.*).filter(status == true).cpm();";
+
+        ClassPool v1Pool = new ClassPool(true);
+        V1GeneratedClasses v1Classes = generateWithV1(oal, v1Pool);
+
+        ClassPool v2Pool = new ClassPool(true);
+        V2GeneratedClasses v2Classes = generateWithV2(oal, v2Pool);
+
+        CtClass v1Metrics = v1Classes.metricsClasses.get(0);
+        CtClass v2Metrics = v2Classes.metricsClasses.get(0);
+
+        log.info("=== Comparing Filtered Metrics Classes ===");
+        compareClasses(v1Metrics, v2Metrics, "FilteredMetrics");
+
+        // Also compare dispatcher (filter logic is in doMetrics method)
+        CtClass v1Dispatcher = v1Classes.dispatcherClasses.get(0);
+        CtClass v2Dispatcher = v2Classes.dispatcherClasses.get(0);
+
+        log.info("=== Comparing Filtered Dispatcher Classes ===");
+        compareClasses(v1Dispatcher, v2Dispatcher, "FilteredDispatcher");
+    }
+
+    /**
+     * Test multiple metrics from same source.
+     */
+    @Test
+    public void testMultipleMetricsIdentical() throws Exception {
+        String oal = "service_cpm = from(Service.*).cpm();\n"
+            + "service_resp_time = from(Service.latency).longAvg();";
+
+        ClassPool v1Pool = new ClassPool(true);
+        V1GeneratedClasses v1Classes = generateWithV1(oal, v1Pool);
+
+        ClassPool v2Pool = new ClassPool(true);
+        V2GeneratedClasses v2Classes = generateWithV2(oal, v2Pool);
+
+        Assertions.assertEquals(2, v1Classes.metricsClasses.size());
+        Assertions.assertEquals(2, v2Classes.metricsClasses.size());
+
+        // Sort by name for consistent comparison
+        v1Classes.metricsClasses.sort((a, b) -> a.getName().compareTo(b.getName()));
+        v2Classes.metricsClasses.sort((a, b) -> a.getName().compareTo(b.getName()));
+
+        for (int i = 0; i < v1Classes.metricsClasses.size(); i++) {
+            log.info("=== Comparing Multiple Metrics {} ===", i);
+            compareClasses(v1Classes.metricsClasses.get(i), v2Classes.metricsClasses.get(i), "MultiMetrics" + i);
+        }
+
+        // Should have only 1 dispatcher (both metrics from Service)
+        Assertions.assertEquals(1, v1Classes.dispatcherClasses.size());
+        Assertions.assertEquals(1, v2Classes.dispatcherClasses.size());
+
+        compareClasses(v1Classes.dispatcherClasses.get(0), v2Classes.dispatcherClasses.get(0), "MultiDispatcher");
+    }
+
+    private void compareClasses(CtClass v1Class, CtClass v2Class, String context) throws Exception {
+        // 1. Compare class names
+        Assertions.assertEquals(v1Class.getName(), v2Class.getName(),
+            context + ": Class names should match");
+        log.info("{}: Class name matches: {}", context, v1Class.getName());
+
+        // 2. Compare superclass
+        Assertions.assertEquals(
+            v1Class.getSuperclass() != null ? v1Class.getSuperclass().getName() : null,
+            v2Class.getSuperclass() != null ? v2Class.getSuperclass().getName() : null,
+            context + ": Superclass should match");
+        log.info("{}: Superclass matches: {}", context,
+            v1Class.getSuperclass() != null ? v1Class.getSuperclass().getName() : "none");
+
+        // 3. Compare interfaces
+        String[] v1Interfaces = Arrays.stream(v1Class.getInterfaces())
+            .map(CtClass::getName).sorted().toArray(String[]::new);
+        String[] v2Interfaces = Arrays.stream(v2Class.getInterfaces())
+            .map(CtClass::getName).sorted().toArray(String[]::new);
+        Assertions.assertArrayEquals(v1Interfaces, v2Interfaces,
+            context + ": Interfaces should match");
+        log.info("{}: Interfaces match: {}", context, Arrays.toString(v1Interfaces));
+
+        // 4. Compare class-level annotations
+        compareClassAnnotations(v1Class, v2Class, context);
+
+        // 5. Compare fields
+        compareFields(v1Class, v2Class, context);
+
+        // 6. Compare methods
+        compareMethods(v1Class, v2Class, context);
+
+        // 7. Compare bytecode
+        compareBytecode(v1Class, v2Class, context);
+    }
+
+    private void compareClassAnnotations(CtClass v1Class, CtClass v2Class, String context) {
+        AnnotationsAttribute v1Annos = (AnnotationsAttribute) v1Class.getClassFile()
+            .getAttribute(AnnotationsAttribute.visibleTag);
+        AnnotationsAttribute v2Annos = (AnnotationsAttribute) v2Class.getClassFile()
+            .getAttribute(AnnotationsAttribute.visibleTag);
+
+        if (v1Annos == null && v2Annos == null) {
+            log.info("{}: No class-level annotations", context);
+            return;
+        }
+
+        Assertions.assertEquals(v1Annos != null, v2Annos != null,
+            context + ": Both should have or not have class annotations");
+
+        if (v1Annos != null) {
+            Annotation[] v1AnnoArray = v1Annos.getAnnotations();
+            Annotation[] v2AnnoArray = v2Annos.getAnnotations();
+
+            Assertions.assertEquals(v1AnnoArray.length, v2AnnoArray.length,
+                context + ": Annotation count should match");
+
+            for (int i = 0; i < v1AnnoArray.length; i++) {
+                compareAnnotation(v1AnnoArray[i], v2AnnoArray[i], context + " class annotation");
+            }
+            log.info("{}: Class annotations match (count={})", context, v1AnnoArray.length);
+        }
+    }
+
+    private void compareAnnotation(Annotation v1Anno, Annotation v2Anno, String context) {
+        Assertions.assertEquals(v1Anno.getTypeName(), v2Anno.getTypeName(),
+            context + ": Annotation type should match");
+
+        // Compare member values
+        if (v1Anno.getMemberNames() == null && v2Anno.getMemberNames() == null) {
+            return;
+        }
+
+        Assertions.assertEquals(
+            v1Anno.getMemberNames() != null ? v1Anno.getMemberNames().size() : 0,
+            v2Anno.getMemberNames() != null ? v2Anno.getMemberNames().size() : 0,
+            context + ": Annotation member count should match");
+
+        if (v1Anno.getMemberNames() != null) {
+            for (String memberName : v1Anno.getMemberNames()) {
+                MemberValue v1Value = v1Anno.getMemberValue(memberName);
+                MemberValue v2Value = v2Anno.getMemberValue(memberName);
+                Assertions.assertNotNull(v2Value,
+                    context + ": V2 should have member " + memberName);
+                Assertions.assertEquals(v1Value.toString(), v2Value.toString(),
+                    context + ": Member " + memberName + " should match");
+            }
+        }
+    }
+
+    private void compareFields(CtClass v1Class, CtClass v2Class, String context) throws Exception {
+        CtField[] v1Fields = v1Class.getDeclaredFields();
+        CtField[] v2Fields = v2Class.getDeclaredFields();
+
+        // Sort fields by name for consistent comparison
+        Map<String, CtField> v1FieldMap = new TreeMap<>();
+        Map<String, CtField> v2FieldMap = new TreeMap<>();
+
+        for (CtField f : v1Fields) {
+            v1FieldMap.put(f.getName(), f);
+        }
+        for (CtField f : v2Fields) {
+            v2FieldMap.put(f.getName(), f);
+        }
+
+        Assertions.assertEquals(v1FieldMap.keySet(), v2FieldMap.keySet(),
+            context + ": Field names should match");
+
+        for (String fieldName : v1FieldMap.keySet()) {
+            CtField v1Field = v1FieldMap.get(fieldName);
+            CtField v2Field = v2FieldMap.get(fieldName);
+
+            // Compare field type
+            Assertions.assertEquals(v1Field.getType().getName(), v2Field.getType().getName(),
+                context + ": Field " + fieldName + " type should match");
+
+            // Compare field modifiers
+            Assertions.assertEquals(v1Field.getModifiers(), v2Field.getModifiers(),
+                context + ": Field " + fieldName + " modifiers should match");
+
+            // Compare field annotations
+            FieldInfo v1FieldInfo = v1Field.getFieldInfo();
+            FieldInfo v2FieldInfo = v2Field.getFieldInfo();
+
+            AnnotationsAttribute v1FieldAnnos = (AnnotationsAttribute) v1FieldInfo
+                .getAttribute(AnnotationsAttribute.visibleTag);
+            AnnotationsAttribute v2FieldAnnos = (AnnotationsAttribute) v2FieldInfo
+                .getAttribute(AnnotationsAttribute.visibleTag);
+
+            if (v1FieldAnnos != null && v2FieldAnnos != null) {
+                Annotation[] v1AnnoArray = v1FieldAnnos.getAnnotations();
+                Annotation[] v2AnnoArray = v2FieldAnnos.getAnnotations();
+
+                // Sort annotations by type name for consistent comparison
+                Arrays.sort(v1AnnoArray, (a, b) -> a.getTypeName().compareTo(b.getTypeName()));
+                Arrays.sort(v2AnnoArray, (a, b) -> a.getTypeName().compareTo(b.getTypeName()));
+
+                Assertions.assertEquals(v1AnnoArray.length, v2AnnoArray.length,
+                    context + ": Field " + fieldName + " annotation count should match");
+
+                for (int i = 0; i < v1AnnoArray.length; i++) {
+                    compareAnnotation(v1AnnoArray[i], v2AnnoArray[i],
+                        context + " field " + fieldName);
+                }
+            }
+        }
+
+        log.info("{}: Fields match (count={})", context, v1FieldMap.size());
+    }
+
+    private void compareMethods(CtClass v1Class, CtClass v2Class, String context) throws Exception {
+        CtMethod[] v1Methods = v1Class.getDeclaredMethods();
+        CtMethod[] v2Methods = v2Class.getDeclaredMethods();
+
+        // Map methods by signature for comparison
+        Map<String, CtMethod> v1MethodMap = new TreeMap<>();
+        Map<String, CtMethod> v2MethodMap = new TreeMap<>();
+
+        for (CtMethod m : v1Methods) {
+            String sig = m.getName() + m.getSignature();
+            v1MethodMap.put(sig, m);
+        }
+        for (CtMethod m : v2Methods) {
+            String sig = m.getName() + m.getSignature();
+            v2MethodMap.put(sig, m);
+        }
+
+        Assertions.assertEquals(v1MethodMap.keySet(), v2MethodMap.keySet(),
+            context + ": Method signatures should match. V1: " + v1MethodMap.keySet()
+                + ", V2: " + v2MethodMap.keySet());
+
+        for (String sig : v1MethodMap.keySet()) {
+            CtMethod v1Method = v1MethodMap.get(sig);
+            CtMethod v2Method = v2MethodMap.get(sig);
+
+            // Compare method modifiers
+            Assertions.assertEquals(v1Method.getModifiers(), v2Method.getModifiers(),
+                context + ": Method " + sig + " modifiers should match");
+
+            // Compare method bytecode
+            MethodInfo v1MethodInfo = v1Method.getMethodInfo();
+            MethodInfo v2MethodInfo = v2Method.getMethodInfo();
+
+            // Compare code attribute (bytecode)
+            if (v1MethodInfo.getCodeAttribute() != null && v2MethodInfo.getCodeAttribute() != null) {
+                byte[] v1Code = v1MethodInfo.getCodeAttribute().getCode();
+                byte[] v2Code = v2MethodInfo.getCodeAttribute().getCode();
+
+                Assertions.assertArrayEquals(v1Code, v2Code,
+                    context + ": Method " + sig + " bytecode should match");
+            }
+        }
+
+        log.info("{}: Methods match (count={})", context, v1MethodMap.size());
+    }
+
+    private void compareBytecode(CtClass v1Class, CtClass v2Class, String context) throws Exception {
+        ByteArrayOutputStream v1Baos = new ByteArrayOutputStream();
+        ByteArrayOutputStream v2Baos = new ByteArrayOutputStream();
+
+        v1Class.toBytecode(new DataOutputStream(v1Baos));
+        v2Class.toBytecode(new DataOutputStream(v2Baos));
+
+        byte[] v1Bytecode = v1Baos.toByteArray();
+        byte[] v2Bytecode = v2Baos.toByteArray();
+
+        Assertions.assertEquals(v1Bytecode.length, v2Bytecode.length,
+            context + ": Bytecode length should match");
+
+        Assertions.assertArrayEquals(v1Bytecode, v2Bytecode,
+            context + ": Complete bytecode should be identical");
+
+        log.info("{}: Complete bytecode identical ({} bytes)", context, v1Bytecode.length);
+    }
+
+    /**
+     * Generate classes with V1 engine using isolated ClassPool.
+     */
+    private V1GeneratedClasses generateWithV1(String oal, ClassPool classPool) throws Exception {
+        V1OALDefine define = new V1OALDefine();
+
+        ScriptParser scriptParser = ScriptParser.createFromScriptText(oal, SOURCE_PACKAGE);
+        OALScripts scripts = scriptParser.parse();
+
+        // OALListener already applies DeepAnalysis during parsing
+        List<AnalysisResult> results = scripts.getMetricsStmts();
+
+        // Build dispatcher context
+        AllDispatcherContext allDispatcherContext = new AllDispatcherContext();
+        for (AnalysisResult metricsStmt : results) {
+            String sourceName = metricsStmt.getFrom().getSourceName();
+            DispatcherContext context = allDispatcherContext.getAllContext().computeIfAbsent(sourceName, name -> {
+                DispatcherContext absent = new DispatcherContext();
+                absent.setSourcePackage(define.getSourcePackage());
+                absent.setSource(name);
+                absent.setPackageName(name.toLowerCase());
+                absent.setSourceDecorator(metricsStmt.getSourceDecorator());
+                return absent;
+            });
+            metricsStmt.setMetricsClassPackage(define.getDynamicMetricsClassPackage());
+            metricsStmt.setSourcePackage(define.getSourcePackage());
+            context.getMetrics().add(metricsStmt);
+        }
+
+        // Generate classes using V1 generator (CtClass only, no JVM loading)
+        V1ClassGenerator generator = new V1ClassGenerator(define, classPool);
+        V1GeneratedClasses classes = new V1GeneratedClasses();
+
+        for (AnalysisResult metricsStmt : results) {
+            classes.metricsClasses.add(generator.generateMetricsCtClass(metricsStmt));
+        }
+
+        for (Map.Entry<String, DispatcherContext> entry : allDispatcherContext.getAllContext().entrySet()) {
+            classes.dispatcherClasses.add(generator.generateDispatcherCtClass(entry.getKey(), entry.getValue()));
+        }
+
+        return classes;
+    }
+
+    /**
+     * Generate classes with V2 engine using isolated ClassPool.
+     */
+    private V2GeneratedClasses generateWithV2(String oal, ClassPool classPool) throws Exception {
+        V2OALDefine define = new V2OALDefine();
+        MetricDefinitionEnricher enricher = new MetricDefinitionEnricher(SOURCE_PACKAGE, METRICS_PACKAGE);
+        OALScriptParserV2 parser = OALScriptParserV2.parse(oal);
+
+        List<CodeGenModel> codeGenModels = new ArrayList<>();
+        for (MetricDefinition metric : parser.getMetrics()) {
+            CodeGenModel model = enricher.enrich(metric);
+            codeGenModels.add(model);
+        }
+
+        // Build dispatcher context
+        Map<String, OALClassGeneratorV2.DispatcherContextV2> dispatcherContextMap = new java.util.HashMap<>();
+        for (CodeGenModel model : codeGenModels) {
+            String sourceName = model.getSourceName();
+            OALClassGeneratorV2.DispatcherContextV2 context = dispatcherContextMap.computeIfAbsent(sourceName, name -> {
+                OALClassGeneratorV2.DispatcherContextV2 ctx = new OALClassGeneratorV2.DispatcherContextV2();
+                ctx.setSourcePackage(define.getSourcePackage());
+                ctx.setSourceName(name);
+                ctx.setPackageName(name.toLowerCase());
+                ctx.setSourceDecorator(model.getSourceDecorator());
+                return ctx;
+            });
+            context.getMetrics().add(model);
+        }
+
+        // Generate classes using V2 generator (CtClass only, no JVM loading)
+        V2ClassGenerator generator = new V2ClassGenerator(define, classPool);
+        V2GeneratedClasses classes = new V2GeneratedClasses();
+
+        for (CodeGenModel model : codeGenModels) {
+            classes.metricsClasses.add(generator.generateMetricsCtClass(model));
+        }
+
+        for (Map.Entry<String, OALClassGeneratorV2.DispatcherContextV2> entry : dispatcherContextMap.entrySet()) {
+            classes.dispatcherClasses.add(generator.generateDispatcherCtClass(entry.getKey(), entry.getValue()));
+        }
+
+        return classes;
+    }
+
+    /**
+     * Container for V1 generated classes.
+     */
+    private static class V1GeneratedClasses {
+        List<CtClass> metricsClasses = new ArrayList<>();
+        List<CtClass> dispatcherClasses = new ArrayList<>();
+    }
+
+    /**
+     * Container for V2 generated classes.
+     */
+    private static class V2GeneratedClasses {
+        List<CtClass> metricsClasses = new ArrayList<>();
+        List<CtClass> dispatcherClasses = new ArrayList<>();
+    }
+
+    /**
+     * OALDefine for V1 engine tests.
+     */
+    private static class V1OALDefine extends OALDefine {
+        protected V1OALDefine() {
+            super("test-v1.oal", SOURCE_PACKAGE);
+        }
+    }
+
+    /**
+     * OALDefine for V2 engine tests.
+     */
+    private static class V2OALDefine extends OALDefine {
+        protected V2OALDefine() {
+            super("test-v2.oal", SOURCE_PACKAGE);
+        }
+    }
+}
diff --git a/oap-server/oal-rt/src/test/java/org/apache/skywalking/oal/v2/generator/V2ClassGenerator.java b/oap-server/oal-rt/src/test/java/org/apache/skywalking/oal/v2/generator/V2ClassGenerator.java
new file mode 100644
index 0000000000..8760bfb694
--- /dev/null
+++ b/oap-server/oal-rt/src/test/java/org/apache/skywalking/oal/v2/generator/V2ClassGenerator.java
@@ -0,0 +1,250 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oal.v2.generator;
+
+import freemarker.template.Configuration;
+import freemarker.template.Version;
+import java.io.StringWriter;
+import java.util.Locale;
+import javassist.ClassPool;
+import javassist.CtClass;
+import javassist.CtConstructor;
+import javassist.CtField;
+import javassist.CtNewConstructor;
+import javassist.CtNewMethod;
+import javassist.bytecode.AnnotationsAttribute;
+import javassist.bytecode.ClassFile;
+import javassist.bytecode.ConstPool;
+import javassist.bytecode.SignatureAttribute;
+import javassist.bytecode.annotation.Annotation;
+import javassist.bytecode.annotation.ClassMemberValue;
+import javassist.bytecode.annotation.IntegerMemberValue;
+import javassist.bytecode.annotation.StringMemberValue;
+import org.apache.skywalking.oap.server.core.analysis.SourceDispatcher;
+import org.apache.skywalking.oap.server.core.analysis.Stream;
+import org.apache.skywalking.oap.server.core.oal.rt.OALCompileException;
+import org.apache.skywalking.oap.server.core.oal.rt.OALDefine;
+import org.apache.skywalking.oap.server.core.storage.annotation.BanyanDB;
+import org.apache.skywalking.oap.server.core.storage.annotation.Column;
+import org.apache.skywalking.oap.server.core.storage.annotation.ElasticSearch;
+
+/**
+ * V2 class generator that produces CtClass without loading to JVM.
+ * Used for bytecode comparison tests.
+ */
+public class V2ClassGenerator {
+
+    private static final String METRICS_FUNCTION_PACKAGE = "org.apache.skywalking.oap.server.core.analysis.metrics.";
+    private static final String WITH_METADATA_INTERFACE = "org.apache.skywalking.oap.server.core.analysis.metrics.WithMetadata";
+    private static final String DISPATCHER_INTERFACE = "org.apache.skywalking.oap.server.core.analysis.SourceDispatcher";
+    private static final String METRICS_STREAM_PROCESSOR = "org.apache.skywalking.oap.server.core.analysis.worker.MetricsStreamProcessor";
+    private static final String[] METRICS_CLASS_METHODS = {
+        "id",
+        "hashCode",
+        "remoteHashCode",
+        "equals",
+        "serialize",
+        "deserialize",
+        "getMeta",
+        "toHour",
+        "toDay"
+    };
+
+    private final ClassPool classPool;
+    private final OALDefine oalDefine;
+    private final Configuration configuration;
+
+    public V2ClassGenerator(OALDefine define, ClassPool classPool) {
+        this.classPool = classPool;
+        this.oalDefine = define;
+
+        configuration = new Configuration(new Version("2.3.28"));
+        configuration.setEncoding(Locale.ENGLISH, "UTF-8");
+        // Use V2-specific FreeMarker templates
+        configuration.setClassLoaderForTemplateLoading(OALClassGeneratorV2.class.getClassLoader(), "/code-templates-v2");
+    }
+
+    /**
+     * Generate metrics CtClass without loading to JVM.
+     */
+    public CtClass generateMetricsCtClass(CodeGenModel model) throws OALCompileException {
+        String className = metricsClassName(model, false);
+        CtClass parentMetricsClass;
+
+        try {
+            parentMetricsClass = classPool.get(METRICS_FUNCTION_PACKAGE + model.getMetricsClassName());
+        } catch (Exception e) {
+            throw new OALCompileException("Can't find parent class for " + className, e);
+        }
+
+        CtClass metricsClass = classPool.makeClass(metricsClassName(model, true), parentMetricsClass);
+
+        try {
+            metricsClass.addInterface(classPool.get(WITH_METADATA_INTERFACE));
+        } catch (Exception e) {
+            throw new OALCompileException("Can't find WithMetadata interface for " + className, e);
+        }
+
+        ClassFile metricsClassClassFile = metricsClass.getClassFile();
+        ConstPool constPool = metricsClassClassFile.getConstPool();
+
+        // Create empty constructor
+        try {
+            CtConstructor defaultConstructor = CtNewConstructor.make("public " + className + "() {}", metricsClass);
+            metricsClass.addConstructor(defaultConstructor);
+        } catch (Exception e) {
+            throw new OALCompileException("Can't add empty constructor in " + className, e);
+        }
+
+        // Add fields with annotations
+        for (CodeGenModel.SourceFieldV2 field : model.getFieldsFromSource()) {
+            try {
+                CtField newField = CtField.make(
+                    "private " + field.getType().getName() + " " + field.getFieldName() + ";", metricsClass);
+
+                metricsClass.addField(newField);
+                metricsClass.addMethod(CtNewMethod.getter(field.getFieldGetter(), newField));
+                metricsClass.addMethod(CtNewMethod.setter(field.getFieldSetter(), newField));
+
+                AnnotationsAttribute annotationsAttribute = new AnnotationsAttribute(
+                    constPool, AnnotationsAttribute.visibleTag);
+
+                // Add @Column annotation
+                Annotation columnAnnotation = new Annotation(Column.class.getName(), constPool);
+                columnAnnotation.addMemberValue("name", new StringMemberValue(field.getColumnName(), constPool));
+                if (field.getType().equals(String.class)) {
+                    columnAnnotation.addMemberValue("length", new IntegerMemberValue(constPool, field.getLength()));
+                }
+                annotationsAttribute.addAnnotation(columnAnnotation);
+
+                if (field.isID()) {
+                    Annotation banyanSeriesIDAnnotation = new Annotation(BanyanDB.SeriesID.class.getName(), constPool);
+                    banyanSeriesIDAnnotation.addMemberValue("index", new IntegerMemberValue(constPool, 0));
+                    annotationsAttribute.addAnnotation(banyanSeriesIDAnnotation);
+
+                    Annotation enableDocValuesAnnotation = new Annotation(ElasticSearch.EnableDocValues.class.getName(), constPool);
+                    annotationsAttribute.addAnnotation(enableDocValuesAnnotation);
+                }
+
+                if (field.isShardingKey()) {
+                    Annotation banyanShardingKeyAnnotation = new Annotation(BanyanDB.ShardingKey.class.getName(), constPool);
+                    banyanShardingKeyAnnotation.addMemberValue("index", new IntegerMemberValue(constPool, field.getShardingKeyIdx()));
+                    annotationsAttribute.addAnnotation(banyanShardingKeyAnnotation);
+                }
+
+                newField.getFieldInfo().addAttribute(annotationsAttribute);
+            } catch (Exception e) {
+                throw new OALCompileException("Can't add field " + field.getFieldName() + " in " + className, e);
+            }
+        }
+
+        // Generate methods using V2 templates
+        for (String method : METRICS_CLASS_METHODS) {
+            StringWriter methodEntity = new StringWriter();
+            try {
+                configuration.getTemplate("metrics/" + method + ".ftl").process(model, methodEntity);
+                metricsClass.addMethod(CtNewMethod.make(methodEntity.toString(), metricsClass));
+            } catch (Exception e) {
+                throw new OALCompileException("Can't generate method " + method + " for " + className, e);
+            }
+        }
+
+        // Add @Stream annotation
+        AnnotationsAttribute annotationsAttribute = new AnnotationsAttribute(
+            constPool, AnnotationsAttribute.visibleTag);
+        Annotation streamAnnotation = new Annotation(Stream.class.getName(), constPool);
+        streamAnnotation.addMemberValue("name", new StringMemberValue(model.getTableName(), constPool));
+        streamAnnotation.addMemberValue("scopeId", new IntegerMemberValue(constPool, model.getSourceScopeId()));
+        streamAnnotation.addMemberValue(
+            "builder", new ClassMemberValue(metricsBuilderClassName(model, true), constPool));
+        streamAnnotation.addMemberValue("processor", new ClassMemberValue(METRICS_STREAM_PROCESSOR, constPool));
+
+        annotationsAttribute.addAnnotation(streamAnnotation);
+        metricsClassClassFile.addAttribute(annotationsAttribute);
+
+        return metricsClass;
+    }
+
+    /**
+     * Generate dispatcher CtClass without loading to JVM.
+     */
+    public CtClass generateDispatcherCtClass(String scopeName, OALClassGeneratorV2.DispatcherContextV2 dispatcherContext) throws OALCompileException {
+        String className = dispatcherClassName(scopeName, false);
+        CtClass dispatcherClass = classPool.makeClass(dispatcherClassName(scopeName, true));
+
+        try {
+            CtClass dispatcherInterface = classPool.get(DISPATCHER_INTERFACE);
+            dispatcherClass.addInterface(dispatcherInterface);
+
+            // Set generic signature
+            String sourceClassName = oalDefine.getSourcePackage() + dispatcherContext.getSourceName();
+            SignatureAttribute.ClassSignature dispatcherSignature =
+                new SignatureAttribute.ClassSignature(
+                    null, null,
+                    new SignatureAttribute.ClassType[]{
+                        new SignatureAttribute.ClassType(
+                            SourceDispatcher.class.getCanonicalName(),
+                            new SignatureAttribute.TypeArgument[]{
+                                new SignatureAttribute.TypeArgument(
+                                    new SignatureAttribute.ClassType(sourceClassName))
+                            }
+                        )
+                    }
+                );
+
+            dispatcherClass.setGenericSignature(dispatcherSignature.encode());
+        } catch (Exception e) {
+            throw new OALCompileException("Can't find Dispatcher interface for " + className, e);
+        }
+
+        // Generate doMetrics methods for each metric
+        for (CodeGenModel metric : dispatcherContext.getMetrics()) {
+            StringWriter methodEntity = new StringWriter();
+            try {
+                configuration.getTemplate("dispatcher/doMetrics.ftl").process(metric, methodEntity);
+                dispatcherClass.addMethod(CtNewMethod.make(methodEntity.toString(), dispatcherClass));
+            } catch (Exception e) {
+                throw new OALCompileException("Can't generate method do" + metric.getMetricsName() + " for " + className, e);
+            }
+        }
+
+        // Generate dispatch method
+        try {
+            StringWriter methodEntity = new StringWriter();
+            configuration.getTemplate("dispatcher/dispatch.ftl").process(dispatcherContext, methodEntity);
+            dispatcherClass.addMethod(CtNewMethod.make(methodEntity.toString(), dispatcherClass));
+        } catch (Exception e) {
+            throw new OALCompileException("Can't generate method dispatch for " + className, e);
+        }
+
+        return dispatcherClass;
+    }
+
+    private String metricsClassName(CodeGenModel model, boolean fullName) {
+        return (fullName ? oalDefine.getDynamicMetricsClassPackage() : "") + model.getMetricsName() + "Metrics";
+    }
+
+    private String metricsBuilderClassName(CodeGenModel model, boolean fullName) {
+        return (fullName ? oalDefine.getDynamicMetricsBuilderClassPackage() : "") + model.getMetricsName() + "MetricsBuilder";
+    }
+
+    private String dispatcherClassName(String scopeName, boolean fullName) {
+        return (fullName ? oalDefine.getDynamicDispatcherClassPackage() : "") + scopeName + "Dispatcher";
+    }
+}